=head1 STRUCTURE-MODIFYING METHODS
These methods are provided for modifying the content of trees
by adding or changing nodes as parents or children of other nodes.
=head2 $h->push_content($element_or_text, ...)
Adds the specified items to the I<end> of the content list of the
element C<$h>. The items of content to be added should each be either a
text segment (a string), an HTML::Element object, or an arrayref.
Arrayrefs are fed thru C<< $h->new_from_lol(that_arrayref) >> to
convert them into elements, before being added to the content
list of C<$h>. This means you can say things concise things like:
$body->push_content(
['br'],
['ul',
map ['li', $_], qw(Peaches Apples Pears Mangos)
]
);
See C<new_from_lol> method's documentation, far below, for more
explanation.
The push_content method will try to consolidate adjacent text segments
while adding to the content list. That's to say, if $h's content_list is
('foo bar ', $some_node, 'baz!')
and you call
$h->push_content('quack?');
then the resulting content list will be this:
('foo bar ', $some_node, 'baz!quack?')
and not this:
('foo bar ', $some_node, 'baz!', 'quack?')
If that latter is what you want, you'll have to override the
feature of consolidating text by using splice_content,
as in:
$h->splice_content(scalar($h->content_list),0,'quack?');
Similarly, if you wanted to add 'Skronk' to the beginning of
the content list, calling this:
$h->unshift_content('Skronk');
then the resulting content list will be this:
('Skronkfoo bar ', $some_node, 'baz!')
and not this:
('Skronk', 'foo bar ', $some_node, 'baz!')
What you'd to do get the latter is:
$h->splice_content(0,0,'Skronk');
=cut
sub push_content {
my $self = shift;
return $self unless @_;
my $content = ($self->{'_content'} ||= []);
for (@_) {
if (ref($_) eq 'ARRAY') {
# magically call new_from_lol
push @$content, $self->new_from_lol($_);
$content->[-1]->{'_parent'} = $self;
}
elsif (ref($_)) { # insert an element
$_->detach if $_->{'_parent'};
$_->{'_parent'} = $self;
push(@$content, $_);
}
else { # insert text segment
if (@$content && !ref $content->[-1]) {
# last content element is also text segment -- append
$content->[-1] .= $_;
} else {
push(@$content, $_);
}
}
}
return $self;
}
=head2 $h->unshift_content($element_or_text, ...)
Just like C<push_content>, but adds to the I<beginning> of the $h
element's content list.
=8= |