Extension to libwww 5.05
Jyrgen Nyrgaard (jnp@www.ifs.dk)
Sat, 25 Jan 97 16:22:15 +0100
The Element library does not have a method for inserting (it appears)
to insert an element before another one in a list.
The patch allows this be introducing insert_element_before.
For example, if I want to insert a paragraph before the first paragraphs
on the <BODY>-contents list, I do, assuming $node->tag is BODY:
my ($first)=($node->content);
for (@$first){
if (ref $_ ) {
$_->insert_element_before(@headerL);
last FindRef;
}
}
The name is not the best and it has not been tested thoroughly yet. It does
work in the cases I need.
Regards,
/jrgen nrgaard | e-mail: jnp@ifs.dk
Innova Financial Solutions | Phone: +45 3917 9797
Copenhagen Science Park | Fax: +45 3927 5521
Copenhagen | Denmark
*** Element.pm-ORIG Thu Jan 9 17:32:58 1997
--- Element.pm Sat Jan 18 17:17:06 1997
***************
*** 387,393 ****
--- 387,445 ----
$self;
}
+ =head2 $h->insert_element_before($element,...)
+ Inserts the element before the current content element and returns the original element. Adjusts parent link of inserted elements.
+ The content should be a list of references to HTML::Element object.
+
+ =cut
+
+ sub insert_element_before
+ {
+ my $self = shift;
+ Carp::croak("No content") unless exists $self->{'_parent'}->{'_content'};
+ my $content = $self->{'_parent'}->{'_content'};
+
+ # printf STDERR "I am: $self ($e) [%d]\n", $#{$content};
+ # print STDERR "In parent:\n";
+ my ($i, $j);
+ MoveElm:
+ for ($i=0;$i<=$#{$content};$i++) {
+ # printf STDERR " +$i (%s)\n", $content->[$i];
+ if ( $content->[$i] == $self ) {
+ my ($howmuch)=(0);
+ for (@_) {
+ if (ref $_) { $howmuch++; }
+ }
+ # print STDERR " Me ($i), moving $howmuch ($#_)!!\n";
+ # Move
+ for ($j=$#{$content};$j>=$i;$j--) {
+ $content->[$j+$howmuch]=$content->[$j];
+ if ( $self == $content->[$j]) {
+ for (@_) {
+ if ( ref $_ ) {
+ $_->{'_parent'} = $self->{'_parent'};
+ $content->[$j++]=$_;
+ } else {
+ if ( ref $content->[$j] ) {
+ $content->[$j++]=$_;
+ } else {
+ $content->[$j].=$_;
+ }
+ }
+
+ }
+ last MoveElm;
+ }
+ }
+ }
+ }
+ Carp::croak("Not in content of parent") unless ($i <= $#{$content} );
+
+ # printf STDERR "New (%d)\n", $#{$content};
+ # for (@$content) { print STDERR " $_\n"; }
+ $self;
+ }
=head2 $h->delete_content()