Skip to content

Commit

Permalink
removed CSS4 selector subject support from Mojo::DOM::CSS until we ac…
Browse files Browse the repository at this point in the history
…tually know the exact semantics
  • Loading branch information
kraih committed Nov 2, 2011
1 parent 9fb0bb6 commit 3bdecef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 72 deletions.
4 changes: 2 additions & 2 deletions Changes
Expand Up @@ -2,8 +2,8 @@ This file documents the revision history for Perl extension Mojolicious.

2.21 2011-11-02 00:00:00
- Removed profile helper.
- Updated CSS4 selectors in Mojo::DOM::CSS with changes from the
latest editor's draft.
- Removed CSS4 selector subject support from Mojo::DOM::CSS until we
actually know the exact semantics.
- Improved Mojo::ByteStream to generate most Mojo::Util based methods
automatically.
- Fixed route pattern bug.
Expand Down
31 changes: 4 additions & 27 deletions lib/Mojo/DOM/CSS.pm
@@ -1,8 +1,6 @@
package Mojo::DOM::CSS;
use Mojo::Base -base;

use List::Util 'first';

my $ESCAPE_RE = qr/\\[^0-9a-fA-F]|\\[0-9a-fA-F]{1,6}/;
my $ATTR_RE = qr/
\[
Expand Down Expand Up @@ -72,9 +70,8 @@ sub select {

# Parts
for my $part (@$pattern) {
my $result = $self->_element($current, $part, $tree);
push(@results, $result) and last
if $result && !first { $_ eq $result } @results;
push(@results, $current) and last
if $self->_element($current, $part, $tree);
}
}
}
Expand Down Expand Up @@ -103,14 +100,10 @@ sub _compile {
my $selector = $part->[-1];

# Element
my $tag = '';
my $tag = '*';
$element =~ s/$ELEMENT_RE// and $tag = $self->_unescape($+{element});

# Subject
$selector->[0] = 'subject' if $tag =~ s/^\?//;

# Tag
$tag = '*' unless $tag;
push @$selector, ['tag', $tag];

# Class or ID
Expand Down Expand Up @@ -223,9 +216,6 @@ sub _element {
# Not a tag
return if $current->[0] ne 'tag';

# Subject
$candidate = $current if $selector->[0] eq 'subject';

# Compare part to element
if ($self->_selector($selector, $current)) {
$siblings = undef;
Expand All @@ -250,7 +240,7 @@ sub _element {
}
}

return $candidate;
return 1;
}

# "Rock stars... is there anything they don't know?"
Expand Down Expand Up @@ -674,19 +664,6 @@ An C<E> element whose attributes match all following attribute selectors.
my $links = $css->select('a[foo^="b"][foo$="ar"]');
=head2 C<E ?F G>
An C<F> element descendant of an C<E> element and ancestor of a C<G> element.
my $wrappers = $css->select('?div.wrapper > :checked');
By default, the subjects of a selector are the elements represented by the
last compound selector.
In CSS4 however the subject can be explicitly identified by prepending a
question mark to one of the compound selectors.
Note that the CSS4 spec is still a work in progress, so this selector might
change without warning!
=head1 ATTRIBUTES
L<Mojo::DOM::CSS> implements the following attributes.
Expand Down
44 changes: 1 addition & 43 deletions t/mojo/dom.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 697;
use Test::More tests => 669;

use ojo;
use Mojo::Util 'encode';
Expand Down Expand Up @@ -1844,48 +1844,6 @@ is "$dom", <<EOF, 'right result';
</table>
EOF

# Selector subject
$dom = Mojo::DOM->new(<<EOF);
<div id="a" class="wrapper">
<div id="b">
<a href="http://kraih.com">Kraih</a>
</div>
<div id="c" class="wrapper">
<a href="http://mojolicio.us">Mojolicious</a>
</div>
</div>
EOF
is $dom->find('div > ?div > a')->[0]->{id}, 'b', 'right attribute';
is $dom->find('div > ?div > a')->[1]->{id}, 'c', 'right attribute';
is $dom->find('div > ?div > a')->[2], undef, 'no result';
is $dom->find('div > ?div > a')->size, 2, 'right number of elements';
is $dom->find('div ?div a')->[0]->{id}, 'b', 'right attribute';
is $dom->find('div ?div a')->[1]->{id}, 'c', 'right attribute';
is $dom->find('div ?div a')->[2], undef, 'no result';
is $dom->find('div ?div a')->size, 2, 'right number of elements';
is $dom->find('.wrapper > ? > [href]')->[0]->{id}, 'b', 'right attribute';
is $dom->find('.wrapper > ? > [href]')->[1]->{id}, 'c', 'right attribute';
is $dom->find('.wrapper ? > [href]')->[2], undef, 'no result';
is $dom->find('.wrapper ? > [href]')->size, 2, 'right number of elements';
is $dom->find('#a > ? [href]')->[0]->{id}, 'b', 'right attribute';
is $dom->find('#a > ? [href]')->[1]->{id}, 'c', 'right attribute';
is $dom->find('#a > ? [href]')->[2], undef, 'no result';
is $dom->find('#a > ? [href]')->size, 2, 'right number of elements';
is $dom->find('?div div a')->[0]->{id}, 'a', 'right attribute';
is $dom->find('?div div a')->[1], undef, 'no result';
is $dom->find('?div div a')->size, 1, 'right number of elements';
is $dom->find('div div ?a')->[0]->{href}, 'http://kraih.com',
'right attribute';
is $dom->find('div div ?a')->[0]->text, 'Kraih', 'right text';
is $dom->find('div div ?a')->[1]->{href}, 'http://mojolicio.us',
'right attribute';
is $dom->find('div div ?a')->[1]->text, 'Mojolicious', 'right text';
is $dom->find('div div ?a')->[2], undef, 'no result';
is $dom->find('div div ?a')->size, 2, 'right number of elements';
is $dom->find('?[class] > div')->[0]->{id}, 'a', 'right attribute';
is $dom->find('?[class] > div')->[2], undef, 'no result';
is $dom->find('?[class] > div')->size, 1, 'right number of elements';

# Preformatted text
$dom = Mojo::DOM->new(<<EOF);
<div>
Expand Down

0 comments on commit 3bdecef

Please sign in to comment.