Monks, I am cleaning up thousands up messy html files, many with ugly nesting, using HTML::TreeBuilder. The following two bits of messily nested html are structured the same, only in one there is nesting with a header tag, and in the other there is nesting with a font tag. As you can see in the output, only the nesting with font is preserved. I would like both strings to parse identically. Is there some way I can get TreeBuilder to do that?

use strict; use warnings; use HTML::TreeBuilder; use HTML::Element; my $html_bold = <<END; <b><h2> Nested once. <b><h2>Nested twice.</h2></b> Nested once. </h2> </b> END my $tree_bold = HTML::TreeBuilder->new_from_content($html_bold); print "Bold:\n\n"; $tree_bold->dump(); print "\n"; #The last "nested once" is actually not nested at all, as can be seen +from the indenting. #And the nested twice isn't quite right either. # #<html> @0 (IMPLICIT) # <head> @0.0 (IMPLICIT) # <body> @0.1 (IMPLICIT) # <b> @0.1.0 # <h2> @0.1.0.0 # " Nested once. " # <b> @0.1.0.0.1 # <h2> @0.1.0.1 # "Nested twice." # " Nested once. " my $html_font = <<END; <b><font color="red"> Nested once. <b><font color="red"><h2>Nested twice.</font></b> Nested once. </font> </b> END my $tree_font = HTML::TreeBuilder->new_from_content($html_font); print "Font:\n\n"; $tree_font->dump(); print "\n"; #Everything is nested like in the original. #<html> @0 (IMPLICIT) # <head> @0.0 (IMPLICIT) # <body> @0.1 (IMPLICIT) # <b> @0.1.0 # <font color="red"> @0.1.0.0 # " Nested once. " # <b> @0.1.0.0.1 # <font color="red"> @0.1.0.0.1.0 # <h2> @0.1.0.0.1.0.0 # "Nested twice." # " Nested once. " # " "

In reply to HTML::TreeBuilder, nesting with header vs font, different behavior by tphyahoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.