Skip to content

Commit

Permalink
added more tests and changes entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 18, 2010
1 parent 5a230e8 commit 99a345d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
0.999930 2010-08-17 00:00:00
- Added EXPERIMENTAL support for --mode and --home options to all
Mojolicious commands.
- Added while and until methods for Mojo::DOM collections. (vti)
- Fixed a bug where Mojo::IOLoop connections could be closed too
early.

Expand Down
23 changes: 10 additions & 13 deletions lib/Mojo/DOM.pm
Expand Up @@ -843,18 +843,14 @@ sub _iterate {
# Shortcut
return @$self unless $cb;

# Iterate
# Iterator
my $i = 1;

# Iterate until condition is true
if (defined $cond) {
!!$_->$cb($i++) == $cond && last for @$self;
}
if (defined $cond) { !!$_->$cb($i++) == $cond && last for @$self }

# Iterate over all elements
else {
$_->$cb($i++) for @$self;
}
else { $_->$cb($i++) for @$self }

# Root
return unless my $start = $self->[0];
Expand Down Expand Up @@ -887,12 +883,13 @@ Mojo::DOM - Minimalistic XML DOM Parser With CSS3 Selectors
# Iterate
$dom->find('div[id]')->each(sub { print shift->text });
# Get the top 10 links
# Get the first 10 links
$dom->find('a[href]')
->while(sub { push @links, shift->attrs->{href} && @links < 10 });
->while(sub { print shift->attrs->{href} && pop() < 10 });
# Get the first link with a specific path
$dom->find('a[href]')->until(sub { shift->attrs->{href} =~ m/kraih/ });
# Get text for the first link containing a specific path
$dom->find('a[href]')
->until(sub { $_->attrs->{href} =~ m/kraih/ && print $_->text });
=head1 DESCRIPTION
Expand Down Expand Up @@ -1016,8 +1013,8 @@ Children of element.
Find elements with CSS3 selectors.
$dom->find('div')->each(sub { print shift->text });
$dom->find('div')->while(sub { shift->text ne 'foo' });
$dom->find('div')->until(sub { shift->text eq 'foo' });
$dom->find('div')->while(sub { print $_->text && $_->text =~ /foo/ });
$dom->find('div')->until(sub { $_->text =~ /foo/ && print $_->text });
=head2 C<name>
Expand Down
11 changes: 10 additions & 1 deletion t/mojo/dom.t
Expand Up @@ -5,7 +5,7 @@ use warnings;

use utf8;

use Test::More tests => 157;
use Test::More tests => 160;

# Homer gave me a kidney: it wasn't his, I didn't need it,
# and it came postage due- but I appreciated the gesture!
Expand All @@ -20,11 +20,20 @@ my @div;
$dom->find('div[id]')->each(sub { push @div, shift->text });
is_deeply(\@div, [qw/A B/], 'found all div elements with id');
@div = ();
$dom->find('div[id]')->each(sub { push @div, $_->text });
is_deeply(\@div, [qw/A B/], 'found all div elements with id');
@div = ();
$dom->find('div[id]')->until(sub { push @div, shift->text; @div == 1 });
is_deeply(\@div, [qw/A/], 'found first div elements with id');
@div = ();
$dom->find('div[id]')->until(sub { pop == 1 && push @div, $_->text });
is_deeply(\@div, [qw/A/], 'found first div elements with id');
@div = ();
$dom->find('div[id]')->while(sub { push @div, shift->text; @div < 1 });
is_deeply(\@div, [qw/A/], 'found first div elements with id');
@div = ();
$dom->find('div[id]')->while(sub { pop() < 2 && push @div, $_->text });
is_deeply(\@div, [qw/A/], 'found first div elements with id');

# Simple nesting (tree structure)
$dom->parse(<<EOF);
Expand Down

0 comments on commit 99a345d

Please sign in to comment.