这个编码可以改进,但我改变了 AttrValidator.php 我添加了以下函数:
/*=======================================
==-- LLS start wildcard handling
==--
==-- data-* ^data-(((?![\s=]).)+)
=========================================*/
private function checkWildCardAttributes($deflist, $attr_key, $value, $config, $context) {
$result = false;
foreach ($deflist as $def_key => $def_value) {
if (strpos($def_key, '*') !== FALSE) {
// found a wildcard
// does wildcard match this attr
$re = implode('(((?![\s=]).)+)',explode("*",$def_key));
preg_match('#^'.$re.'#',$attr_key,$wcout);
if (count($wcout)>0) {
// the attribute matched against the wildcard definition
$result = $deflist[$attr_key]->validate(
$value,
$config,
$context
);
break;
}
}
}
return $result;
}
在函数 validateToken 中找到以下行:
// put the results into effect
在此行之前添加以下内容:
/*=======================================
==-- start wildcard handling
=========================================*/
if (!$result) {
// definitions
$result = $this->checkWildCardAttributes($defs, $attr_key, $value, $config, $context);
if (!$result) {
// global definitions
$result = $this->checkWildCardAttributes($d_defs, $attr_key, $value, $config, $context);
}
}
//=======================================
// put the results into effect
if ($result === false || $result === null) {
在此之后,您可以在属性定义中使用 * 通配符。例子:
// See: AttrValidator.php in the HTMLPurifier for the wildcard addition
$def->info_global_attr['data-*'] = new HTMLPurifier_AttrDef_Text;
就像我说的,它可以改进......但它确实有效:)
玩得开心....