Cascade is a pure Java implementation of CSS Selectors operating on W3C DOM.
Selector selector = Selector.compile("div p");
Document doc = ... /* doc is an instance of org.w3c.dom.Document */
List<Element> selected = selector.select(doc.getDocumentElement());
- Compliant with W3C Selectors Level 3.
- Operating on standard W3C DOM interface defined in
org.w3c.dom
package. - Simple and intuitive API.
- Optimization is performed when selectors are compiled.
- Supports namespace.
- Compatible with documents provided by
javafx.scene.web.WebEngine
. - Passed 700+ test cases.
For more detail, please see API documentation
- Java 8 or higher.
The library is available in the Maven Central repository. Add a dependency to your pom.xml as below:
<dependency>
<groupId>io.github.i49</groupId>
<artifactId>cascade</artifactId>
<version>3.2.0</version>
</dependency>
Selector | Example |
---|---|
Universal selector | * |
Type selector | h1 |
ID selector | #notice |
Class selector | .example |
Attribute selector (presence) | [title] |
Attribute selector (exact match) | [class="example"] |
Attribute selector (space-separated) | [class~="example"] |
Attribute selector (dash-separated) | [hreflang|="en"] |
Attribute selector (prefix) | [type^="image/"] |
Attribute selector (suffix) | [href$=".html"] |
Attribute selector (substring) | [title*="hello"] |
Selector | Example |
---|---|
:root | :root |
:nth-child | :nth-child(2n + 1) :nth-child(odd) |
:nth-last-child | :nth-last-child(2n) :nth-last-child(even) |
:nth-of-type | :nth-of-type(2n + 1) :nth-of-type(odd) |
:nth-last-of-type | :nth-last-of-type(2n) :nth-last-of-type(even) |
:first-child | :first-child |
:last-child | :last-child |
:first-of-type | :first-of-type |
:last-of-type | :last-of-type |
:only-child | :only-child |
:only-of-type | :only-of-type |
:empty | :empty |
:not | :not(.example) |
Selector | Example |
---|---|
Descendant combinator | h1 em |
Child combinator | body > p |
Adjacent sibling combinator | h1 + h2 |
General sibling combinator | h1 ~ pre |
Pseudo-elements are not supported.
Element and attribute names can be qualified with namespaces.
SelectorCompiler compiler = SelectorCompiler.create()
.withNamespace("ns", "http://www.w3.org/2000/svg");
Selector selector = compiler.compile("ns|circle");
Document doc = ... /* doc is an instance of org.w3c.dom.Document */
List<Element> selected = selector.select(doc.getDocumentElement());