Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix a few small selector bugs in Mojo::DOM::CSS
  • Loading branch information
kraih committed Nov 12, 2015
1 parent 5307d52 commit d0d09f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

6.31 2015-11-12
6.31 2015-11-13
- Fixed a few small selector bugs in Mojo::DOM::CSS.

6.30 2015-11-11
- Fixed bug in Mojolicious::Renderer where layouts could not be used with
Expand Down
15 changes: 7 additions & 8 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -125,14 +125,13 @@ sub _equation {
# "odd"
return [2, 1] if $equation =~ /^\s*odd\s*$/i;

# Equation
my $num = [1, 1];
return $num if $equation !~ /(?:(-?(?:\d+)?)?(n))?\s*\+?\s*(-?\s*\d+)?\s*$/i;
$num->[0] = defined($1) && $1 ne '' ? $1 : $2 ? 1 : 0;
$num->[0] = -1 if $num->[0] eq '-';
$num->[1] = $3 // 0;
$num->[1] =~ s/\s+//g;
return $num;
# "4", "+4" and "-4"
return [0, $1] if $equation =~ /^\s*((?:\+|-)?\d+)\s*$/;

# "4n", "+4n", "-4n", "4n+1" and "4n-1"
return [1, 1]
unless $equation =~ /^\s*((?:\+|-)?(?:\d+)?)?n\s*((?:\+|-)\s*\d+)?\s*$/i;
return [$1 eq '-' ? -1 : $1 eq '' ? 1 : $1, join('', split(' ', $2 // 0))];
}

sub _match {
Expand Down
7 changes: 5 additions & 2 deletions t/mojo/dom.t
Expand Up @@ -1027,15 +1027,18 @@ is_deeply \@li, [qw(C F)], 'found every third li elements';
$dom->find('li:Nth-Last-Child(3N)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(C F)], 'found every third li elements';
@li = ();
$dom->find('li:nth-child(3)')->each(sub { push @li, shift->text });
$dom->find('li:nth-child( 3 )')->each(sub { push @li, shift->text });
is_deeply \@li, ['C'], 'found third li element';
@li = ();
$dom->find('li:nth-last-child(3)')->each(sub { push @li, shift->text });
$dom->find('li:nth-last-child( +3 )')->each(sub { push @li, shift->text });
is_deeply \@li, ['F'], 'found third last li element';
@li = ();
$dom->find('li:nth-child(1n+0)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
@li = ();
$dom->find('li:nth-child(1n-0)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
@li = ();
$dom->find('li:nth-child(n+0)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A B C D E F G)], 'found first three li elements';
@li = ();
Expand Down

0 comments on commit d0d09f9

Please sign in to comment.