Module Css.Selector

CSS selectors: core types and helpers.

Shared selector types exposed by both implementation and interface.

type attribute_match =
  1. | Presence
  2. | Exact of string
  3. | Whitespace_list of string
  4. | Hyphen_list of string
  5. | Prefix of string
  6. | Suffix of string
  7. | Substring of string
type combinator =
  1. | Descendant
  2. | Child
  3. | Next_sibling
  4. | Subsequent_sibling
  5. | Column
type ns =
  1. | Any
  2. | None
  3. | Prefix of string
type attr_flag =
  1. | Case_insensitive
  2. | Case_sensitive
type aria_attr =
  1. | Busy
  2. | Checked
  3. | Disabled
  4. | Expanded
  5. | Hidden
  6. | Pressed
  7. | Readonly
  8. | Required
  9. | Selected
  10. | Custom of string
    (*

    For aria-custom arbitrary values

    *)

ARIA attribute names for type-safe handling

type attr_name =
  1. | Aria of aria_attr
    (*

    aria-* attributes

    *)
  2. | Data of string
    (*

    data-* attributes

    *)
  3. | Regular of string
    (*

    All other attributes (class, type, etc.)

    *)

Structured attribute names

type nth =
  1. | Odd
  2. | Even
  3. | Index of int
  4. | An_plus_b of int * int
type t =
  1. | Element of ns option * string
  2. | Class of string
  3. | Id of string
  4. | Universal of ns option
  5. | Attribute of ns option * attr_name * attribute_match * attr_flag option
  6. | Hover
  7. | Active
  8. | Focus
  9. | Focus_visible
  10. | Focus_within
  11. | Target
  12. | Visited
  13. | Target_within
  14. | Scope
  15. | Root
  16. | Empty
  17. | First_child
  18. | Last_child
  19. | Only_child
  20. | First_of_type
  21. | Last_of_type
  22. | Only_of_type
  23. | Enabled
  24. | Disabled
  25. | Read_only
  26. | Read_write
  27. | Placeholder_shown
  28. | Default
  29. | Checked
  30. | Indeterminate
  31. | Blank
  32. | Valid
  33. | Invalid
  34. | In_range
  35. | Out_of_range
  36. | Required
  37. | Optional
  38. | User_invalid
  39. | User_valid
  40. | Autofill
  41. | Fullscreen
  42. | Modal
  43. | Picture_in_picture
  44. | Left
  45. | Right
  46. | First
  47. | Defined
  48. | Playing
  49. | Paused
  50. | Seeking
  51. | Buffering
  52. | Stalled
  53. | Muted
  54. | Volume_locked
  55. | Future
  56. | Past
  57. | Current
  58. | Popover_open
  59. | Before
  60. | After
  61. | First_letter
  62. | First_line
  63. | Backdrop
  64. | Marker
  65. | Placeholder
  66. | Selection
  67. | File_selector_button
  68. | Moz_focusring
  69. | Webkit_any
  70. | Webkit_autofill
  71. | Moz_placeholder
  72. | Webkit_input_placeholder
  73. | Ms_input_placeholder
  74. | Moz_ui_invalid
  75. | Moz_ui_valid
  76. | Webkit_scrollbar
  77. | Webkit_search_cancel_button
  78. | Webkit_search_decoration
  79. | Webkit_datetime_edit_fields_wrapper
  80. | Webkit_date_and_time_value
  81. | Webkit_datetime_edit
  82. | Webkit_datetime_edit_year_field
  83. | Webkit_datetime_edit_month_field
  84. | Webkit_datetime_edit_day_field
  85. | Webkit_datetime_edit_hour_field
  86. | Webkit_datetime_edit_minute_field
  87. | Webkit_datetime_edit_second_field
  88. | Webkit_datetime_edit_millisecond_field
  89. | Webkit_datetime_edit_meridiem_field
  90. | Webkit_inner_spin_button
  91. | Webkit_outer_spin_button
  92. | Webkit_calendar_picker_indicator
  93. | Is of t list
  94. | Where of t list
  95. | Not of t list
  96. | Has of t list
  97. | Nth_child of nth * t list option
  98. | Nth_last_child of nth * t list option
  99. | Nth_of_type of nth * t list option
  100. | Nth_last_of_type of nth * t list option
  101. | Dir of string
  102. | Lang of string list
  103. | Host of t list option
  104. | Host_context of t list
  105. | State of string
  106. | Active_view_transition_type of string list option
  107. | Heading
  108. | Part of string list
  109. | Slotted of t list
  110. | Cue of t list
  111. | Cue_region of t list
  112. | Highlight of string list
  113. | View_transition_group of string
  114. | View_transition_image_pair of string
  115. | View_transition_old of string
  116. | View_transition_new of string
  117. | Compound of t list
  118. | Combined of t * combinator * t
  119. | List of t list
  120. | Nesting
