htmlpurifier 自定义属性

发布于 2024-10-08 03:54:29 字数 422 浏览 0 评论 0原文

如何在 HtmlPurifier 中允许自定义(html5 data-*)属性?

输入:

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

导致错误:

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

HtmlPurifier 选项设置为:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-10-15 03:54:29

HTML 净化器定义了符合标准的属性矩阵,并在您尝试使用该矩阵中未定义的属性时发出警告。但是,您可以使用 HTMLDefinition::addAttribute() 函数向默认定义添加新属性,如下所示:

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

请参阅 HTMLDefinition::addAttribute 了解更多详细信息。 'Text' 这里是属性类型,你可以从 AttrTypes.php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文