Css.SelectorCSS selectors: core types and helpers.
Shared selector types exposed by both implementation and interface.
type attr_name = | Aria of aria_attraria-* attributes
*)| Data of stringdata-* attributes
*)| Regular of stringAll other attributes (class, type, etc.)
*)Structured attribute names
type t = | Element of ns option * string| Class of string| Id of string| Universal of ns option| Attribute of ns option * attr_name * attribute_match * attr_flag option| Hover| Active| Focus| Focus_visible| Focus_within| Target| Link| Visited| Any_link| Local_link| Target_within| Scope| Root| Empty| First_child| Last_child| Only_child| First_of_type| Last_of_type| Only_of_type| Enabled| Disabled| Read_only| Read_write| Placeholder_shown| Default| Checked| Indeterminate| Blank| Valid| Invalid| In_range| Out_of_range| Required| Optional| User_invalid| User_valid| Autofill| Fullscreen| Modal| Picture_in_picture| Left| Right| First| Defined| Playing| Paused| Seeking| Buffering| Stalled| Muted| Volume_locked| Future| Past| Current| Popover_open| Before| After| First_letter| First_line| Backdrop| Marker| Placeholder| Selection| Moz_focusring| Webkit_any| Webkit_autofill| Moz_placeholder| Webkit_input_placeholder| Ms_input_placeholder| Moz_ui_invalid| Moz_ui_valid| Webkit_scrollbar| Webkit_search_decoration| Webkit_datetime_edit_fields_wrapper| Webkit_date_and_time_value| Webkit_datetime_edit| Webkit_datetime_edit_year_field| Webkit_datetime_edit_month_field| Webkit_datetime_edit_day_field| Webkit_datetime_edit_hour_field| Webkit_datetime_edit_minute_field| Webkit_datetime_edit_second_field| Webkit_datetime_edit_millisecond_field| Webkit_datetime_edit_meridiem_field| Webkit_calendar_picker_indicator| Is of t list| Where of t list| Not of t list| Has of t list| Nth_child of nth * t list option| Nth_last_child of nth * t list option| Nth_of_type of nth * t list option| Nth_last_of_type of nth * t list option| Dir of string| Lang of string list| Host of t list option| Host_context of t list| State of string| Active_view_transition_type of string list option| Heading| Part of string list| Slotted of t list| Cue of t list| Cue_region of t list| Highlight of string list| View_transition_group of string| View_transition_image_pair of string| View_transition_old of string| View_transition_new of string| Compound of t list| Combined of t * combinator * t| List of t list| Nestingelement ?ns name element selector (e.g., "div"). Validates CSS identifiers; raises Invalid_argument on invalid.
val class_ : string -> tclass_ name class selector from raw (unescaped) string. Accepts any serializable characters including special chars that will be escaped during pretty-printing. Only rejects control characters and '--' prefix. Raises Invalid_argument on invalid.
val id : string -> tid name ID selector from raw (unescaped) string. Accepts any serializable characters including special chars that will be escaped during pretty-printing. Only rejects control characters and '--' prefix. Raises Invalid_argument on invalid.
val of_string : string -> tof_string s parses a CSS-escaped selector string:
".classname" → class selector"#idname" → id selector"element" → element selectorUnescapes both simple escapes (e.g., "\:") and hex escapes (e.g., "\3A"). Example: of_string ".sm\\:p-4" creates a class selector for "sm:p-4".
val universal : tuniversal universal selector "*" (no namespace).
val attribute : ?ns:ns -> ?flag:attr_flag -> string -> attribute_match -> tattribute ?ns ?flag name match attribute selector. Validates identifiers; raises Invalid_argument on invalid.
val combine : t -> combinator -> t -> tcombine a comb b combines selectors with a combinator.
val is_compound_list : t -> boolis_compound_list selector returns true if already a list of selectors.
as_list selector returns Some selectors if selector is a list, None otherwise. Useful for pattern matching on merged selectors.
val to_string : ?minify:bool -> t -> stringto_string ?minify sel renders a selector to a string.
map f selector recursively applies f to all selectors in the tree. The function f is applied bottom-up: first to descendants, then to the current node. This is useful for transforming class names throughout a complex selector structure.
val pp_combinator : combinator Pp.tpp_combinator pretty-prints selector combinators.
val pp_attribute_match : attribute_match Pp.tpp_attribute_match pretty-prints attribute matchers.
attr_value_needs_quoting value returns true if the given attribute value requires quoting according to CSS specifications. Values need quotes if:
val read_selector_list : Reader.t -> tread_selector_list r reads a selector list without checking for end of input. Used when parsing selectors as part of a larger CSS structure.
val read : Reader.t -> tread r parses a CSS selector.
val read_combinator : Reader.t -> combinatorread_combinator r parses a combinator.
val read_attribute_match : Reader.t -> attribute_matchread_attribute_match r parses an attribute matcher.
val read_ns : Reader.t -> ns optionread_ns r parses an optional attribute/selector namespace.
val read_attr_flag : Reader.t -> attr_flag optionread_attr_flag r parses an attribute selector flag (i or s).
val read_nth : Reader.t -> nthread_nth r parses an An+B nth expression.
any predicate selector returns true if any node in selector satisfies predicate. Analysis helper (structure-based, no string scanning).
val has_focus : t -> boolhas_focus sel checks presence of :focus pseudo-class in the selector.
val has_focus_within : t -> boolhas_focus_within sel checks presence of :focus-within pseudo-class in the selector.
val has_focus_visible : t -> boolhas_focus_visible sel checks presence of :focus-visible pseudo-class in the selector.
val exists_class : (string -> bool) -> t -> boolexists_class pred sel returns true if any class node satisfies pred.
val first_class : t -> string optionfirst_class sel returns the first class name found along the leftmost path (Compound > Class, then left side of Combined, or first in List), or None if no class is found.
val contains_modifier_colon : t -> boolcontains_modifier_colon sel returns true if any class name contains a modifier colon (e.g., "md:...", "hover:...").
val has_group_marker : t -> boolhas_group_marker sel returns true if selector contains :where(.group), indicating a group-* modifier like group-hover, group-focus, group-has.
val has_peer_marker : t -> boolhas_peer_marker sel returns true if selector contains :where(.peer), indicating a peer-* modifier like peer-checked, peer-focus, peer-has.
val has_pseudo_element : t -> boolhas_pseudo_element sel returns true if selector contains a pseudo-element like ::before, ::after, ::marker, etc.
val modifier_prefix : t -> string optionmodifier_prefix sel extracts the modifier prefix from the first class in the selector. Returns Some "before:" for ".before:absolute", Some "hover:" for ".hover:bg-blue-500", None for ".shadow" or ".shadow-sm".
This is used by the CSS optimizer to determine if selectors can be safely merged while preserving cascade semantics. Selectors with different modifier prefixes target different elements and must remain separate.