glexml/selector

CSS selectors for glexml documents.

import glexml
import glexml/selector

let assert Ok(document) = glexml.parse(opf_source)
selector.select(document.root, "manifest > item[properties~=nav]")
// -> Ok([the nav item])

Supported syntax:

XML-specific behaviour, since names here are literal strings rather than resolved namespaces:

Types

A parsed selector, ready to be matched. Parse one with parse and reuse it across as many queries as you like.

pub opaque type Selector

A problem found while parsing a selector string.

pub type SelectorError {
  EmptySelector
  UnexpectedEndOfSelector
  UnexpectedCharacter(character: String)
  UnknownPseudoClass(name: String)
  InvalidNth(argument: String)
}

Constructors

  • EmptySelector
  • UnexpectedEndOfSelector
  • UnexpectedCharacter(character: String)
  • UnknownPseudoClass(name: String)
  • InvalidNth(argument: String)

Values

pub fn error_to_string(error: SelectorError) -> String

Convert a SelectorError into a human readable message.

pub fn matches(
  element: glexml.Element,
  selector: Selector,
) -> Bool

Whether the element itself matches the selector, treating it as the root of its own tree.

pub fn parse(input: String) -> Result(Selector, SelectorError)

Parse a selector string.

pub fn query(
  element: glexml.Element,
  selector: Selector,
) -> List(glexml.Element)

Find every element in the tree matching the selector, in document order. The given element is part of the tree being searched, so query(root, ":root") returns the root itself.

pub fn query_first(
  element: glexml.Element,
  selector: Selector,
) -> Result(glexml.Element, Nil)

Find the first element in document order matching the selector.

pub fn select(
  element: glexml.Element,
  selector: String,
) -> Result(List(glexml.Element), SelectorError)

Parse and run a selector in one step. Prefer parse + query when the same selector is used repeatedly.

Search Document