Abela
Abela

Reputation: 1233

Allowing custom attribute to element with htmlpurifier

Got stuck half-way through my little process, need help with this last step.

Needed to add a custom element to accept a custom book element that would be passed through a textarea form.

$dirty = 'you should check this out: <book author="me">my book title</book>!';

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'book');
$def = $config->getHTMLDefinition(true);
$def->addElement('book',  'Inline', 'Inline', 'Common');
$purifier = new HTMLPurifier($config);
echo $purifier->purify($dirty);

Returns: you should check this out: <book>my book title</book>!

But it is stripping out the author="me"

If I add in the standard htmlpurifier attr allowance, such as:

`$config->set('HTML.Allowed', 'book[author]');`

it returns error: Attribute 'author' in element 'book' not supported

I need it so that the author="me" can be included or not included.

Can somebody please share the code change/addition I need to get the author="me" to be allowed - thanks!

I am running htmlpurifier 4.6.0

Upvotes: 2

Views: 1687

Answers (1)

pinkgothic
pinkgothic

Reputation: 6179

Have you tried using $def->addAttribute('book', 'author', 'CDATA'); after your $def->addElement(...) call?

Upvotes: 3

Related Questions