| CSS-Selector-Parser documentation | view source | Contained in the CSS-Selector-Parser distribution. |
CSS::Selector::Parser - parse CSS selectors to Perl data structures
version 0.002
use CSS::Selector::Parser 'parse_selector';
my @rules = parse_selector('#foo .bar, baz:quux');
# [ { id => 'foo' }, { class => 'bar', combinator => ' ' } ]
# [ { element => 'baz', pseudo => { quux => undef } ]
This module parses CSS selectors and gives back a series of Perl data structures corresponding to the selectors.
CSS::Selector::Parser uses Sub::Exporter. See its documentation for various ways to customize exporting.
my @rules = parse_selector($selector);
CSS selectors are mapped to Perl data structures. Each set of selectors is returned as an arrayref of hashrefs (see SYNOPSIS for an example).
The hashrefs have:
foo in foo#bar.baz.
bar in foo#bar.baz. Note: NOT [id="..."].
baz in foo#bar.baz. Note: NOT [class="..."].
A hashref of attribute selectors, each of which has a hashref of operators and values:
parse_selector('[foo="bar"]')
# [ { attr => { foo => { '=' => 'bar' } } } ]
Attribute selectors can also test for presence:
parse_selector('[foo]')
# [ { attr => { foo => undef } } ]
A hashref of pseudo-classes and their contents, if present:
parse_selector(':active:nth(2)')
# [ { pseudo => { active => undef, nth => 2 } } ]
All hashrefs after the first will have this. One of <[ +]>>. See
SYNOPSIS for an example.
HTML::Selector::XPath, from which I stole code
Hans Dieter Pearcey <hdp@cpan.org>
This software is copyright (c) 2010 by Hans Dieter Pearcey <hdp@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as perl itself.
| CSS-Selector-Parser documentation | view source | Contained in the CSS-Selector-Parser distribution. |