1

Topic: keep_bad help/bug?

Try the following code, "elements" set to: ul, li.

    <font color="#123456">Valid text inside unwanted tag.</font>
    <ul>
        Invalid text inside ul without li
        <li>One</li>
        <li>Two</li>
    </ul>

I would like htmLawed to remove the invalid <font> tag while leaving its contents, as well as the invalid text inside the <ul> without a <li>.

keep_bad set to 0 will remove the <font> while leaving the text inside, but it will also leave the invalid text inside the <ul>. With keep_bad=3,4,5 or 6 it will remove the invalid text but only neutralize the <font> tags, not strip them.

Shouldn't keep_bad=4 or 6 do what I want?

I see a line in the source (436) that appears to only remove on 0, neutralize otherwise.

2

Re: keep_bad help/bug?

I changed line 436 to read:

 return (($cf['keep_bad'] % 2) ? str_replace(array('<', '>'), array('&lt;', '&gt;'), $in) : '');

and it's now behaving how I think it should. This assumes I correctly understand what the documentation says should happen.

3

Re: keep_bad help/bug?

Thanks for pointing this out. I will make sure that there are no other similar mistakes where the finer shade of 'keep_bad' is ignored before putting in the changes in the source.

(Added after some checking)

I have to say it is not as simple as that. Consider the results after implementing the code change with input

<< * >> Want this << * >>
<p>Disallowed p</p>
<ul>Invalid<li>Two</li></ul>

With 'keep_bad' as 5, it becomes (as desired)

&lt;&lt; * &gt;&gt; Want this &lt;&lt; * &gt;&gt;
&lt;p&gt;Disallowed p&lt;/p&gt;
<ul><li>Two</li></ul>

But with 'keep_bad' as 6, it becomes

&gt; Want this &gt;
Disallowed p
<ul><li>Two</li></ul>

A better fix seems to be

if(!preg_match('`^<(/?)([a-zA-Z1-6]+)([^>]*?)\s?>$`m', $in, $m)){
 return str_replace(array('<', '>'), array('&lt;', '&gt;'), $in);
}elseif(!isset($cf['elements'][($e = strtolower($m[2]))])){
 return (($cf['keep_bad']%2) ? str_replace(array('<', '>'), array('&lt;', '&gt;'), $in) : '');
}

I will put it in the next release.

4

Re: keep_bad help/bug?

This issue is fixed in the new release (version 1.0.2)

5

Re: keep_bad help/bug?

Thanks, glad I could help.