Skip to content

Commit

Permalink
improve Mojo::DOM::CSS to handle attribute selectors with single quot…
Browse files Browse the repository at this point in the history
…es correctly (closes #777)
  • Loading branch information
kraih committed Apr 5, 2015
1 parent 74ea9ad commit 3bcced3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@

6.06 2015-04-02
6.06 2015-04-05
- Added "chat.pl" to example scripts.
- Improved Mojo::DOM::CSS to handle attribute selectors with single quotes
correctly.

6.05 2015-03-24
- Fixed circular require bug in Mojo::Base and Mojo::Util.
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -6,11 +6,11 @@ has 'tree';
my $ESCAPE_RE = qr/\\[^0-9a-fA-F]|\\[0-9a-fA-F]{1,6}/;
my $ATTR_RE = qr/
\[
((?:$ESCAPE_RE|[\w\-])+) # Key
((?:$ESCAPE_RE|[\w\-])+) # Key
(?:
(\W)?= # Operator
(?:"((?:\\"|[^"])*)"|([^\]]+?)) # Value
(?:\s+(i))? # Case-sensitivity
(\W)?= # Operator
(?:"((?:\\"|[^"])*)"|'((?:\\'|[^'])*)'|([^\]]+?)) # Value
(?:\s+(i))? # Case-sensitivity
)?
\]
/x;
Expand Down Expand Up @@ -90,7 +90,7 @@ sub _compile {

# Attributes
elsif ($css =~ /\G$ATTR_RE/gco) {
push @$last, ['attr', _name($1), _value($2 // '', $3 // $4, $5)];
push @$last, ['attr', _name($1), _value($2 // '', $3 // $4 // $5, $6)];
}

# Pseudo-class (":not" contains more selectors)
Expand Down
18 changes: 10 additions & 8 deletions t/mojo/dom.t
Expand Up @@ -370,14 +370,16 @@ is $dom->at('[foo=ba]'), undef, 'no result';
is $dom->at('.tset')->text, 'works', 'right text';

# Already decoded Unicode snowman and quotes in selector
$dom = Mojo::DOM->new('<div id="snowm&quot;an">☃</div>');
is $dom->at('[id="snowm\"an"]')->text, '', 'right text';
is $dom->at('[id="snowm\22 an"]')->text, '', 'right text';
is $dom->at('[id="snowm\000022an"]')->text, '', 'right text';
is $dom->at('[id="snowm\22an"]'), undef, 'no result';
is $dom->at('[id="snowm\21 an"]'), undef, 'no result';
is $dom->at('[id="snowm\000021an"]'), undef, 'no result';
is $dom->at('[id="snowm\000021 an"]'), undef, 'no result';
$dom = Mojo::DOM->new('<div id="snow&apos;m&quot;an">☃</div>');
is $dom->at('[id="snow\'m\"an"]')->text, '', 'right text';
is $dom->at('[id="snow\'m\22 an"]')->text, '', 'right text';
is $dom->at('[id="snow\'m\000022an"]')->text, '', 'right text';
is $dom->at('[id="snow\'m\22an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\21 an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\000021an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\000021 an"]'), undef, 'no result';
is $dom->at("[id='snow\\'m\"an']")->text, '', 'right text';
is $dom->at("[id='snow\\27m\"an']")->text, '', 'right text';

# Unicode and escaped selectors
my $html
Expand Down

0 comments on commit 3bcced3

Please sign in to comment.