val element : ?ns:ns -> string -> t

element ?ns name element selector (e.g., "div"). Validates CSS identifiers; raises Invalid_argument on invalid.

val class_ : string -> t

class_ 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 -> t

id 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 -> t

of_string s parses a CSS-escaped selector string:

  • ".classname" → class selector
  • "#idname" → id selector
  • "element" → element selector

Unescapes 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 : t

universal universal selector "*" (no namespace).

val attribute : ?ns:ns -> ?flag:attr_flag -> string -> attribute_match -> t

attribute ?ns ?flag name match attribute selector. Validates identifiers; raises Invalid_argument on invalid.

val combine : t -> combinator -> t -> t

combine a comb b combines selectors with a combinator.

val (++) : t -> t -> t

a ++ b combines with descendant (space).

val (>>) : t -> t -> t

a >> b combines with child (>).

val where : t list -> t

where selectors :where(...) pseudo-class.

val not : t list -> t

not selectors :not(...) pseudo-class.

val list : t list -> t

list selectors comma-separated selector list.

val is_compound_list : t -> bool

is_compound_list selector returns true if already a list of selectors.

val as_list : t -> t list option

as_list selector returns Some selectors if selector is a list, None otherwise. Useful for pattern matching on merged selectors.

val compound : t list -> t

compound selectors compound selector (concatenates simple selectors).

val (&&) : t -> t -> t

a && b combines two simple selectors into a compound selector.

val (||) : t -> t -> t

a || b combines with column combinator (||).

val pp : t Pp.t

pp pretty-prints selectors.

val to_string : ?minify:bool -> t -> string

to_string ?minify sel renders a selector to a string.

val map : (t -> t) -> t -> t

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.t

pp_combinator pretty-prints selector combinators.

val pp_attribute_match : attribute_match Pp.t

pp_attribute_match pretty-prints attribute matchers.

val pp_ns : ns Pp.t

pp_ns pretty-prints namespaces.

val pp_attr_flag : attr_flag option Pp.t

pp_attr_flag pretty-prints optional attribute flags.

val attr_value_needs_quoting : string -> bool

attr_value_needs_quoting value returns true if the given attribute value requires quoting according to CSS specifications. Values need quotes if:

  • They are empty
  • They start with a digit
  • They start with double hyphen (--)
  • They contain characters that are not valid in CSS identifiers (anything other than letters, digits, hyphens, underscores, or non-ASCII)
val pp_nth : nth Pp.t

pp_nth pretty-prints nth expressions.

val read_selector_list : Reader.t -> t

read_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 -> t

read r parses a CSS selector.

val read_combinator : Reader.t -> combinator

read_combinator r parses a combinator.

val read_attribute_match : Reader.t -> attribute_match

read_attribute_match r parses an attribute matcher.

val read_ns : Reader.t -> ns option

read_ns r parses an optional attribute/selector namespace.

val read_attr_flag : Reader.t -> attr_flag option

read_attr_flag r parses an attribute selector flag (i or s).

val read_nth : Reader.t -> nth

read_nth r parses an An+B nth expression.

val is_ : t list -> t

is_ selectors :is(...) pseudo-class.

val has : t list -> t

has selectors :has(...) pseudo-class.

val nth_child : ?of_:t list -> nth -> t

nth_child ?of_ nth builds :nth-child with optional :of.

val host : ?selectors:t list -> unit -> t

host ?selectors () :host or :host(selector) pseudo-class.

val any : (t -> bool) -> t -> bool

any predicate selector returns true if any node in selector satisfies predicate. Analysis helper (structure-based, no string scanning).

val has_focus : t -> bool

has_focus sel checks presence of :focus pseudo-class in the selector.

val has_focus_within : t -> bool

has_focus_within sel checks presence of :focus-within pseudo-class in the selector.

val has_focus_visible : t -> bool

has_focus_visible sel checks presence of :focus-visible pseudo-class in the selector.

val exists_class : (string -> bool) -> t -> bool

exists_class pred sel returns true if any class node satisfies pred.

val first_class : t -> string option

first_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 -> bool

contains_modifier_colon sel returns true if any class name contains a modifier colon (e.g., "md:...", "hover:...").

val has_group_marker : t -> bool

has_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 -> bool

has_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 -> bool

has_pseudo_element sel returns true if selector contains a pseudo-element like ::before, ::after, ::marker, etc.

val modifier_prefix : t -> string option

modifier_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.