Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attributes not working #2

Open
jplew opened this issue Sep 28, 2017 · 6 comments
Open

Custom attributes not working #2

jplew opened this issue Sep 28, 2017 · 6 comments

Comments

@jplew
Copy link

jplew commented Sep 28, 2017

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?

@LukeTowers
Copy link
Owner

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'],
        ],

@jplew
Copy link
Author

jplew commented Sep 28, 2017

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.

@jplew
Copy link
Author

jplew commented Sep 28, 2017

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

@pastuh
Copy link

pastuh commented Oct 20, 2017

I got same problem.
Tested both Mewebstudio and LukeTowers no result..

@LukeTowers
Copy link
Owner

@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.

@pastuh
Copy link

pastuh commented Oct 20, 2017

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants