Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLPurifier allow class attribute

How can i allow "class" in HTMLPurifier? I am trying to purify this:

 <div class="txt_r" id="test">Blah</div>

And i get:

 <div id="test">Blah</div>

Why class is dissapeared? I am using next config:

 $config->set('Attr.EnableID', true);
 $config->set('CSS.Trusted', true);
 $config->set('HTML.AllowedAttributes', 'style, src, class');
like image 664
SomeoneS Avatar asked Jul 31 '12 19:07

SomeoneS


1 Answers

Your problem is probably that HTML.AllowedAttributes doesn't actually work that way. :) From the docs:

The syntax is "tag.attr" or "*.attr" for the global attributes (style, id, class, dir, lang, xml:lang).

What you probably want is...

$config->set('HTML.AllowedAttributes', 'img.src,*.style,*.class');

You also shouldn't use HTML.AllowedAttributes by itself, but in tandem with HTML.AllowedElements:

$config->set('HTML.AllowedElements', 'img,div');

Alternatively, use neither HTML.AllowedAttributes nor HTML.AllowedElements and instead use HTML.Allowed. That would look something like this:

$config->set('HTML.Allowed', 'div, *[style|class], img[src]');
like image 177
pinkgothic Avatar answered Nov 12 '22 04:11

pinkgothic