The code below should round trip a small HTML document. However, the output version has moved the declaration and comment lines from the start of the file to after the body. Is this expected behaviour (if so, why?) or a bug in TreeBuilder?

use strict; use warnings; use HTML::TreeBuilder; my $data = do {local $/ = ""; <DATA>}; my $tree = HTML::TreeBuilder->new; $tree->store_comments(1); $tree->store_declarations(1); $tree->parse ($data); $tree->eof (); print $tree->as_HTML(undef, ' '); __DATA__ <!DOCTYPE html PUBLIC> <!-- saved from url --> <html lang="en"> <head> </head> <body> </body> </html>

The output generated is:

<html lang="en"> <head> </head> <body> </body><!DOCTYPE html PUBLIC><!-- saved from url --> </html>
Update:

I'm using HTML::TreeBuilder 1.01

Replacing the print line in the code above with the following code works around the problem (this would require something a little smarter in "real" code):

my @prefix = @{$tree->{_content}}[2..3]; @{$tree->{_content}} = @{$tree->{_content}}[0..1]; print $prefix[0]->as_HTML(undef, ' '); print $prefix[1]->as_HTML(undef, ' ');

Perl is Huffman encoded by design.

In reply to HTML::TreeBuilder bug or feature? 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.