【问题标题】:How to add elements figureand figcaption in htmlpurifier?如何在 htmlpurifier 中添加元素 figure 和 figcaption?
【发布时间】:2019-05-14 06:05:51
【问题描述】:

当我添加带有参数的图像时,我遇到了错误

ErrorException:不支持元素“figcaption”(对于 有关实施此功能的信息,请参阅支持论坛)

我的代码

         'HTML.Allowed' => 'a[href],blockquote,br,del,em,figcaption,figure,h1,h2,h3,h4,h5,h6,img[title|alt|src],li,ol,p,pre,strong,ul',

 .//////////////////////////////////////////////////////////////

        $config = HTMLPurifier_Config::createDefault();

        $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');

        $config->set('HTML.DefinitionRev', 1);

        if ($def = $config->maybeGetRawHTMLDefinition()) {
            $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
            $def->addElement('figcaption', 'Block', 'Flow', 'Common');
        }

【问题讨论】:

  • 您是否尝试切换您的 figcaptionfigure 定义的顺序?
  • 实际上,先解决这个问题。你的代码对我有用;我将看看我是否可以巧妙地破解它以找出您的情况可能出了什么问题并报告。

标签: php element figure htmlpurifier


【解决方案1】:

您的代码对我来说看起来不错。您执行操作的顺序可能会导致操作失败 - 在添加元素之前,请确保您的应用程序没有尝试 purify()。这个例子对我有用:

<?php

require_once 'library/HTMLPurifier.auto.php';

$dirty_html = '<figure>
    <img src="/media/examples/elephant-660-480.jpg"
         alt="Elephant at sunset">
    <figcaption>An elephant at sunset</figcaption>
</figure>';

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'a[href],blockquote,br,del,em,figcaption,figure,h1,h2,h3,h4,h5,h6,img[title|alt|src],li,ol,p,pre,strong,ul');
$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
$config->set('HTML.DefinitionRev', 1);
if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('figcaption', 'Block', 'Flow', 'Common');
    $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
}

$purifier = new HTMLPurifier($config);

$clean_html = $purifier->purify($dirty_html);

echo $clean_html;

这给了我&lt;figure&gt;&lt;img src="/media/examples/elephant-660-480.jpg" alt="Elephant at sunset" /&gt;&lt;figcaption&gt;An elephant at sunset&lt;/figcaption&gt;&lt;/figure&gt;

注释掉这些行:

$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
$config->set('HTML.DefinitionRev', 1);
if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('figcaption', 'Block', 'Flow', 'Common');
    $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
}

...给了我您在问题中引用的错误,表明当您调用 purify() 时,代码尚未执行。

如果您告诉我们更多关于在您的应用程序工作流程中您尝试调整 HTML 定义的位置,以及您正在净化的位置,我也许可以帮助您解决问题的原因问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 2021-06-18
    相关资源
    最近更新 更多