Documentation
¶
Overview ¶
Package dom is a small, owned DOM node tree built from golang.org/x/net/html.
The engine deliberately owns its node type rather than passing x/net/html nodes around: it keeps the rest of the engine (css, layout, paint) independent of the parser and gives a stable, minimal surface — element tag, attributes, text — that is trivial to construct in tests.
Index ¶
- func AppendChild(parent, child *Node)
- func InnerHTML(n *Node) string
- func InsertBefore(parent, newChild, ref *Node)
- func OuterHTML(n *Node) string
- func RemoveChild(parent, child *Node)
- func SetInnerHTML(n *Node, htmlSrc string) error
- func SetTextContent(n *Node, text string)
- func TextContent(n *Node) string
- func Title(root *Node) string
- type Node
- type NodeType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendChild ¶
func AppendChild(parent, child *Node)
AppendChild appends child to parent's child list, first detaching child from any current parent. Nil operands and self-appends are no-ops.
func InnerHTML ¶
InnerHTML serializes n's children back to HTML (the DOM innerHTML getter). Attribute order is normalised (sorted) so the output is deterministic.
func InsertBefore ¶
func InsertBefore(parent, newChild, ref *Node)
InsertBefore inserts newChild immediately before ref among parent's children, detaching newChild from any current parent first. If ref is nil or not a child of parent, newChild is appended.
func RemoveChild ¶
func RemoveChild(parent, child *Node)
RemoveChild removes child from parent's child list, clearing its Parent. It is a no-op if child is not a direct child of parent.
func SetInnerHTML ¶
SetInnerHTML parses htmlSrc as an HTML fragment and replaces n's children with the result (the DOM innerHTML setter). On a parse error the children are left unchanged and the error is returned.
func SetTextContent ¶
SetTextContent replaces all of n's children with a single text node (or none, for empty text), matching the DOM textContent setter.
func TextContent ¶
TextContent returns the concatenation of every descendant text node's data, in document order (the DOM textContent getter).
Types ¶
type Node ¶
type Node struct {
Type NodeType
Tag string // lowercased tag name (Element only)
Attr map[string]string // lowercased attribute names (Element only)
Text string // character data (Text only)
Parent *Node
Children []*Node
}
Node is a single DOM node. Elements carry a lowercased Tag and an attribute map; text nodes carry Text. Children are in document order.
func NewElement ¶
NewElement creates a detached element node with the given (lowercased) tag and an empty attribute map. It is used by the JS binding's document.createElement.
func Parse ¶
Parse turns an HTML document into an owned Node tree. The returned node is a Document whose single element child is <html>.
func ParseFragment ¶
ParseFragment parses an HTML fragment (as if it were the innerHTML of a <body>) into a list of detached owned nodes.
func (*Node) Attribute ¶
Attribute returns the value of the named attribute (lowercased name) and whether it was present.