【问题标题】:htmlpurifier custom attributeshtmlpurifier 自定义属性
【发布时间】:2011-05-26 17:27:08
【问题描述】:

如何在 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')

【问题讨论】:

  • 如果您需要改进您的问题,请随时进行编辑。如果您有答案......请随时发布

标签: html custom-attributes htmlpurifier


【解决方案1】:

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找到默认的属性类型

【讨论】:

  • 谢谢,这种方式让我畅通无阻!
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
相关资源
最近更新 更多