Skip to content

Commit

Permalink
handle unknown pseudo-classes gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 17, 2015
1 parent 804c89d commit b05eb88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -101,19 +101,20 @@ sub _compile {
# Pseudo-class
elsif ($css =~ /\G:([\w\-]+)(?:\(((?:\([^)]+\)|[^)])+)\))?/gcs) {
my ($name, $args) = (lc $1, $2);
push @$last, my $pc = ['pc', $name];

# ":not" (contains more selectors)
$pc->[2] = _compile($args) if $name eq 'not';
$args = _compile($args) if $name eq 'not';

# "nth-*" (with An+B notation)
$args = _equation($args) if $name =~ /^nth-/;

# ":first-*" (rewrite to ":nth-*")
@$pc[1, 2] = ("nth-$1", [0, 1]) if $name =~ /^first-(.+)$/;
($name, $args) = ("nth-$1", [0, 1]) if $name =~ /^first-(.+)$/;

# ":last-*" (rewrite to ":nth-*")
@$pc[1, 2] = ("nth-$name", [-1, 1]) if $name =~ /^last-/;
($name, $args) = ("nth-$name", [-1, 1]) if $name =~ /^last-/;

# "nth-*" (with An+B notation)
$pc->[2] = _equation($args) if $name =~ /^nth-/;
push @$last, ['pc', $name, $args];
}

# Tag
Expand Down Expand Up @@ -172,7 +173,7 @@ sub _pc {
if $class eq 'checked';

# ":nth-*"
if ($args) {
if (ref $args) {
my $type = $class =~ /of-type$/ ? $current->[1] : undef;
my @siblings = @{_siblings($current, $type)};

Expand Down
1 change: 1 addition & 0 deletions t/mojo/dom.t
Expand Up @@ -1063,6 +1063,7 @@ is $dom->find('li:nth-child(0n+0)')->size, 0, 'no results';
is $dom->find('li:nth-child(0)')->size, 0, 'no results';
is $dom->find('li:nth-child()')->size, 0, 'no results';
is $dom->find('li:nth-child(whatever)')->size, 0, 'no results';
is $dom->find('li:whatever(whatever)')->size, 0, 'no results';

# Even more pseudo-classes
$dom = Mojo::DOM->new(<<EOF);
Expand Down

0 comments on commit b05eb88

Please sign in to comment.