Welcome! » Log In » Create A New Profile

RemoveEmpty tags but keep table

Posted by Sebastian 
Sebastian
RemoveEmpty tags but keep table
October 11, 2012 02:04PM

Hi, I try to validate some HTML code and remove empty tags. That works fine, but in some cases I don't want to have empty tags removed like and in tables.

How can I do this?

My actual configuration is



    $config = HTMLPurifier_Config::createDefault();

    $config->set('Core.Encoding', 'UTF-8');
    $config->set('HTML.Doctype', 'HTML 4.01 Transitional');

    $config->set('HTML.Allowed', 'sub, sup, table, tbody, thead, tfoot, tr, td[colspan|rowspan|scope],th[colspan|rowspan|scope], p, ol, ul, li, h2, h3, h4, h5, span[class], img[src]');

    $config->set('AutoFormat.RemoveEmpty', true);

    $config->set('Attr.DefaultImageAlt', '');

    $config->set('Core.CollectErrors', true);

    $classes_list = 'Enum#cave,ankerlink,lektorat,linksuggest,leitwort,wichtig,unwichtig;

    $def = $config->getHTMLDefinition(true);

    $def->addAttribute('span', 'class*', $classes_list);
    $def->addAttribute('img', 'src*', 'Text');

Greetings, Sebastian

Sebastian
Re: RemoveEmpty tags but keep table
October 11, 2012 02:08PM

yeah, I meant I try to keep

<th></th>

and

<td></td>
Re: RemoveEmpty tags but keep table
October 11, 2012 02:19PM

Try putting a non-breaking space in them?

Sebastian
Re: RemoveEmpty tags but keep table
October 11, 2012 02:30PM

That's not a really practical solution, since the HTML code is user generated..

Re: RemoveEmpty tags but keep table
October 11, 2012 02:42PM

Anyway, this is probably a bug on the part of remove empty. Maybe try this patch?

diff --git a/library/HTMLPurifier/Injector/RemoveEmpty.php b/library/HTMLPurifier/Injector/RemoveEmpty.php
index 638bfca..39d6b77 100644
--- a/library/HTMLPurifier/Injector/RemoveEmpty.php
+++ b/library/HTMLPurifier/Injector/RemoveEmpty.php
@@ -30,7 +30,7 @@ class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
             break;
         }
         if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
-            if ($token->name == &#039;colgroup&#039;) return;
+            if ($token->name == &#039;colgroup&#039; || $token->name == &#039;th&#039; || $token->name == &#039;td&#039;) return;
             $this->attrValidator->validateToken($token, $this->config, $this->context);
             $token->armor[&#039;ValidateAttributes&#039;] = true;
             if (isset($token->attr[&#039;id&#039;]) || isset($token->attr[&#039;name&#039;])) return;
Sebastian
Re: RemoveEmpty tags but keep table
October 11, 2012 02:56PM

Great, that works! Thanks a lot!

Chris T
Re: RemoveEmpty tags but keep table
October 24, 2012 07:21PM

iframes should also be exempt from the remove empty rule.

Re: RemoveEmpty tags but keep table
October 27, 2012 05:46PM

OK, done.

Mehrdad
Re: RemoveEmpty tags but keep table
July 10, 2016 03:24AM

Hi, I tried to remove empty tags with this:

$config->set(&#039;AutoFormat.RemoveEmpty&#039;, true);

But this config consider below html as a not empty tag:

<li>


</li>

Any solution?

Thanks.

ps: I'm new to htmlpurifier.

Re: RemoveEmpty tags but keep table
March 08, 2017 03:09AM

I can't reproduce, here is the text pasted in here.

Can you repro the problem on the demo?

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