dom

package
v0.3.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

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

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

func InnerHTML(n *Node) string

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 OuterHTML

func OuterHTML(n *Node) string

OuterHTML serializes n itself (element and its subtree) back to HTML.

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

func SetInnerHTML(n *Node, htmlSrc string) error

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

func SetTextContent(n *Node, text string)

SetTextContent replaces all of n's children with a single text node (or none, for empty text), matching the DOM textContent setter.

func TextContent

func TextContent(n *Node) string

TextContent returns the concatenation of every descendant text node's data, in document order (the DOM textContent getter).

func Title

func Title(root *Node) string

Title returns the document's <title> text, trimmed, or "".

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 Find

func Find(n *Node, tag string) *Node

Find returns the first element in document order with the given tag, or nil.

func NewElement

func NewElement(tag string) *Node

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 NewText

func NewText(text string) *Node

NewText creates a detached text node with the given character data.

func Parse

func Parse(htmlSrc string) (*Node, error)

Parse turns an HTML document into an owned Node tree. The returned node is a Document whose single element child is <html>.

func ParseFragment

func ParseFragment(htmlSrc string) ([]*Node, error)

ParseFragment parses an HTML fragment (as if it were the innerHTML of a <body>) into a list of detached owned nodes.

func (*Node) Attribute

func (n *Node) Attribute(name string) (string, bool)

Attribute returns the value of the named attribute (lowercased name) and whether it was present.

func (*Node) Classes

func (n *Node) Classes() []string

Classes returns the whitespace-split class list of an element.

func (*Node) ID

func (n *Node) ID() string

ID returns the element's id attribute (empty if absent).

type NodeType

type NodeType uint8

NodeType enumerates the kinds of node the engine cares about.

const (
	// Document is the synthetic root of a parsed tree.
	Document NodeType = iota
	// Element is a tag node (Tag/Attr populated).
	Element
	// Text is a run of character data (Text populated).
	Text
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL