Skip to content

Commit

Permalink
deprecated Mojo::DOM::siblings
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 9, 2014
1 parent 6030b08 commit f308b6d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
5 changes: 3 additions & 2 deletions Changes
@@ -1,10 +1,11 @@

5.69 2014-12-09
- Deprecated Mojo::DOM::siblings.
- Added following, following_siblings, preceding and preceding_siblings
methods to Mojo::DOM.
- Removed deprecated emit_safe method from Mojo::EventEmitter.
- Removed deprecated render_static method from Mojolicious::Controller.
- Removed deprecated has_conditions method from Mojolicious::Routes::Route.
- Added following, following_siblings, preceding and preceding_siblings
methods to Mojo::DOM.
- Improved Mojo::DOM::HTML performance slightly.
- Fixed parent combinator bug in Mojo::DOM::CSS.
- Fixed whitespace bug in Mojo::DOM::HTML.
Expand Down
25 changes: 9 additions & 16 deletions lib/Mojo/DOM.pm
Expand Up @@ -162,7 +162,12 @@ sub root {
return _build($self, $tree, $self->xml);
}

sub siblings { _select($_[0]->_collect(@{_siblings($_[0], 1, 1)}), $_[1]) }
# DEPRECATED in Tiger Face!
sub siblings {
deprecated 'Mojo::DOM::siblings is DEPRECATED';
my $siblings = _siblings($_[0], 1);
return _select($_[0]->_collect(@{$siblings->[0]}, @{$siblings->[1]}), $_[1]);
}

sub strip {
my $self = shift;
Expand Down Expand Up @@ -332,9 +337,9 @@ sub _select {
}

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

return [] unless my $parent = $self->parent;
return [[], []] unless my $parent = $self->parent;

my $tree = $self->tree;
my (@before, @after, $match);
Expand All @@ -344,7 +349,7 @@ sub _siblings {
$match ? push @after, $node : push @before, $node;
}

return $all ? [@before, @after] : [\@before, \@after];
return [\@before, \@after];
}

sub _start { $_[0][0] eq 'root' ? 1 : 4 }
Expand Down Expand Up @@ -850,18 +855,6 @@ Replace this node with HTML/XML fragment and return L</"parent">.
Return L<Mojo::DOM> object for root node.
=head2 siblings
my $collection = $dom->siblings;
my $collection = $dom->siblings('div > p');
Find all sibling elements of this node matching the CSS selector and return a
L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# List types of sibling elements
say $dom->siblings->map('type')->join("\n");
=head2 strip
my $parent = $dom->strip;
Expand Down
16 changes: 4 additions & 12 deletions t/mojo/dom.t
Expand Up @@ -220,7 +220,7 @@ is "$dom", '<script>a<b>c</b>1<b>d</b></script>', 'right result';
is $dom->at('b')->contents->first->append('e')->content, 'c', 'right content';
is $dom->at('b')->contents->first->prepend('f')->node, 'text', 'right node';
is "$dom", '<script>a<b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('script')->contents->first->siblings->first->type, 'b',
is $dom->at('script')->contents->first->following->first->type, 'b',
'right type';
is $dom->at('script')->contents->first->next->content, 'fce', 'right content';
is $dom->at('script')->contents->first->previous, undef, 'no siblings';
Expand Down Expand Up @@ -326,14 +326,6 @@ is_deeply [$dom->at('p')->ancestors->map('type')->each],
[qw(div div div body html)], 'right results';
is_deeply [$dom->at('html')->ancestors->each], [], 'no results';
is_deeply [$dom->ancestors->each], [], 'no results';
is_deeply [$dom->siblings->each], [], 'no results';
ok $dom->at('form')->siblings->[0]->match('#header'), 'right sibling';
ok $dom->at('form')->siblings->[1]->match('#content'), 'right sibling';
is $dom->at('form')->siblings('#content')->first->text, 'More stuff',
'right text';
is_deeply [$dom->at('form')->siblings('#nothing')->each], [], 'no results';
is_deeply [$dom->at('#header')->siblings->map('type')->each], [qw(form div)],
'right results';

# Script tag
$dom = Mojo::DOM->new(<<EOF);
Expand Down Expand Up @@ -1551,10 +1543,10 @@ is $dom->find('tbody > tr > .gamma')->[0]->text, '', 'no text';
is $dom->find('tbody > tr > .gamma > a')->[0]->text, 'Gamma', 'right text';
is $dom->find('tbody > tr > .alpha')->[1]->text, 'Alpha Two', 'right text';
is $dom->find('tbody > tr > .gamma > a')->[1]->text, 'Gamma Two', 'right text';
my @siblings
= $dom->find('tr > td:nth-child(1)')->map(siblings => ':nth-child(even)')
my @following
= $dom->find('tr > td:nth-child(1)')->map(following => ':nth-child(even)')
->flatten->map('all_text')->each;
is_deeply \@siblings, ['Beta', 'Delta', 'Beta Two', 'Delta Two'],
is_deeply \@following, ['Beta', 'Delta', 'Beta Two', 'Delta Two'],
'right results';

# Real world list
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -8,6 +8,6 @@ plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Tiger Face!
my @tiger = qw(decode encode error new pluck val);
my @tiger = qw(decode encode error new pluck siblings val);

all_pod_coverage_ok({also_private => [@tiger]});

0 comments on commit f308b6d

Please sign in to comment.