PureHTML.TreeBuilder.State (PureHTML v0.4.0)

View Source

Parser state for the HTML5 tree construction algorithm.

Architecture: Stack tracks "open elements" for parsing context, while DOM structure is built via explicit parent_ref relationships in the elements map.

Summary

Types

Active formatting element entry.

Child content: element ref, text, comment, or pre-built tuple.

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).

Output node format: {tag, attrs, children} tuple.

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() | String.t() | {:comment, String.t()} | output_node()

Child content: element ref, text, comment, or pre-built tuple.

element()

@type element() :: %{
  ref: element_ref(),
  tag: tag_name(),
  attrs: [{String.t(), String.t()}],
  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() ::
  {tag_name(), [{String.t(), String.t()}], [output_node() | String.t()]}

Output node format: {tag, attrs, children} tuple.

t()

@type t() :: %PureHTML.TreeBuilder.State{
  af: [af_entry()],
  context_element: term(),
  document_children: [child()],
  elements: %{required(element_ref()) => element()},
  error_count: non_neg_integer(),
  form_element: element_ref() | nil,
  foster_parenting: term(),
  frameset_ok: boolean(),
  head_element: element_ref() | nil,
  mode: insertion_mode(),
  original_mode: insertion_mode() | nil,
  pending_table_text: String.t(),
  post_html_nodes: [child()],
  quirks_mode: term(),
  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}).