Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/HTML/Element.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2132,10 +2132,11 @@ sub content_as_XML

$s = $h->as_text();
$s = $h->as_text(skip_dels => 1);
$s - $h->as_text(delimiter => "\n");

Returns a string consisting of only the text parts of the element's
descendants. Any whitespace inside the element is included unchanged,
but whitespace not in the tree is never added. But remember that
but whitespace not in the tree is never added unless a delimiter is included. But remember that
whitespace may be ignored or compacted by HTML::TreeBuilder during
parsing (depending on the value of the C<ignore_ignorable_whitespace>
and C<no_space_compacting> attributes). Also, since whitespace is
Expand All @@ -2144,7 +2145,8 @@ never added during parsing,
HTML::TreeBuilder->new_from_content("<p>a</p><p>b</p>")
->as_text;

returns C<"ab">, not C<"a b"> or C<"a\nb">.
returns C<"ab">, not C<"a b"> or C<"a\nb">,
unless those characters are specified as a delimiter.

Text under C<< <script> >> or C<< <style> >> elements is never
included in what's returned. If C<skip_dels> is true, then text
Expand Down Expand Up @@ -2179,12 +2181,14 @@ sub as_text {
my (@pile) = ($this);
my $tag;
my $text = '';
my $delimiter = $options{delimiter} || undef;
while (@pile) {
if ( !defined( $pile[0] ) ) { # undef!
shift @pile; # how did that get in here?
}
elsif ( !ref( $pile[0] ) ) { # text bit! save it!
$text .= shift @pile;
$text .= $delimiter if defined ($delimiter);
}
else { # it's a ref -- traverse under it
unshift @pile, @{ $this->{'_content'} || $nillio }
Expand All @@ -2209,6 +2213,8 @@ sub as_trimmed_text {
return $text;
}



sub as_text_trimmed { shift->as_trimmed_text(@_) } # alias, because I forget

=method-dump as_XML
Expand Down