PureHTML.TreeBuilder.State (PureHTML v0.5.0)

View Source

Parser state for the HTML5 tree construction algorithm.

The stack of open elements is the insertion parent. Each element's parent_ref is its tree parent (nil for html).

Summary

Types

Active formatting element entry.

Child on an in-progress element: a stack ref or a finished node.

Internal element representation stored in the elements map.

Reference to an element in the elements map.

HTML5 insertion mode.

Namespace for foreign elements (SVG or MathML).

A finished node, same as PureHTML.html_node/0.

t()

The tree builder state.

HTML tag name (string) or foreign element tag ({namespace, name}).

Types

af_entry()

@type af_entry() :: :marker | {element_ref(), String.t(), [{String.t(), String.t()}]}

Active formatting element entry.

Either a marker (for scope boundaries like applet, object, etc.) or a tuple containing the element ref, tag name, and attributes for reconstruction.

child()

@type child() :: element_ref() | PureHTML.html_node()

Child on an in-progress element: a stack ref or a finished node.

element()

@type element() :: %{
  ref: element_ref(),
  tag: tag_name(),
  attrs: [PureHTML.attr()],
  children: [child()],
  parent_ref: element_ref() | nil
}

Internal element representation stored in the elements map.

Fields:

  • ref - unique reference for this element
  • tag - tag name (string or {namespace, name} tuple)
  • attrs - element attributes as a list of {name, value} tuples
  • children - list of children (refs, text strings, comments, or tuples)
  • parent_ref - reference to parent element (nil for root)

element_ref()

@type element_ref() :: reference()

Reference to an element in the elements map.

insertion_mode()

@type insertion_mode() ::
  :initial
  | :before_html
  | :before_head
  | :in_head
  | :in_head_noscript
  | :after_head
  | :in_body
  | :text
  | :in_table
  | :in_table_text
  | :in_caption
  | :in_column_group
  | :in_table_body
  | :in_row
  | :in_cell
  | :in_template
  | :after_body
  | :after_after_body
  | :in_frameset
  | :after_frameset
  | :after_after_frameset

HTML5 insertion mode.

The tree builder uses insertion modes to handle tokens differently based on the current parsing context (e.g., inside head vs inside body vs inside table).

namespace()

@type namespace() :: :svg | :math

Namespace for foreign elements (SVG or MathML).

output_node()

@type output_node() :: PureHTML.html_node()

A finished node, same as PureHTML.html_node/0.

t()

@type t() :: %PureHTML.TreeBuilder.State{
  af: [af_entry()],
  context_element: {atom() | nil, String.t()} | nil,
  doctype: {String.t() | nil, String.t() | nil, String.t() | nil} | nil,
  document_children: [child()],
  elements: %{required(element_ref()) => element()},
  error_count: non_neg_integer(),
  form_element: element_ref() | nil,
  foster_parenting: boolean(),
  frameset_ok: boolean(),
  head_element: element_ref() | nil,
  ignore_next_lf: boolean(),
  mode: insertion_mode(),
  original_mode: insertion_mode() | nil,
  pending_table_text: String.t(),
  post_html_nodes: [child()],
  quirks_mode: boolean(),
  scripting: boolean(),
  stack: [element_ref()],
  template_mode_stack: [insertion_mode()],
  tokenizer_state: atom() | nil
}

The tree builder state.

tag_name()

@type tag_name() :: String.t() | {namespace(), String.t()}

HTML tag name (string) or foreign element tag ({namespace, name}).