使用 htmlpurifier 允许自定义属性到元素

Allowing custom attribute to element with htmlpurifier

我的小过程中途卡住了,需要最后一步的帮助。

需要添加自定义元素以接受将通过文本区域表单传递的自定义 book 元素。

$dirty = 'you should check this out: <book author="me">my book title</book>!';

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'book');
$def = $config->getHTMLDefinition(true);
$def->addElement('book',  'Inline', 'Inline', 'Common');
$purifier = new HTMLPurifier($config);
echo $purifier->purify($dirty);

Returns: you should check this out: <book>my book title</book>!

但它正在剥离 author="me"

如果我在标准的 htmlpurifier attr allowance 中添加,例如:

`$config->set('HTML.Allowed', 'book[author]');`

它returns错误: Attribute 'author' in element 'book' not supported

我需要它,以便可以包含或不包含 author="me"

有人可以分享代码 change/addition 我需要获得 author="me" 允许 - 谢谢!

我是运行 htmlpurifier 4.6.0

您在 $def->addElement(...) 通话后尝试过使用 $def->addAttribute('book', 'author', 'CDATA'); 吗?