Greetings.

Perhaps this is bug with HTML Tidy. One of my scripts was subjected to some HTML today which passed through HTML Tidy but nonetheless crashed HTML::TreeBuilder::XPath. Below is a stripped down sample. The following Perl script produces the error message "Can't locate object method "as_XML_indented" via package " trololo " (perhaps you forgot to load " trololo "?) at ./script.pl line 12." and does not proceed through the rest of the script.

#!/usr/bin/perl use HTML::TreeBuilder::XPath; use strict; use warnings; my $tree = HTML::TreeBuilder::XPath->new_from_file(\*DATA); for my $body ($tree->findnodes('//body')) { for my $element ($body->detach_content) { print $element->as_XML_indented; } } print "\n"; print "OK\n"; exit(0); __DATA__ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content= "HTML Tidy for HTML5 for Linux version 5.6.0" /> <title></title> </head> <body> <p>foo</p> <p>bar</p> trololo </body> </html>

Since the HTML seems to be valid, having just passed through HTML Tidy, I would have expected as_XML_indented to have just plowed through it, either rendering it as XML or at least not stopping. A work-around has been to wrap it in an eval,

#!/usr/bin/perl use HTML::TreeBuilder::XPath; use strict; use warnings; my $tree = HTML::TreeBuilder::XPath->new_from_file(\*DATA); for my $body ($tree->findnodes('//body')) { for my $element ($body->detach_content) { eval { print $element->as_XML_indented; }; if ($@) { print STDERR qq(\n),$@,qq(\n); print STDERR qq(Failed HTML.\n); } } print "\n"; print "OK\n"; exit(0); __DATA__ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content= "HTML Tidy for HTML5 for Linux version 5.6.0" /> <title></title> </head> <body> <p>foo</p> <p>bar</p> trololo </body> </html>

I'm not sure how to interpret the HTML5 spec. However, the HTML4 spec seems to indicate that the loose text ought to have been wrapped in a block element of some kind.

So if I may tap your collective wisdom,

  1. Is this a bug with HTML::TreeBuilder::XPath or with HTML Tidy?
  2. For error trapping:
    1. Is there a better way to do eval?
    2. Or should try / catch be used?

General comments and advice also welcome.


In reply to Seemingly Valid HTML which crashes HTML::TreeBuilder::XPath by mldvx4

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.