Skip to content

Commit

Permalink
slightly faster pseudo classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 11, 2014
1 parent d44a213 commit 1622b49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

5.13 2014-07-10
5.13 2014-07-11
- Improved HTML5.1 compliance of Mojo::DOM::HTML.
- Fixed Mojo::Reactor::Poll bug where watchers were active after they have
been removed. (jberger)
Expand Down
39 changes: 15 additions & 24 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -171,37 +171,28 @@ sub _parent {
sub _pc {
my ($class, $args, $current) = @_;

# ":first-*"
if ($class =~ /^first-(?:(child)|of-type)$/) {
$class = defined $1 ? 'nth-child' : 'nth-of-type';
$args = 1;
}

# ":last-*"
elsif ($class =~ /^last-(?:(child)|of-type)$/) {
$class = defined $1 ? 'nth-last-child' : 'nth-last-of-type';
$args = '-n+1';
}

# ":checked"
if ($class eq 'checked') {
my $attrs = $current->[2];
return 1 if exists $attrs->{checked} || exists $attrs->{selected};
}

# ":empty"
elsif ($class eq 'empty') { return 1 unless defined $current->[4] }
return !defined $current->[4] if $class eq 'empty';

# ":root"
elsif ($class eq 'root') {
if (my $parent = $current->[3]) { return 1 if $parent->[0] eq 'root' }
}
return $current->[3] && $current->[3][0] eq 'root' if $class eq 'root';

# ":not"
elsif ($class eq 'not') { return 1 if !_match($args, $current, $current) }
return !_match($args, $current, $current) if $class eq 'not';

# ":checked"
return exists $current->[2]{checked} || exists $current->[2]{selected}
if $class eq 'checked';

# ":first-*" or ":last-*" (rewrite with equation)
if ($class =~ /^(first|last)-(?:(child)|of-type)$/) {
$class = $1 eq 'first' ? 'nth-' : 'nth-last-';
$class .= defined $2 ? 'child' : 'of-type';
$args = $1 eq 'first' ? 1 : '-n+1';
}

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

Expand Down

0 comments on commit 1622b49

Please sign in to comment.