Class CssSelector

java.lang.Object
com.oorian.css.CssSelector

public class CssSelector extends Object
Builder class for constructing complex CSS selectors programmatically.

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 Details

    • CssSelector

      public CssSelector()
      Constructs an empty CSS selector.
  • Method Details

    • setElement

      public CssSelector setElement(String element)
      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

      public CssSelector setId(String id)
      Sets the ID for this selector.
      Parameters:
      id - The element ID (without the "#" prefix).
      Returns:
      This CssSelector for method chaining.
    • addClassName

      public CssSelector addClassName(String className)
      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

      public CssSelector setPseudoClass(String pseudoClass)
      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

      public CssSelector setPseudoElement(String pseudoElement)
      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

      public CssSelector setAttribute(String attribute)
      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

      public CssSelector setAttributeEquals(String attribute, String value)
      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

      public CssSelector setAttributeContainsSpecific(String attribute, String value)
      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

      public CssSelector setAttributeExactly(String attribute, String value)
      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

      public CssSelector setAttributeStartsWith(String attribute, String value)
      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

      public CssSelector setAttributeEndsWith(String attribute, String value)
      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

      public CssSelector setAttributeContains(String attribute, String value)
      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

      public String toString()
      Converts this selector to a CSS selector string.
      Overrides:
      toString in class Object
      Returns:
      The CSS selector as a lowercase string.