Skip to content

Commit

Permalink
deprecated Mojo::DOM::text_after and Mojo::DOM::text_before in favor …
Browse files Browse the repository at this point in the history
…of Mojo::DOM::contents
  • Loading branch information
kraih committed Feb 5, 2014
1 parent ca54968 commit af606a5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 103 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

4.77 2014-02-05
- Deprecated Mojo::DOM::text_after and Mojo::DOM::text_before in favor of
Mojo::DOM::contents.
- Added wrap_content method to Mojo::DOM.
- Improved wrap method in Mojo::DOM to allow wrapping of the root node.

Expand Down
85 changes: 36 additions & 49 deletions lib/Mojo/DOM.pm
Expand Up @@ -14,7 +14,7 @@ use Mojo::Collection;
use Mojo::DOM::CSS;
use Mojo::DOM::HTML;
use Mojo::DOM::Node;
use Mojo::Util 'squish';
use Mojo::Util qw(deprecated squish);
use Scalar::Util qw(blessed weaken);

sub AUTOLOAD {
Expand Down Expand Up @@ -155,8 +155,41 @@ sub tap { shift->Mojo::Base::tap(@_) }

sub text { shift->_all_text(0, @_) }

sub text_after { shift->_sibling_text(1, @_) }
sub text_before { shift->_sibling_text(0, @_) }
# DEPRECATED in Top Hat!
sub text_after {
deprecated
'Mojo::DOM::text_after is DEPRECATED in favor of Mojo::DOM::contents';
my ($self, $trim) = @_;

return '' if (my $tree = $self->tree)->[0] eq 'root';

my (@nodes, $started);
for my $n (_nodes($tree->[3])) {
++$started and next if $n eq $tree;
next unless $started;
last if $n->[0] eq 'tag';
push @nodes, $n;
}

return _text(\@nodes, 0, _trim($tree->[3], $trim));
}

# DEPRECATED in Top Hat!
sub text_before {
deprecated
'Mojo::DOM::text_before is DEPRECATED in favor of Mojo::DOM::contents';
my ($self, $trim) = @_;

return '' if (my $tree = $self->tree)->[0] eq 'root';

my @nodes;
for my $n (_nodes($tree->[3])) {
last if $n eq $tree;
@nodes = $n->[0] eq 'tag' ? () : (@nodes, $n);
}

return _text(\@nodes, 0, _trim($tree->[3], $trim));
}

sub to_xml { shift->[0]->render }

Expand Down Expand Up @@ -276,24 +309,6 @@ sub _select {
return $collection->new(grep { $_->match($selector) } @$collection);
}

sub _sibling_text {
my ($self, $after, $trim) = @_;

return '' if (my $tree = $self->tree)->[0] eq 'root';

my (@before, @after, $match);
for my $n (_nodes($tree->[3])) {
if ($after && $match) {
last if $n->[0] eq 'tag';
push @after, $n;
}
$match++ if $n eq $tree;
@before = $n->[0] eq 'tag' ? () : (@before, $n) unless $after || $match;
}

return _text($after ? \@after : \@before, 0, _trim($tree->[3], $trim));
}

sub _siblings {
my ($self, $merge) = @_;

Expand Down Expand Up @@ -753,34 +768,6 @@ smart whitespace trimming is enabled by default.
# "foo\nbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text(0);
=head2 text_after
my $trimmed = $dom->text_after;
my $untrimmed = $dom->text_after(0);
Extract text content immediately following this element, smart whitespace
trimming is enabled by default.
# "baz"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after;
# "baz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after(0);
=head2 text_before
my $trimmed = $dom->text_before;
my $untrimmed = $dom->text_before(0);
Extract text content immediately preceding this element, smart whitespace
trimming is enabled by default.
# "foo"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before;
# "foo\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before(0);
=head2 to_xml
my $str = $dom->to_xml;
Expand Down
19 changes: 8 additions & 11 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -784,20 +784,17 @@ web application.
# Extract headings
$tx->res->dom('h1, h2, h3')->each(sub { say 'Heading: ', shift->all_text });

# Visit all elements recursively to extract more than just text
for my $e ($tx->res->dom('*')->each) {
# Visit all nodes recursively to extract more than just text
for my $n ($tx->res->dom->all_contents->each) {

# Text before this element
print $e->text_before(0);
# Text or CDATA node
print $n->content if $n->node eq 'text' || $n->node eq 'cdata';

# Also include alternate text for images
print $e->{alt} if $e->type eq 'img';

# Text for elements without children
print $e->text(0) unless $e->children->size;
# Make sure we have a tag node
next unless $n->node eq 'tag';

# Text after last element
print $e->text_after(0) unless $e->next;
# Also include alternate text for images
print $n->{alt} if $n->type eq 'img';
}

For a full list of available CSS selectors see L<Mojo::DOM::CSS/"SELECTORS">.
Expand Down
42 changes: 0 additions & 42 deletions t/mojo/dom.t
Expand Up @@ -2074,8 +2074,6 @@ is $dom->text(0), "\n", 'right text';
is $dom->all_text, "looks like\n it\n really\n works", 'right text';
is $dom->all_text(0), "\n looks\n like\n it\n really\n \n works\n\n",
'right text';
is $dom->text_before, '', 'no text';
is $dom->text_after, '', 'no text';
is $dom->div->text, 'looks works', 'right text';
is $dom->div->text(0), "\n looks\n \n works\n", 'right text';
is $dom->div->all_text, "looks like\n it\n really\n works", 'right text';
Expand All @@ -2089,46 +2087,6 @@ is $dom->div->pre->code->text, "like\n it\n really", 'right text';
is $dom->div->pre->code->text(0), "like\n it\n really", 'right text';
is $dom->div->pre->code->all_text, "like\n it\n really", 'right text';
is $dom->div->pre->code->all_text(0), "like\n it\n really", 'right text';
is $dom->div->pre->text_before, 'looks', 'right text';
is $dom->div->pre->text_after, 'works', 'right text';

# Text siblings
$dom = Mojo::DOM->new(<<EOF);
ok
<div>
looks<p>like</p>
thi<![CDATA[s]]>
<p>might</p><p>really</p>
<p>
just
</p>work
</div>
wow
EOF
is $dom->text_before, '', 'no text';
is $dom->text_before(0), '', 'no text';
is $dom->div->text_before, 'ok', 'right text';
is $dom->div->text_before(0), "ok\n", 'right text';
is $dom->div->p->[0]->text_before, 'looks', 'right text';
is $dom->div->p->[0]->text_before(0), "\n looks", 'right text';
is $dom->div->p->[1]->text_before, 'thi s', 'right text';
is $dom->div->p->[1]->text_before(0), "\n thi s\n ", 'right text';
is $dom->div->p->[2]->text_before, '', 'no text';
is $dom->div->p->[2]->text_before(0), '', 'no text';
is $dom->div->p->[3]->text_before, '', 'no text';
is $dom->div->p->[3]->text_before(0), "\n ", 'right text';
is $dom->text_after, '', 'no text';
is $dom->text_after(0), '', 'no text';
is $dom->div->text_after, 'wow', 'right text';
is $dom->div->text_after(0), "\nwow\n", 'right text';
is $dom->div->p->[0]->text_after, 'thi s', 'right text';
is $dom->div->p->[0]->text_after(0), "\n thi s\n ", 'right text';
is $dom->div->p->[1]->text_after, '', 'no text';
is $dom->div->p->[1]->text_after(0), '', 'no text';
is $dom->div->p->[2]->text_after, '', 'no text';
is $dom->div->p->[2]->text_after(0), "\n ", 'right text';
is $dom->div->p->[3]->text_after, 'work', 'right text';
is $dom->div->p->[3]->text_after(0), "work\n", 'right text';

# PoCo example with whitespace sensitive text
$dom = Mojo::DOM->new(<<EOF);
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -8,7 +8,7 @@ plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Top Hat!
my @tophat = qw(secret to_rel);
my @tophat = qw(secret text_after text_before to_rel);

# False positive constants
all_pod_coverage_ok({also_private => [qw(IPV6 TLS), @tophat]});

0 comments on commit af606a5

Please sign in to comment.