GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
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, ' ');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML::TreeBuilder bug or feature?
by gam3 (Curate) on Sep 07, 2005 at 03:23 UTC | |
|
Re: HTML::TreeBuilder bug or feature?
by pg (Canon) on Sep 07, 2005 at 03:58 UTC | |
by GrandFather (Saint) on Sep 07, 2005 at 04:20 UTC | |
by pg (Canon) on Sep 07, 2005 at 05:12 UTC | |
by GrandFather (Saint) on Sep 07, 2005 at 05:17 UTC |