2011-10-18 38 views
3

我在使用HTMLPurifier删除我的样式标记时遇到问题。这是(测试)配置使用:如何在HTML净化器过滤中允许内嵌样式标记

$config = HTMLPurifier_Config::createDefault(); 
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding 
$config->set('HTML.Doctype', 'HTML 4.01 Transitional'); // replace with your doctype 
$config->set('Cache.DefinitionImpl', null); 
$config->set('HTML.AllowedElements','div'); 
$config->set('HTML.AllowedAttributes', "*.style"); 
$config->set('CSS.AllowedProperties', 'background-color'); 

当我过滤此HTML:

<div style="background-color: #fff;">test</div> 
<div style="border: 1px solid #000;">test</div> 

这就是我得到:

<div>test</div> 
<div style="border:;">test</div> 

我不明白为什么边界属性被留下(但它的值被剥离),以及为什么背景颜色属性被删除。如何配置这些样式标签才能通过过滤器?另外,我想允许我允许的样式属性的任何样式值。

+0

即使注释掉配置,它是否会被删除? –

+0

我很抱歉 - 经过几个小时的故障排除后,我发现错误,并且它与HTML Purifier无关。一旦我解决了它,配置设置按预期工作。感谢一个伟大的图书馆。 – Johan

回答

1

试试这个:

$config = HTMLPurifier_Config::createDefault(); 
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding 
$config->set('HTML.Doctype', 'HTML 4.01 Transitional'); 
$config->set('CSS.Trusted', 'HTML 4.01 Transitional'); // allow any css 
$config->set('HTML.Allowed','div[style]'); 
$config->set('CSS.AllowedProperties', 'background-color'); 

这对我的作品!

相关问题