Purifier
Purifier copied to clipboard
Custom attributes not working
Hi:
I'm trying to pass <img data-filename="...">
and getting this error:
ErrorException in HTMLDefinition.php line 379:
Attribute 'data-filename' in element 'img' not supported (for information on implementing this, see the support forums)
Here is the relevant part of my config/purifier.php
:
'HTML.Allowed' => 'div,b,strong,i,em,u,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src|style|data-filename]',
In addition, I'm trying to pass a 'style' attribute on my <img>
tag. Despite the fact I have also specified 'style' in my HTML.Allowed
, the clean()
function is stripping out my style attribute. It doesn't give an error message like it does for data-filename, however.
Am I doing something wrong?
Did you define those attributes in your config file? Not only do you have to enable them in the HTML.Allowed
property, you also have to tell HTMLPurifier that they exist by defining them in the custom_attributes
config property. https://github.com/LukeTowers/Purifier/blob/master/config/purifier.php#L97
Example:
'custom_attributes' => [
['a', 'target', 'Enum#_blank,_self,_target,_top'],
['img', 'style', 'Text'],
['img', 'data-filename', 'Text'],
],
ok, thanks, I'll try it right now. Perhaps I missed them, but are there are any docs/examples to explain usage? I just saw the simple example at the bottom of the README.md.
I tried setting the custom_attributes
like you said, and am getting the exact same error:
ErrorException in HTMLDefinition.php line 379:
Attribute 'data-filename' in element 'img' not supported (for information on implementing this, see the support forums)
in HTMLDefinition.php line 379
at HandleExceptions->handleError('512', 'Attribute 'data-filename' in element 'img' not supported (for information on implementing this, see the support forums) ', '/Users/govinda/Sites/intellitrucksmaster/vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLDefinition.php', '379', array('config' => object(HTMLPurifier_Config), 'block_wrapper' => 'p', 'parent' => 'div', 'def' => object(HTMLPurifier_ElementDef), 'support' => '(for information on implementing this, see the support forums) ', 'allowed_elements' => array(), 'allowed_attributes' => array('a.href' => true, 'a.title' => true, 'p.style' => true, 'span.style' => true, 'img.width' => true, 'img.height' => true, 'img.alt' => true, 'img.src' => true, 'img.style' => true, 'img.data-filename' => true), 'allowed' => 'div,b,strong,i,em,u,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src|style|data-filename]', 'd' => true, 'name' => 'u', 'element' => 'img', 'allowed_attributes_mutable' => array('img.data-filename' => true), 'x' => object(HTMLPurifier_AttrDef_Lang), 'attr' => 'lang', 'keys' => array('u@lang', 'lang', '*@lang', 'u.lang', '*.lang'), 'delete' => true, 'key' => '*.lang', 'info' => object(HTMLPurifier_ElementDef), 'tag' => 'u', 'elattr' => 'img.data-filename', 'bits' => array('img', 'data-filename'), 'c' => '2', 'attribute' => 'data-filename'))
Here is a gist of my config file: https://gist.github.com/jplew/07d9be71058927b0f3fb2e15b92ea468
I got same problem. Tested both Mewebstudio and LukeTowers no result..
@jplew @pastuh Try playing around with this code: https://github.com/LukeTowers/Purifier/blob/master/src/Purifier.php#L160 and see if you can get HTMLPurifier to accept any hardcoded custom attributes.
Sorry, but im new in Laravel.. I dont know what to edit.
I just tested default custom_attribute:
'custom_attributes' => [ ['a', 'target', 'Enum#_blank,_self,_target,_top'], ],
And looks like even target="_top"
not works..