如何在htmlpurifier中添加元素figure和figcaption?

How to add elements figureand figcaption in htmlpurifier?

当我添加带有参数的图像时出现错误

ErrorException: Element 'figcaption' is not supported (for information on implementing this, see the support forums)

我的代码

         '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');
        }

我觉得你的代码不错。您执行操作的顺序可能会导致操作失败 - 请确保您的应用程序在添加元素之前没有尝试 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;

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

注释掉这些行:

$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 定义,以及您要净化的地方,我可能能够帮助您解决问题的原因。