Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
compile equations in advance
  • Loading branch information
kraih committed Dec 2, 2014
1 parent 818bae8 commit bbe375e
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -106,7 +106,7 @@ sub _compile {
}

# Pseudo classes (":not" contains more selectors)
push @$selector, ['pc', "$1", $1 eq 'not' ? _compile($2) : $2]
push @$selector, ['pc', "$1", $1 eq 'not' ? _compile($2) : _equation($2)]
while $pc =~ /$PSEUDO_CLASS_RE/go;

# Attributes
Expand All @@ -123,7 +123,7 @@ sub _compile {
sub _empty { $_[0][0] eq 'comment' || $_[0][0] eq 'pi' }

sub _equation {
my $equation = shift;
return [] unless my $equation = shift;

# "even"
return [2, 2] if $equation =~ /^even$/i;
Expand Down Expand Up @@ -171,7 +171,7 @@ sub _pc {
if $class eq 'checked';

# ":first-*" or ":last-*" (rewrite with equation)
($class, $args) = $1 ? ("nth-$class", 1) : ("nth-last-$class", '-n+1')
($class, $args) = $1 ? ("nth-$class", [0, 1]) : ("nth-last-$class", [-1, 1])
if $class =~ s/^(?:(first)|last)-//;

# ":nth-*"
Expand All @@ -182,7 +182,6 @@ sub _pc {
# ":nth-last-*"
@siblings = reverse @siblings if $class =~ /^nth-last/;

$args = _equation($args) unless ref $args;
for my $i (0 .. $#siblings) {
next if (my $result = $args->[0] * $i + $args->[1]) < 1;
last unless my $sibling = $siblings[$result - 1];
Expand Down

0 comments on commit bbe375e

Please sign in to comment.