use Mojo::DOM::CSS; # Select elements from DOM tree my $css = Mojo::DOM::CSS->new(tree => $tree); my $elements = $css->select('h1, h2, h3');
my $all = $css->select('*');
my $title = $css->select('title');
my $links = $css->select('a[href]');
my $case_sensitive = $css->select('input[type="hidden"]'); my $case_sensitive = $css->select('input[type=hidden]');
my $case_insensitive = $css->select('input[type="hidden" i]'); my $case_insensitive = $css->select('input[type=hidden i]'); my $case_insensitive = $css->select('input[class~="foo" i]');
This selector is part of Selectors Level 4 <http://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
my $foo = $css->select('input[class~="foo"]'); my $foo = $css->select('input[class~=foo]');
my $begins_with = $css->select('input[name^="f"]'); my $begins_with = $css->select('input[name^=f]');
my $ends_with = $css->select('input[name$="o"]'); my $ends_with = $css->select('input[name$=o]');
my $contains = $css->select('input[name*="fo"]'); my $contains = $css->select('input[name*=fo]');
my $english = $css->select('link[hreflang|=en]');
my $root = $css->select(':root');
my $third = $css->select('div:nth-child(3)'); my $odd = $css->select('div:nth-child(odd)'); my $even = $css->select('div:nth-child(even)'); my $top3 = $css->select('div:nth-child(-n+3)');
my $third = $css->select('div:nth-last-child(3)'); my $odd = $css->select('div:nth-last-child(odd)'); my $even = $css->select('div:nth-last-child(even)'); my $bottom3 = $css->select('div:nth-last-child(-n+3)');
my $third = $css->select('div:nth-of-type(3)'); my $odd = $css->select('div:nth-of-type(odd)'); my $even = $css->select('div:nth-of-type(even)'); my $top3 = $css->select('div:nth-of-type(-n+3)');
my $third = $css->select('div:nth-last-of-type(3)'); my $odd = $css->select('div:nth-last-of-type(odd)'); my $even = $css->select('div:nth-last-of-type(even)'); my $bottom3 = $css->select('div:nth-last-of-type(-n+3)');
my $first = $css->select('div p:first-child');
my $last = $css->select('div p:last-child');
my $first = $css->select('div p:first-of-type');
my $last = $css->select('div p:last-of-type');
my $lonely = $css->select('div p:only-child');
my $lonely = $css->select('div p:only-of-type');
my $empty = $css->select(':empty');
my $links = $css->select(':link'); my $links = $css->select(':visited');
my $input = $css->select(':checked');
my $warning = $css->select('div.warning');
my $foo = $css->select('div#foo');
my $others = $css->select('div p:not(:first-child, :last-child)');
Support for compound selectors was added as part of Selectors Level 4 <http://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
my $headers = $css->select(':matches(section, article, aside, nav) h1');
This selector is part of Selectors Level 4 <http://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
my $elem = $css->select('lq|elem', lq => 'http://example.com/q-markup');
Using an empty alias searches for an element that belongs to no namespace.
my $div = $c->select('|div');
my $headlines = $css->select('div h1');
my $headlines = $css->select('html > body > div > h1');
my $second = $css->select('h1 + h2');
my $second = $css->select('h1 ~ h2');
my $headlines = $css->select('h1, h2, h3');
my $links = $css->select('a[foo^=b][foo$=ar]');
my $tree = $css->tree; $css = $css->tree(['root']);
Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
my $bool = $css->matches('head > title'); my $bool = $css->matches('svg|line', svg => 'http://www.w3.org/2000/svg');
Check if first node in ``tree'' matches the CSS selector. Trailing key/value pairs can be used to declare xml namespace aliases.
my $results = $css->select('head > title'); my $results = $css->select('svg|line', svg => 'http://www.w3.org/2000/svg');
Run CSS selector against ``tree''. Trailing key/value pairs can be used to declare xml namespace aliases.
my $result = $css->select_one('head > title'); my $result = $css->select_one('svg|line', svg => 'http://www.w3.org/2000/svg');
Run CSS selector against ``tree'' and stop as soon as the first node matched. Trailing key/value pairs can be used to declare xml namespace aliases.