HTMLPurifier - 将名称属性添加到锚标记

HTMLPurifier - add name attribute to anchor tag

HTMLPurifier 从 anchor 标签中去除 name 属性 过去使用 documentation,我成功创建了一个新元素 "include"。

但是我无法获取代码来为 anchor 标签添加 name 属性。我的偏好是不限制 name 值。任何帮助将不胜感激。

这是代码:

$config = HTMLPurifier_Config::createDefault();
// custom tag for included files
$config->set('HTML.DefinitionID', 'include');
$config->set('HTML.DefinitionRev', 16);
if ($def = $config->maybeGetRawHTMLDefinition()) {
    // this works for adding the include element
    $def->addElement('include', 'Block', 'Empty', 'Common', array('file*' => 'URI', 'height' => 'Text', 'width' => 'Text'));

  // This doesn't work - among the many things, I've tried...
  // 1) trying to get at least name ="target" to work
  // $def->addAttribute('a', 'name', 'Enum#target');
  // 2) trying to get any text to work
  // $def->addAttribute('a', 'name', 'text');
  // $def->addAttribute('a', 'name', new HTMLPurifier_AttrDef_Text());
}

$purifier = new HTMLPurifier($config);

我认为您必须在 Name.php 中将 name 属性设置为 true,请检查

http://htmlpurifier.org/live/configdoc/plain.html#HTML.Attr.Name.UseCDATA

看起来有两种选择。一种是使用 HTML.Allowed 定义所有元素和标签。更简单的是设置 Attr.EnableID 允许 idname 属性。设置 Attr.IDPrefix 将有助于防止名称冲突。这有效:

$config = HTMLPurifier_Config::createDefault();
// allow name and id attributes
$config->set('Attr.EnableID', true);
// prefix all id and names with user_
$config->set('Attr.IDPrefix', 'user_');

// custom tag 
$config->set('HTML.DefinitionID', 'include');
$config->set('HTML.DefinitionRev', 2);
if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('include', 'Block', 'Empty', 'Common', array('file*' => 'URI', 'height' => 'Text', 'width' => 'Text'));
}

$purifier = new HTMLPurifier($config);