I want to get rid of white space between the
<h1> and
<h2> tags, and am trying to use HTML::Element->splice_content to do it. I guess I'm doing it wrong. Can someone tell me why?
use strict;
use warnings;
use HTML::TreeBuilder;
my $html = '<h1>Blah</h1>
<p> <br>
<h2>Blah</h2>';
my $element_root = HTML::TreeBuilder->new_from_content($html);
$element_root->dump;
print "\n";
$element_root->splice_content(1,1);
$element_root->dump;
Outputs:
<html> @0 (IMPLICIT)
<head> @0.0 (IMPLICIT)
<body> @0.1 (IMPLICIT)
<h1> @0.1.0
"Blah"
" á"
<p> @0.1.2
"á"
<br> @0.1.2.1
<h2> @0.1.3
"Blah"
<html> @0 (IMPLICIT)
<head> @0.0 (IMPLICIT)
UPDATE: Thanks to everyone below. Practically every suggestion was useful. In the end, I realized that the main mistake I was making was doing the splice from the root node, rather than the body. I'm still not 100% satisfied, but at least now the splice works. The code I'm using now is:
use strict;
use warnings;
use HTML::Treebuilder;
my $html = '<h1>Blah</h1>
<p> <br>
<h2>Blah</h2>';
my $element_root = HTML::TreeBuilder->new_from_content($html);
my $body = $element_root->look_down( _tag => "body");
$body->dump;
print "\n";
$body->splice_content(1,2);
$body->dump;