htmlpurifier自定义属性 [英] htmlpurifier custom attributes

查看:207
本文介绍了htmlpurifier自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



输入:

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

导致错误:

 不支持元素 img中的属性数据类型 
(有关实现此信息的信息,请参阅支持论坛)


HtmlPurifier选项设置为:

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


解决方案

HTML净化器定义了符合标准的属性矩阵,当您尝试使用该矩阵中未定义的属性时会抱怨。但是,您可以使用函数HTMLDefinition :: addAttribute()将新属性添加到默认定义,如下所示:

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

请参见 HTMLDefinition :: addAttribute 文本 这是属性类型,您可以从 AttrTypes.php


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

解决方案

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

这篇关于htmlpurifier自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