Skip to content

Instantly share code, notes, and snippets.

/attr.diff Secret

Created January 10, 2015 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/07799f5a33ec597c88af to your computer and use it in GitHub Desktop.
Save anonymous/07799f5a33ec597c88af to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/DOM/CSS.pm b/lib/Mojo/DOM/CSS.pm
index f5f94cc..3a1e857 100644
--- a/lib/Mojo/DOM/CSS.pm
+++ b/lib/Mojo/DOM/CSS.pm
@@ -41,14 +41,13 @@ sub _ancestor {
}
sub _attr {
- my ($key, $regex, $current) = @_;
+ my ($name_re, $value_re, $current) = @_;
# Ignore namespace prefix
my $attrs = $current->[2];
- for my $name (keys %$attrs) {
- next unless $name =~ /(?:^|:)\Q$key\E$/;
- return 1 unless defined $attrs->{$name} && defined $regex;
- return 1 if $attrs->{$name} =~ $regex;
+ for my $name (grep { $_ =~ $name_re } keys %$attrs) {
+ return 1 unless defined $attrs->{$name} && defined $value_re;
+ return 1 if $attrs->{$name} =~ $value_re;
}
return undef;
@@ -96,7 +95,7 @@ sub _compile {
# Tag
push @$part, my $selector = [];
- push @$selector, ['tag', qr/(?:^|:)\Q@{[_unescape($1)]}\E$/]
+ push @$selector, ['tag', _prefix($1)]
if $element =~ s/^((?:\\\.|\\\#|[^.#])+)// && $1 ne '*';
# Class or ID
@@ -106,11 +105,11 @@ sub _compile {
}
# Pseudo classes (":not" contains more selectors)
- push @$selector, ['pc', "$1", $1 eq 'not' ? _compile($2) : _equation($2)]
+ push @$selector, ['pc', lc $1, $1 eq 'not' ? _compile($2) : _equation($2)]
while $pc =~ /$PSEUDO_CLASS_RE/go;
# Attributes
- push @$selector, ['attr', _unescape($1), _regex($2 // '', $3 // $4)]
+ push @$selector, ['attr', _prefix($1), _regex($2 // '', $3 // $4)]
while $attrs =~ /$ATTR_RE/go;
# Combinator
@@ -199,6 +198,8 @@ sub _pc {
return undef;
}
+sub _prefix {qr/(?:^|:)\Q@{[_unescape(shift)]}\E$/}
+
sub _regex {
my ($op, $value) = @_;
return undef unless defined $value;
@@ -249,9 +250,7 @@ sub _selector {
elsif ($type eq 'attr') { return undef unless _attr(@$s[1, 2], $current) }
# Pseudo class
- elsif ($type eq 'pc') {
- return undef unless _pc(lc $s->[1], $s->[2], $current);
- }
+ elsif ($type eq 'pc') { return undef unless _pc(@$s[1, 2], $current) }
}
return 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment