presentable_soup

Types

A HTML element, queried from a HTML document.

pub type Element {
  Element(
    tag: String,
    attributes: List(#(String, String)),
    children: List(Element),
  )
  Text(String)
}

Constructors

  • Element(
      tag: String,
      attributes: List(#(String, String)),
      children: List(Element),
    )

    A HTML element

  • Text(String)

    Some text

A Matcher describes how to match a specific element in an Element tree. It might be the element’s tag name, a class name, an attribute, or some combination of these.

pub opaque type Matcher

A query is used to find elements within a HTML document, similar to a CSS selector.

Run a query on a HTML document with the find and find_all functions.

pub opaque type Query

Values

pub fn aria(name: String, value: String) -> Matcher

Match elements that have the given aria-* attribute.

pub fn attribute(name: String, value: String) -> Matcher

Matches elements that have the specified attribute with the given value. If the value is left blank, this matcher will match any element that has the attribute, regardless of its value.

pub fn class(name: String) -> Matcher

Matches elements that include the given space-separated class name(s).

If you need to match the class attribute exactly, you can use the attribute matcher instead.

pub fn data(name: String, value: String) -> Matcher

Matches elements that have the given data-* attribute.

pub fn descendant(
  of parent: Query,
  matching matcher: List(Matcher),
) -> Query

Given a Query that finds an element, find any of that element’s descendants that match the given Matcher. This will walk the entire tree from the matching parent.

pub fn element(matching matcher: List(Matcher)) -> Query

Find any elements in a view that match the given Matcher.

pub fn elements_to_string(html: List(Element)) -> String

Convert elements into a pretty-printed HTML string.

Examples

let elements = [
  soup.Element("h1", [], soup.Text("Hello, Joe! <3"))
]
assert soup.elements_to_string(elements)
  == "<h1>Hello, Joe! &lt;3</h1>"
pub fn find(
  in html: String,
  matching query: Query,
) -> Result(Element, Nil)

Find the first element in a view that matches the given Query.

pub fn find_all(
  in html: String,
  matching query: Query,
) -> Result(List(Element), Nil)

Find all elements in a view that matches the given Query.

pub fn id(name: String) -> Matcher

Matches an element based on its id attribute. Well-formed HTML means that only one element should have a given id.

pub fn math_ml(value: String) -> Matcher

Matches MathML elements based on their tag name.

pub fn svg(value: String) -> Matcher

Matches SVG elements based on their tag name.

pub fn tag(value: String) -> Matcher

Matches elements based on their tag name, like "div", "span", or "a".

pub fn test_id(value: String) -> Matcher

It is a common convention to use the data-test-id attribute to mark elements for easy querying in tests. This function is a shorthand for writing query.data("test-id", value)

Search Document