htmlpurifier custom attributes

给你一囗甜甜゛ 提交于 2020-01-01 02:16:28

问题


How to allow custom (html5 data-*) attributes in HtmlPurifier?

Input:

<img src="/my.jpg" data-type="5" alt="" />

leads to an error:

Attribute 'data-type' in element 'img' not supported 
(for information on implementing this, see the support forums) 

HtmlPurifier options are set to:

'HTML.AllowedAttributes' => array('img.src', 'a.href', 'img.data-type')

回答1:


HTML purifier defines the matrix of attributes that are standard compliant and complains when you try to use an attribute that it is not defined in this matrix. However, you can add new attributes to the default definition using the function HTMLDefinition::addAttribute() as follow:

$config = HTMLPurifier_Config::createDefault();
$def = $config->getHTMLDefinition(true);
$def->addAttribute('img', 'data-type', 'Text');
$purifier = new HTMLPurifier($config);

See the definition of HTMLDefinition::addAttribute for more details. 'Text' here is the attribute type, you can find the default attribute type from AttrTypes.php



来源:https://stackoverflow.com/questions/4464004/htmlpurifier-custom-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!