2009-11-10 52 views
1

我试图让HTML净化器过滤器中的元素的rel属性。我按照本指南http://htmlpurifier.org/docs/enduser-customize.html这里是我的代码:将属性添加到HTML净化器过滤器?

$config = HTMLPurifier_Config::createDefault(); 
$config->set('HTML.Doctype', 'XHTML 1.0 Strict'); 
$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial'); 
$config->set('HTML.DefinitionRev', 1); 
$config->set('Cache.DefinitionImpl', null); // remove this later! 
$def = $config->getHTMLDefinition(true); 
$def->addAttribute('a', 'href*', 'URI'); 
$def->addAttribute('a', 'rel', 'CDATA'); 
$purifier = new HTMLPurifier($config); 

然而,HTML净化器还过滤掉所有属性相对......我有点困惑的问题可能是什么。

当我使用:

$config->set('Attr', 'AllowedRel', array('something')); 

我得到这个错误:

Notice: Using deprecated API: use $config->set('Attr.AllowedRel', ...) instead on line 191 in file C:\wamp\www\neonet\application\modules\admin\controllers\IndexController.php in C:\wamp\www\neonet\library\My\htmlpurifier-4.0.0-standalone\HTMLPurifier.standalone.php on line 1819

编辑:

新代码:

$config = HTMLPurifier_Config::createDefault(); 
$config->set('HTML.Doctype', 'XHTML 1.0 Strict'); 
$config->set('Attr.AllowedRel', array('something')); 
$purifier = new HTMLPurifier($config); 

当我使用:

<href="/" rel="something">anchor</a> 

Rel属性仍然被过滤。

回答

2

This configuration directive may be of interest给你。至于你的代码,它适用于我;也许你已经开启了魔术引号或者没有适当地刷新缓存? (在这种情况下,尝试碰撞DefinitionRev。)

尝试使用rel时的另一个经典错误是它不适用于XHTML Strict; doctype没有定义rel,所以Attr.AllowedRel不会做任何事情(这应该在文档中提到,但不是)。所以,如果你想保留你的W3C复选标记,你必须选择一个不同的文档类型或使用原始代码。

+0

是的,但是当我尝试使用它时,出现错误,请参阅上文,我编辑了我的帖子。 – 2009-11-10 18:00:39

+0

确定忽略最后一条评论,我使用的是弃用的API ...但它仍然无效。 – 2009-11-10 18:04:18

+0

谢谢,我会改变文档类型。 – 2009-11-11 12:28:27