NAME

CSS::Selector::Parser - parse CSS selectors to Perl data structures

VERSION

version 0.003

SYNOPSIS

use CSS::Selector::Parser 'parse_selector';

my @rules = parse_selector('#foo .bar, baz:quux');
# [ { id => 'foo' }, { class => 'bar', combinator => ' ' } ]
# [ { element => 'baz', pseudo => { quux => undef } ]

DESCRIPTION

This module parses CSS selectors and gives back a series of Perl data structures corresponding to the selectors.

FUNCTIONS

CSS::Selector::Parser uses Sub::Exporter. See its documentation for various ways to customize exporting.

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 "SYNOPSIS" for an example).

Supported options:

class_as_array

If set, class will always be an arrayref, even if no class was present in the selector (in which case it will be empty).

See the description of class below.

The hashrefs have:

element

foo in foo#bar.baz.

id

bar in foo#bar.baz. Note: NOT [id="..."].

class

baz.quux in foo#bar.baz.quux if class_as_array option is not set.

[baz, quux] in foo#bar.baz.quux if class_as_array option is set.

Note: NOT [class="..."].

attr

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 } } ]
pseudo

A hashref of pseudo-classes and their contents, if present:

parse_selector(':active:nth(2)')
# [ { pseudo => { active => undef, nth => 2 } } ]
combinator

All hashrefs after the first will have this. One of <[ +]>>. See "SYNOPSIS" for an example.

SEE ALSO

HTML::Selector::XPath, from which I stole code

AUTHOR

Hans Dieter Pearcey <hdp@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Hans Dieter Pearcey <hdp@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.