javascript_dom_parser

Bindings to the JavaScript DOMParser API, to enable parsing of HTML in the browser with Gleam. And any other JavaScript runtimes that have DOMParser.

Types

A reference to a node in a DOM tree, parsed from some HTML.

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

Constructors

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

    Arguments

    • tag

      The tag of the element, written in uppercase. For example:

      <div>Hello!</div>
      

      Would have the tag "DIV".

    • attributes

      The attributes of the element. For example:

      <div class="greeting">Hello!</div>
      

      Would have the attributes [#("class", "greeting")].

    • children

      The children of the element.

  • Text(String)
  • Comment(String)

Functions

pub fn dom_to_records(html: Dom) -> HtmlNode

Convert from JavaScript DOM to Gleam records representing the same document.

pub fn dom_to_string(element: Dom) -> String

Convert DOM into HTML.

pub fn parse_to_dom(html: String) -> Dom

Parse HTML to the DOM using the JavaScript DOMParser. Any invalid HTML will be converted into a valid document with a similar structure rather than returning an error or crashing.

For more information see JavaScript documentation for the DOMParser: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser

pub fn parse_to_records(html: String) -> HtmlNode

Parse HTML to Gleam records using the JavaScript DOMParser. Any invalid HTML will be converted into a valid document with a similar structure rather than returning an error or crashing.

For more information see JavaScript documentation for the DOMParser: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser

Search Document