Class CssSelector
CssSelector allows you to build CSS selectors piece by piece, combining elements, IDs, classes, pseudo-classes, pseudo-elements, and attribute selectors.
Example usage:
CssSelector selector = new CssSelector();
selector.setElement("input");
selector.addClassName("form-control");
selector.setPseudoClass("focus");
// Generates: input.form-control:focus
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddClassName(String className) Adds a CSS class to this selector.setAttribute(String attribute) Sets an attribute presence selector.setAttributeContains(String attribute, String value) Sets a substring attribute value selector (*=).setAttributeContainsSpecific(String attribute, String value) Sets a space-separated attribute value selector (~=).setAttributeEndsWith(String attribute, String value) Sets a suffix attribute value selector ($=).setAttributeEquals(String attribute, String value) Sets an exact attribute value selector (=).setAttributeExactly(String attribute, String value) Sets a hyphen-separated attribute value selector (|=).setAttributeStartsWith(String attribute, String value) Sets a prefix attribute value selector (^=).setElement(String element) Sets the HTML element type for this selector.Sets the ID for this selector.setPseudoClass(String pseudoClass) Sets a pseudo-class for this selector.setPseudoElement(String pseudoElement) Sets a pseudo-element for this selector.toString()Converts this selector to a CSS selector string.
-
Constructor Details
-
CssSelector
public CssSelector()Constructs an empty CSS selector.
-
-
Method Details
-
setElement
Sets the HTML element type for this selector.- Parameters:
element- The HTML tag name (e.g., "div", "p", "input").- Returns:
- This CssSelector for method chaining.
-
setId
Sets the ID for this selector.- Parameters:
id- The element ID (without the "#" prefix).- Returns:
- This CssSelector for method chaining.
-
addClassName
Adds a CSS class to this selector.Multiple classes can be added to create compound class selectors.
- Parameters:
className- The class name to add (without the "." prefix).- Returns:
- This CssSelector for method chaining.
-
setPseudoClass
Sets a pseudo-class for this selector.- Parameters:
pseudoClass- The pseudo-class name (e.g., "hover", "focus", "active").- Returns:
- This CssSelector for method chaining.
-
setPseudoElement
Sets a pseudo-element for this selector.- Parameters:
pseudoElement- The pseudo-element name (e.g., "before", "after", "first-line").- Returns:
- This CssSelector for method chaining.
-
setAttribute
Sets an attribute presence selector.Matches elements that have the specified attribute, regardless of its value.
- Parameters:
attribute- The attribute name to check for presence.- Returns:
- This CssSelector for method chaining.
-
setAttributeEquals
Sets an exact attribute value selector (=).Matches elements where the attribute exactly equals the specified value.
- Parameters:
attribute- The attribute name.value- The exact value to match.- Returns:
- This CssSelector for method chaining.
-
setAttributeContainsSpecific
Sets a space-separated attribute value selector (~=).Matches elements where the attribute contains the value as a whole word in a space-separated list.
- Parameters:
attribute- The attribute name.value- The value to find in the space-separated list.- Returns:
- This CssSelector for method chaining.
-
setAttributeExactly
Sets a hyphen-separated attribute value selector (|=).Matches elements where the attribute value is exactly the value or starts with the value followed by a hyphen. Commonly used for language codes.
- Parameters:
attribute- The attribute name.value- The value or prefix to match.- Returns:
- This CssSelector for method chaining.
-
setAttributeStartsWith
Sets a prefix attribute value selector (^=).Matches elements where the attribute value starts with the specified value.
- Parameters:
attribute- The attribute name.value- The prefix to match.- Returns:
- This CssSelector for method chaining.
-
setAttributeEndsWith
Sets a suffix attribute value selector ($=).Matches elements where the attribute value ends with the specified value.
- Parameters:
attribute- The attribute name.value- The suffix to match.- Returns:
- This CssSelector for method chaining.
-
setAttributeContains
Sets a substring attribute value selector (*=).Matches elements where the attribute value contains the specified value anywhere.
- Parameters:
attribute- The attribute name.value- The substring to match.- Returns:
- This CssSelector for method chaining.
-
toString
Converts this selector to a CSS selector string.
-