Subject: | Method "as_HTML()" does not format new() elements. |
Where-as method "as_HTML()" will format a tree that was originally created via the method "parse_content()"; it will not format elements created via the method "new()". The attached test script demonstrates that the origin of the element, "parse_content()" versus "new()", is the deciding factor regarding whether the element will be formatted.
I am on CygWin, Perl 5.14, with HTML::Tree 5.03.
Subject: | test-as-html.pl |
#!/usr/bin/perl
# Script: test-as-html.pl
# Started by Alexander Danel on 2014-March-7
# E-Mail: alexander.danel@gmail.com
# BitCard user name: AL_X
# This script demonstrates a peculiarity with "as_HTML()".
# The first "print" statement shows that a tree created with "parse_content()"
# then formatted with "as_HTML()" is nicely formatted.
# The second "print" statement shows that elements created with "new()"
# and inserted into the hierarchy do not get formatted.
#
# Note that in the middle of the clump of elements created with "new()",
# I have pushed the original "Second paragraph" and "Third paragraph".
# While most of the clump is not formatted, "Second" and "Third" are formatted;
# this is compelling evidence that origin, not proximity, is the deciding
# factor.
# Elements originating from "parse_content()" get formatted,
# elements originationf from "new()" do not get formatted.
#
# Clumps of unformatted HTML become a problem when the clump becomes so large
# that it hampers editing and even viewing in a small terminal window,
# for example, a 24x80 terminal.
# $ perl --version
#
# This is perl 5, version 14, subversion 2 (v5.14.2) built for cygwin-thread-multi-64int
# (with 7 registered patches, see perl -V for more detail)
# $ ls -d -1 .cpan/build/HTML-Tree*
# .cpan/build/HTML-Tree-5.03-LAT5Ju
# .cpan/build/HTML-Tree-5.03-LAT5Ju.yml
# Here are results; can you reproduce them on your system?
my $resultsOfPrintStatementsLookLikeThis = <<"END_HERE";
<html>
<head>
</head>
<body>
<p>First paragraph here</p>
<div>
<p>Second paragraph here</p>
<p>Third paragraph here</p>
</div>
<p>Fourth paragraph here</p>
</body>
</html>
#==========================
<html>
<head>
</head>
<body>
<p>First paragraph here</p>
<div><span>Contents of span001</span><span>Contents of span002</span><div>
<p>Second paragraph here</p>
<p>Third paragraph here</p>
</div><span>Contents of span003</span><span>Contents of span004</span></div>
<p>Fourth paragraph here</p>
</body>
</html>
#==========================
END_HERE
use strict; use warnings;
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new; # empty tree
$tree->ignore_text(0);
$tree->parse_content(<main::DATA>);
$tree->elementify();
print $tree->as_HTML(undef,' ',{}),"\n#==========================\n";
my $originalDiv = $tree->find_by_tag_name('div');
my @originalContent = $originalDiv->detach_content();
my $exteriorDiv = HTML::Element->new('div');
$originalDiv->replace_with($exteriorDiv);
$originalDiv->destroy();
my $span001 = HTML::Element->new('span');
$span001->push_content('Contents of span001');
my $span002 = HTML::Element->new('span');
$span002->push_content('Contents of span002');
my $interiorDiv = HTML::Element->new('div');
$interiorDiv->push_content(@originalContent); # paragraphs "Second", "Third"
my $span003 = HTML::Element->new('span');
$span003->push_content('Contents of span003');
my $span004 = HTML::Element->new('span');
$span004->push_content('Contents of span004');
$exteriorDiv->push_content($span001,$span002,$interiorDiv,$span003,$span004);
print $tree->as_HTML(undef,' ',{}),"\n#==========================\n";
exit 0;
__DATA__
<html>
<head> </head>
<body> <p>First paragraph here</p>
<div> <p>Second paragraph here</p> <p>Third paragraph here</p> </div>
<p>Fourth paragraph here</p> </body>
</html>