x17*_*7az 12 html5 custom-attributes htmlpurifier
如何在HtmlPurifier中允许自定义(html5 data-*)属性?
输入:
<img src="/my.jpg" data-type="5" alt="" />
Run Code Online (Sandbox Code Playgroud)
导致错误:
Attribute 'data-type' in element 'img' not supported
(for information on implementing this, see the support forums)
Run Code Online (Sandbox Code Playgroud)
HtmlPurifier选项设置为:
'HTML.AllowedAttributes' => array('img.src', 'a.href', 'img.data-type')
Run Code Online (Sandbox Code Playgroud)
小智 19
HTML净化器定义符合标准的属性矩阵,并在尝试使用未在此矩阵中定义的属性时进行抱怨.但是,您可以使用HTMLDefinition :: addAttribute()函数将新属性添加到默认定义,如下所示:
$config = HTMLPurifier_Config::createDefault();
$def = $config->getHTMLDefinition(true);
$def->addAttribute('img', 'data-type', 'Text');
$purifier = new HTMLPurifier($config);
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅HTMLDefinition :: addAttribute的定义.'Text'这是属性类型,您可以从AttrTypes.php中找到默认属性类型