Menu

#166 Bad handling of <p> inside <ul> in HTML 5

v2.19
closed-fixed
nobody
None
5
2017-02-06
2016-01-05
No

Hello again :)

This time I may have found a real bug.

Here is the code to reproduce:

package com.guillaumedelhumeau.htmlcleaner;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

/**

 * @version $Id: $
 */
public class Test
{
    public static void main(String[] args)
    {
        String htmlContent = "<ul><p>Hello</p></ul>";

        CleanerProperties props = new CleanerProperties();
        props.setHtmlVersion(4);

        HtmlCleaner cleaner = new HtmlCleaner(props);

        try {
            TagNode cleanedNode = cleaner.clean(htmlContent);
            displayElement(cleanedNode, 0);
        } catch (Exception e) {
            throw new RuntimeException("Unhandled error when cleaning HTML", e);
        }
    }

    private static void displayElement(TagNode tagNode, int level)
    {
        for (int i = 0; i < level; ++i) {
            System.out.print("--");
        }
        System.out.println(tagNode.getName());
        for (TagNode child : tagNode.getChildTagList()) {
            displayElement(child, level + 1);
        }
    }
}

Results with HTML version 4:

html
--head
--body
----ul
------p

Results with HTML version 5:

props.setHtmlVersion(5);
html
--head
--body
----p
----ul

Now the

tag is outside the

    .

    The final objective is to get this code by using the serializer:

    <html>
      <head>
      </head>
      <body>
          <ul>
              <li> <!-- Adding the missing li here -->
                  <p>Hello</p>
               </li>
           </ul>
       </body>
    </html>
    

    But it doesn't work anymore with HTML 5.

Discussion

  • Guillaume Delhumeau

    SF has eatten my message.

    I repost the end of it:

    Now the "p" tag is outside the "ul".

    The final objective is to get this code by using the serializer:

    <html>
        <head>
        </head>
        <body>
            <ul>
                <li><!-- Adding the missing li here -->
                   <p>Hello</p>
                </li>
            </ul>
        </body>
    </html>
    

    But it doesn't work anymore with HTML 5.

     

    Last edit: Guillaume Delhumeau 2016-01-05
  • Scott Wilson

    Scott Wilson - 2017-02-06
    • Group: v 2.7 --> v2.19
     
  • Scott Wilson

    Scott Wilson - 2017-02-06

    Hmm, this is going to be an interesting challenge..!

    I'm on the case...

     
  • Scott Wilson

    Scott Wilson - 2017-02-06

    OK I've got a fix for this. For some tags (UL, OL, TR) I try to first see if injecting an intervening tag (LI, TD) will fix things before trying moving the content to somewhere its allowed. I've set this behaviour using a "preferredChild" in TagInfo that is set by TagProvider just for those three tags.

     
  • Scott Wilson

    Scott Wilson - 2017-02-06
    • status: open --> closed-fixed
     

Log in to post a comment.

Monday.com Logo