Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

class_as_array option

  • Loading branch information...
commit 4267b2fa6b357944942bf2a8dbcc4bc18ec4733e 1 parent 044d397
@yko authored
Showing with 32 additions and 3 deletions.
  1. +3 −2 Changes
  2. +23 −1 lib/CSS/Selector/Parser.pm
  3. +6 −0 t/parser.t
View
5 Changes
@@ -2,10 +2,11 @@
* first release
-0.002 2010-01-31 hdp
+0.002 Sun Nov 06 16:26:38 2011
* prereq on Test::More with done_testing()
-0.003 2011-11-03 yko
+0.003 2011-11-06 yko
* multiple class selectors support
+ * class_as_array option
View
24 lib/CSS/Selector/Parser.pm
@@ -18,6 +18,8 @@ my $re_comma = qr/^\s*,/;
sub parse_selector {
local $_ = shift;
+ my %options = @_;
+
my @rules;
s/\s+$//;
RULE: {
@@ -55,6 +57,10 @@ sub parse_selector {
}
}
+ if ($options{class_as_array}) {
+ $class = defined $class ? [split /\.+/, $class] : [];
+ }
+
my $simple = {
element => $element,
id => $id,
@@ -113,10 +119,22 @@ ways to customize exporting.
=head2 parse_selector
my @rules = parse_selector($selector);
+ my @rules = parse_selector($selector, %options);
CSS selectors are mapped to Perl data structures. Each set of selectors is
returned as an arrayref of hashrefs (see L</SYNOPSIS> for an example).
+Supported options:
+
+=over
+
+=item class_as_array
+
+If set, class will be always arrayref. Each element of class will represent
+a single class defined in selector.
+
+=back
+
The hashrefs have:
=over
@@ -131,7 +149,11 @@ C<bar> in C<foo#bar.baz>. Note: NOT C<[id="..."]>.
=item class
-C<baz> in C<foo#bar.baz>. Note: NOT C<[class="..."]>.
+C<baz.qux> in C<foo#bar.baz.qux> if C<class_as_array> option is not set.
+
+[C<baz>, C<qux>] in C<foo#bar.baz.qux> if C<class_as_array> option is set.
+
+Note: NOT C<[class="..."]>.
=item attr
View
6 t/parser.t
@@ -21,6 +21,12 @@ is_deeply [ parse('#foo:first, .bar:nth(2)') ],
is_deeply [parse('.first.second.third')], [[{class => 'first.second.third'}]];
+is_deeply [parse('.first.second.third', class_as_array => 1)],
+ [[{class => [qw/first second third/]}]], 'class as array';
+
+is_deeply [parse('p', class_as_array => 1)],
+ [[{element => 'p', class => []}]], 'empty class as array';
+
is_deeply
[ parse('foo#bar:baz.quux') ],
[ parse('foo.quux:baz#bar') ],
Please sign in to comment.
Something went wrong with that request. Please try again.