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:
Update:<html lang="en"> <head> </head> <body> </body><!DOCTYPE html PUBLIC><!-- saved from url --> </html>
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, ' ');
In reply to HTML::TreeBuilder bug or feature? by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |