Welcome! » Log In » Create A New Profile

Allowed elements input format and style attribute

Posted by atDev 
Allowed elements input format and style attribute
August 15, 2010 10:26PM

The configuration documentation says the AllowedElements should be set as an array.

However when we pass in:

Array
(
    [0] => p
    [1] => ul
    [2] => ol
    [3] => li
    [4] => strong
    [5] => em
    [6] => u
    [7] => span
    [8] => hr
    [9] => div
    [10] => br
    [11] => b
    [13] => i
)

We get errors like: Element '0' is not supported Element '1' is not supported etc.

What format should the input be?

$config->set('HTML', 'AllowedElements', (array) $tags['tags']);

$tags['tags'] is the above array.

Secondly how come we can't set 'style' as an attribute so styles can be added to elements?

<font style="...">
Re: Allowed elements input format and style attribute
August 15, 2010 10:33PM

Your array is missing an element at index 12, so we're interpreting it as an associative array (ala 'foo' => true) and getting things wrong. Try using the more general %HTML.Allowed, which allows attributes to be tuned too.

Re: Allowed elements input format and style attribute
August 15, 2010 10:37PM

The reason an array index is missing is because this is a part of a CMS software where we allow the admin to define what tags they want to allow.

We run array_unique on the admins input to prevent their input from including duplicate elements. So array_unique() is causing one of the duplicates to be removed, therefore the reason for the missed index.

I am not 100% sure why a missed index is causing it to be parsed as an associative array. What is the theory behind that just so I can understand why?

Here is a larger piece of the code:

        if(is_null($tags)) {
            $config->set(&#039;HTML&#039;, &#039;AllowedElements&#039;, null);
            $config->set(&#039;HTML&#039;, &#039;AllowedAttributes&#039;, null);
        } else {
            $config->set(&#039;HTML&#039;, &#039;AllowedElements&#039;, (array) $tags[&#039;tags&#039;]);
            $config->set(&#039;HTML&#039;, &#039;AllowedAttributes&#039;, (array) $tags[&#039;attributes&#039;]);
        }
Re: Allowed elements input format and style attribute
August 15, 2010 10:40PM

Also I notice htmlpurifier also fails if a duplicate tag exists in the array, which is the reason why we use array_unique.

Re: Allowed elements input format and style attribute
August 15, 2010 10:44PM

Run the result through array_values and you should be peachy.

Re: Allowed elements input format and style attribute
August 15, 2010 10:47PM

Ok will do.

I guess you don't consider this a bug then? :)

It's no problem hacking around this quirk but see other users maybe having trouble with it in the future. Perhaps this configuration setting can be made to be more forgiving on the input? If not, maybe a note could be added to the configuration stating that the array can not include duplicate tags and the array can not have missing indexes.

Re: Allowed elements input format and style attribute
August 15, 2010 10:55PM

You’re the first person to run into this problem, since the configuration system was revamped back in 2006. :-) If I receive another report I might consider making the heuristics for determining lookup array versus array that needs to be flipped better.

Re: Allowed elements input format and style attribute
August 15, 2010 11:59PM

Ok, maybe update the manual until then because even duplicate tags causes it to fail.

Re: Allowed elements input format and style attribute
September 04, 2010 02:29AM

Manual updated in Git.

Sorry, you do not have permission to post/reply in this forum.