Re: Deleting images using libwww-perl

WWW projekt (wwwproj@dna.lth.se)
Mon, 12 Aug 1996 09:42:24 +0200


Andreas Koenig wrote:
> 
>  www> Note the $ele->tag("noop"); line, he sets the tag to be 'noop' 
>  www> and it will stay in the tree, but as an unknown tag NOOP.
> 
> The NOOP was intentional, not an unfortunate effect. $ele->delete
> works just fine for real deletes. The NOOP is there, so I do not
> censor what the people wrote, but prevent them from breaking the
> guestbook page. $self->{HTML_OK} allows e.g. UL, OL, A, LI, and some
> such. If they come with IMG, they get what they deserve :-) But I
> still can see, what they tried to do.

Ok, I can see that now. I will explain why I misunderstood the code
later.

>  www> I still think that a 'remove' method should me implemented that 
>  www> really removes the element from the tree, but it might be a bit 
>  www> tricky because of the use of arrays instead of linked lists in 
>  www> the structure.
> 
> Sorry, can't follow you. Why does ->delete() not do what you want?

Because the tag is not deleted from the tree:
~/Stefan/tmp>perl -w -MHTML::TreeBuilder
$html = '<I> Italic </I> <B> Bold </B> <HR>';
 
$p = new HTML::TreeBuilder;
$p->parse($html);
 
 
$p->traverse(sub {
    my ($ele, $flag, $depth) = @_;
    if ($ele->tag eq 'b') {
        $ele->delete();
    }
    return 1;
}, 1);
 
print "---------\n", $p->as_HTML, "--------\n";      
 
$p->traverse(sub {
    my ($ele, $flag, $depth) = @_;
    return unless $flag;
    print $ele->tag, " printing parent tag: ";
    if ($ele->tag ne 'html') {
        print $ele->parent->tag;
    }
    print "\n";
    return 1;
}, 1);
---------

Italic


-------- html printing parent tag: body printing parent tag: html p printing parent tag: body i printing parent tag: p Can't call method "tag" without a package or object reference at - line 21. b printing parent tag: ~/Stefan/tmp> Look at the line printed by ->as_HTML: The tag is still there, but it's content is gone. Then Have a look at the error, it's printed when trying to print parent of the 'b' tag, that I really don't want to be a part of the tree anymore. Maybe I have misunderstood you, but was this not the way you wanted to delete elements? Have I done anything wrong? > > I append my (unfortunately not really complete) answer to your recent > posting. > >>> Today there is no way of inserting a new element in the tree, > >>> i.e. inserting an element into a certain position in the contents > > ^^^^^^^^^^^^^^^^ > >>> of another element. > >> > > It's becoming a bit lengthy, but it's feasible. _Where_ exactly did > you have in mind? I insert for you something in three places. The > first requires to know a bit of the source, that's kind of > hackery. The second is fine, the third needs a flag that I can set > myself, but I think, that's no bad style. The third style was really what I wanted. I'm still not convinced that it is a general method to solve the problem, though, so I challange you to solve this problem: Insert

Bold paragraph inbetween the hr and br tags in this tree:



Can you do this in a way that excludes the risk of errors and that does not depend on the type of tags that are inserted or surround the inserted tag? --- Stefan Eriksson, Lund university, Sweden wwwproj@dna.lth.se, dat93ser@ludat.lth.se