PureHTML.Query.Selector.Tokenizer (PureHTML v0.5.0)

View Source

Tokenizes CSS selector strings into a stream of tokens.

Identifiers follow CSS Syntax Level 3: an ident-start code point is a letter, an underscore, or any non-ASCII code point; an ident code point is also a digit or a hyphen.

Token Types

  • {:ident, value} - Identifier (tag name, attribute name/value)
  • {:class, value} - Class selector (.class)
  • {:id, value} - ID selector (#id)
  • :star - Universal selector (*)
  • :open_bracket - Opening bracket for attribute selector
  • :close_bracket - Closing bracket for attribute selector
  • :equal - Exact match (=)
  • :prefix_match - Prefix match (^=)
  • :suffix_match - Suffix match ($=)
  • :substring_match - Substring match (*=)
  • :comma - Selector list separator
  • {:string, value} - Quoted string
  • :child - Child combinator (>)
  • :adjacent_sibling - Adjacent sibling combinator (+)
  • :general_sibling - General sibling combinator (~)
  • :whitespace - Whitespace (potential descendant combinator)

Summary

Functions

Tokenizes a CSS selector string.

Types

error()

@type error() :: {:invalid_selector, String.t()}

token()

@type token() ::
  {:ident, String.t()}
  | {:class, String.t()}
  | {:id, String.t()}
  | :star
  | :open_bracket
  | :close_bracket
  | :equal
  | :prefix_match
  | :suffix_match
  | :substring_match
  | :comma
  | {:string, String.t()}
  | :child
  | :adjacent_sibling
  | :general_sibling
  | :whitespace

Functions

tokenize(input)

@spec tokenize(String.t()) :: {:ok, [token()]} | {:error, error()}

Tokenizes a CSS selector string.

Returns {:ok, tokens} with leading and trailing whitespace removed, or {:error, {:invalid_selector, reason}}.

Examples

iex> PureHTML.Query.Selector.Tokenizer.tokenize("div")
{:ok, [{:ident, "div"}]}

iex> PureHTML.Query.Selector.Tokenizer.tokenize("div.foo#bar")
{:ok, [{:ident, "div"}, {:class, "foo"}, {:id, "bar"}]}

iex> PureHTML.Query.Selector.Tokenizer.tokenize(".")
{:error, {:invalid_selector, "expected an identifier after '.'"}}