Sablon.XML.Document (sablon v0.4.4)

Copy Markdown View Source

A mutable XML document.

The Ruby implementation of sablon leans heavily on Nokogiri's mutable DOM: nodes are removed, duplicated and re-parented in place while operations are evaluated. Reproducing that faithfully is far simpler with a mutable store than with a persistent tree, so every document owns a Sablon.XML.Store holding its nodes. A Sablon.XML.Node is just a {document, id} pair pointing into that store, which gives the same reference semantics Nokogiri nodes have.

A document is process local; see Sablon.XML.Store for why.

Summary

Functions

Identifier of the implicit document node that owns all top level nodes.

The node representing the document itself.

Discards the document, freeing everything its store holds.

Parses an XML fragment. The resulting document has no single root element, the parsed nodes are direct children of the document node.

Creates a new empty document.

Parses an XML string into a document.

Stores an arbitrary value alongside the document.

The root element of the document, i.e. the first top level element node. Returns nil for documents that only contain a prolog or text.

The concatenated text of the whole document.

Serialises the whole document back to XML.

Updates a value stored with put_meta/3, starting from default.

Types

t()

@type t() :: %Sablon.XML.Document{store: Sablon.XML.Store.t()}

Functions

doc_id()

Identifier of the implicit document node that owns all top level nodes.

doc_node(doc)

@spec doc_node(t()) :: Sablon.XML.Node.t()

The node representing the document itself.

drop(document)

@spec drop(t()) :: :ok

Discards the document, freeing everything its store holds.

fragment(binary)

@spec fragment(binary()) :: t()

Parses an XML fragment. The resulting document has no single root element, the parsed nodes are direct children of the document node.

get_meta(document, key, default \\ nil)

@spec get_meta(t(), term(), term()) :: term()

Reads a value stored with put_meta/3.

new()

@spec new() :: t()

Creates a new empty document.

parse(binary)

@spec parse(binary()) :: t()

Parses an XML string into a document.

put_meta(document, key, value)

@spec put_meta(t(), term(), term()) :: :ok

Stores an arbitrary value alongside the document.

Used for state that Nokogiri keeps on mutable Ruby objects, such as the reference count of a merge field shared by several blocks.

root(doc)

@spec root(t()) :: Sablon.XML.Node.t() | nil

The root element of the document, i.e. the first top level element node. Returns nil for documents that only contain a prolog or text.

text(doc)

@spec text(t()) :: binary()

The concatenated text of the whole document.

to_xml(doc)

@spec to_xml(t()) :: binary()

Serialises the whole document back to XML.

update_meta(document, key, default, fun)

@spec update_meta(t(), term(), term(), (term() -> term())) :: term()

Updates a value stored with put_meta/3, starting from default.