HtmlPurifier - 允许数据属性

HtmlPurifier - allow data attribute

我试图让一些 data-attribute 和 htmlPurifier 用于我的所有 span 但没办法...

我有这个字符串:

<p>
    <span data-time-start="1" data-time-end="5" id="5">
       <word class="word">My</word>
       <word class="word">Name</word>
    </span>
    <span data-time-start="6" data-time-end="15" id="88">
       <word class="word">Is</word>
       <word class="word">Zooboo</word>
    </span>
<p>

我的 htmlpurifier 配置:

$this->HTMLpurifierConfigInverseTransform = \HTMLPurifier_Config::createDefault();
$this->HTMLpurifierConfigInverseTransform->set('HTML.Allowed', 'span,u,strong,em');
$this->HTMLpurifierConfigInverseTransform->set('HTML.ForbiddenElements', 'word,p');
$this->HTMLpurifierConfigInverseTransform->set('CSS.AllowedProperties', 'font-weight, font-style, text-decoration');
$this->HTMLpurifierConfigInverseTransform->set('AutoFormat.RemoveEmpty', true);

我这样净化我的 $value

$purifier = new \HTMLPurifier($this->HTMLpurifierConfigInverseTransform);
var_dump($purifier->purify($value));die;

得到这个:

<span>My Name</span><span>Is Zoobo</span>

但是如何在我的 span 中保存我的数据属性 iddata-time-startdata-time-end

我需要这个 :

<span data-time-start="1" data-time-end="5" id="5">My Name</span data-time-start="6" data-time-end="15" id="88"><span>Is Zoobo</span>

我尝试使用此配置进行测试:

$this->HTMLpurifierConfigInverseTransform->set('HTML.Allowed', 'span[data-time-start],u,strong,em');

但是错误信息:

User Warning: Attribute 'data-time-start' in element 'span' not supported (for information on implementing this, see the support forums)

感谢您的帮助!!

编辑 1

我第一次尝试使用此代码行允许 ID:

$this->HTMLpurifierConfigInverseTransform->set('Attr.EnableID', true);

它对我不起作用...

编辑 2

对于 data-* 属性,我添加了这一行,但也没有任何反应...

$def = $this->HTMLpurifierConfigInverseTransform->getHTMLDefinition(true);
$def->addAttribute('sub', 'data-time-start', 'CDATA');
$def->addAttribute('sub', 'data-time-end', 'CDATA');

HTML Purifier 了解 HTML 的结构,并将此知识用作其白名单过程的基础。如果您将标准属性添加到白名单,它不允许该属性的任意内容 - 它 理解 该属性并且仍然会拒绝没有意义的内容。

例如,如果您在某处有一个采用数值的属性,HTML Purifier 仍会拒绝 HTML 试图为该属性输入值 'foo'。

如果你添加自定义属性,只是将它添加到白名单并没有教 HTML Purifier 如何处理属性:它可以在这些属性中期望什么数据?哪些数据是恶意的?

这里有大量文档可以告诉 HTML Purifier 您自定义属性的结构:Customize

<a> 标签的 'target' 属性有一个代码示例:

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
$config->set('HTML.DefinitionRev', 1);
$config->set('Cache.DefinitionImpl', null); // remove this later!
$def = $config->getHTMLDefinition(true);
$def->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');

这会将 target 添加为仅接受值 "_blank""_self""_target""_top" 的字段。这比实际的 HTML 定义要严格一些,但对于大多数目的来说完全足够了。

这是 data-time-startdata-time-end 需要采用的一般方法。有关可能的配置,请查看官方 HTML Purifier 文档(如上链接)。从你的例子中我最好的猜测是你不想要 Enum#...Number,就像这样......

$def->addAttribute('span', 'data-time-start', 'Number');
$def->addAttribute('span', 'data-time-end', 'Number');

...但是检查一下,看看什么最适合您的用例。 (在您实施此操作时,请不要忘记您还需要像当前一样在白名单中列出属性。)

对于 id,您应该将 Attr.EnableID = true 作为配置的一部分。

希望对您有所帮助!

如果其他人(像我一样)因为 id 属性不起作用,更奇怪的是 在所有情况下都不起作用

在版本 4.8.0 中添加了 Attr.ID.HTML5 并反映了为 HTML5 引入的宽松格式的用法。

例如,不允许使用数值,以及以数字开头的值。以下示例在 HTML5 中均有效,但只有前三个在 HTML5 之前有效(净化器的默认行为):

  1. foo(两者)
  2. foo-bar(两者)
  3. foo-10(两者)
  4. 10(仅限 HTML5)
  5. 10-foo(仅限 HTML5)