2009-11-10 47 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