James.Q.L wrote:
>
> I am trying to add html tag to an existing html file using HTML::TreeBuiler.
>
> the problem is that the added tags isn't encoded after the output. everything els is
> fine.
>
> ###########
> my $tree = HTML::TreeBuilder->new;
> $tree->no_space_compacting(1);
> $tree->store_comments(1);
> $tree->parse_file("f.html");
>
> my $ele;
> my $tag = '<tr><td>TEST</td></tr>';
>
> for (@{ $tree->extract_links('a', 'href') }) {
> my($link, $element, $attr, $tag) = @$_;
> if ($link=~ m{/mylink\.htm}) {
> $ele = $element;
> last;
> }
> }
> $ele = $ele->parent(); # td
> $ele = $ele->parent(); # tr
> my $r = $ele->postinsert($tag);
>
> open F,">f.html";
> print F $tree->as_HTML('<>&',' ',{});
> close F;
> $tree->delete;
>
> the output in html turn out to be
>
> <tr><td><b>TEST</b></td></tr>
>
>
The 'postinsert' method expects an HTML::Element object to add
into the tree, not simple HTML text. The easiest way to build
what you want here is probably with the 'new_from_lol' method
like this.
my $tag = HTML::Element->new_from_lol (['tr', ['td', 'TEST']])
HTH,
Rob