I had a need to compare two fragments of HTML to see if they were equivalent.

This snippet builds two HTML::TreeBuilder representations of the fragments, then recursively compares the contents of the fragments.

To use the snippet call cmpHtml passing the two fragments as strings:

print cmpHtml( '<p><font foo="bar" bar="1">bar 1</font></p>', '<p><font bar="2" foo="bar">bar 1</font></p>' );

or if you already have two HTML::Elements that you want to compare you can:

print cmpHtmlElt ($elt1, $elt2);
sub cmpHtml { my ($html1, $html2) = @_; my $root1 = HTML::TreeBuilder->new; my $root2 = HTML::TreeBuilder->new; $root1->parse_content ($html1); $root1->elementify (); $root2->parse_content ($html2); $root2->elementify (); return cmpHtmlElt ($root1, $root2); } sub cmpHtmlElt { my ($elt1, $elt2) = @_; my $cmp = defined $elt1 cmp defined $elt2; return $cmp if $cmp; return 0 unless defined $elt1; $cmp = ref $elt1 cmp ref $elt2; return $cmp if $cmp; return $elt1 cmp $elt2 unless ref $elt1; $cmp = $elt1->tag () cmp $elt2->tag (); return $cmp if $cmp; my %attribs1 = $elt1->all_attr (); my %attribs2 = $elt2->all_attr (); $cmp = keys %attribs1 <=> keys %attribs2; return $cmp if $cmp; for my $key (keys %attribs1) { return 1 unless exists $attribs2{$key}; next if $key =~ /^_/; $cmp = $attribs1{$key} cmp $attribs2{$key}; return $cmp if $cmp; } my @children1 = $elt1->content_list (); my @children2 = $elt2->content_list (); $cmp = @children1 <=> @children2; return $cmp if $cmp; for my $index (0 .. $#children1) { $cmp = cmpHtmlElt ($children1[$index], $children2[$index]); return $cmp if $cmp; } }

In reply to cmp two HTML fragments by GrandFather

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.