An css selector parser and converter
Thanks to Benjamin Eberlei, Bastian Feder and Jakob Westhoff for ideas and concepts., (*2)
PhpCSS is a parser for CSS 3 selectors. It parses them into an AST and allows them to compile the AST to CSS selectors or Xpath expressions., (*3)
The main target of this project is the possibilty to convert CSS selectors into Xpath expressions., (*4)
A small demo application can be found at: http://xpath.thomas.weinert.info/, (*5)
PhpCss is available on Packagist: Carica/PhpCss. Add it to you composer.json and update., (*6)
Get CSS selector as Xpath expression, (*7)
$expression = PhpCss::toXpath($selector);
Reformat/Validate CSS Selector, (*8)
$selector = PhpCss::reformat($selector);
Get the AST, (*9)
$ast = PhpCss::getAst($selector);
FluentDOM 5 allows to inject a callback to convert selectors. If you have FluentDOM and PhpCss installed in your project, you can use CSS selectors in FluentDOM:, (*10)
$fd = FluentDOM::QueryCss($xml); $fd ->find('td:nth-of-type(even)') ->addClass('even');
Selector | to CSS | to Xpath |
---|---|---|
* | โ | โ |
E | โ | โ |
ns|* | โ | โ |
ns|E | โ | โ |
Attributes | ||
E[foo] | โ | โ |
E[foo="bar"] | โ | โ |
E[foo~="bar"] | โ | โ |
E[foo^="bar"] | โ | โ |
E[foo$="bar"] | โ | โ |
E[foo*="bar"] | โ | โ |
E[foo|="bar"] | โ | โ |
Structural Pseudo Classes | ||
E:root | โ | โ |
E:nth-child(42) | โ | โ |
E:nth-last-child(42) | โ | โ |
E:nth-of-type(42) | โ | โ |
E:nth-last-of-type(42) | โ | โ |
E:first-child | โ | โ |
E:last-child | โ | โ |
E:first-of-type | โ | โ |
E:last-of-type | โ | โ |
E:only-child | โ | โ |
E:only-of-type | โ | โ |
E:empty | โ | โ |
Link Pseudo Classes | ||
E:link | โ | โ |
E:visited | โ | โ |
User Action Pseudo Classes | ||
E:active | โ | โ |
E:hover | โ | โ |
E:focus | โ | โ |
Target Pseudo Class | ||
E:target | โ | โ |
Language Pseudo Class | ||
E:lang(fr) | โ | โ |
UI Element states Pseudo Class | ||
E:enabled | โ | โ (not disabled) |
E:disabled | โ | โ (attribute) |
E:checked | โ | โ (attribute) |
Pseudo Elements | ||
E:first-line | โ | โ |
E:first-letter | โ | โ |
E:before | โ | โ |
E:after | โ | โ |
Class Selector | ||
E.warning | โ | โ |
Id Selector | ||
E#myid | โ | โ |
Negation Pseudo Class | ||
E:not(s) | โ | โ |
Combinators | ||
E F | โ | โ |
E > F | โ | โ |
E + F | โ | โ |
E ~ F | โ | โ |
jQuery | ||
:contains("text") | โ | โ |
:has(s) | โ | โ |
:gt(42) | โ | โ |
:lt(42) | โ | โ |
:odd | โ | โ |
:even | โ | โ |