dom

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package dom defines Growse's browser-owned document model.

Index

Constants

View Source
const (
	MaxNodesPerDocument  = 50_000
	MaxTreeDepth         = 256
	MaxAttributesPerNode = 256
	MaxDOMStringBytes    = 1 << 20
	MaxScriptTextBytes   = 2 << 20
	MaxStyleTextBytes    = 4 << 20
)

Browser-visible DOM limits are shared by initial HTML, runtime mutation, and isolated worker snapshots.

Variables

This section is empty.

Functions

func TextLimitForParent added in v0.15.0

func TextLimitForParent(parentTag string) int

TextLimitForParent preserves the separately specified script and stylesheet resource limits while ordinary DOM strings remain capped at 1 MiB.

Types

type Document

type Document struct {
	Root *Node
	// contains filtered or unexported fields
}

Document owns a DOM tree and its node indexes.

func NewDocument

func NewDocument() *Document

NewDocument creates an empty document with a document root node.

func NewDocumentFromSnapshot added in v0.14.0

func NewDocumentFromSnapshot(snapshot DocumentSnapshot) (*Document, error)

NewDocumentFromSnapshot validates snapshot and constructs an independent document with the same stable Node IDs.

func (*Document) AppendChild

func (d *Document) AppendChild(parent, child *Node) error

AppendChild attaches child beneath parent.

func (*Document) ApplySnapshot added in v0.14.0

func (d *Document) ApplySnapshot(snapshot DocumentSnapshot) error

ApplySnapshot updates a live document in place. Existing nodes with matching IDs and types are reused so runtime-side Element handles remain valid.

func (*Document) CreateDocumentFragment added in v0.15.0

func (d *Document) CreateDocumentFragment() *Node

CreateDocumentFragment creates an unattached fragment owned by the document.

func (*Document) CreateElement

func (d *Document) CreateElement(tagName string, attributes map[string]string) *Node

CreateElement creates an unattached element owned by the document.

func (*Document) CreateText

func (d *Document) CreateText(text string) *Node

CreateText creates an unattached text node owned by the document.

func (*Document) DetachChild added in v0.14.0

func (d *Document) DetachChild(parent, child *Node) bool

DetachChild removes a direct child without destroying its subtree identity.

func (*Document) ElementCount

func (d *Document) ElementCount() int

ElementCount returns the number of element nodes.

func (*Document) GetElementByID

func (d *Document) GetElementByID(id string) (*Node, bool)

GetElementByID returns the first element with the given id attribute.

func (*Document) GetElementsByClassName added in v0.14.0

func (d *Document) GetElementsByClassName(value string) []*Node

GetElementsByClassName returns connected elements containing every class token.

func (*Document) GetElementsByTagName added in v0.14.0

func (d *Document) GetElementsByTagName(value string) []*Node

GetElementsByTagName returns connected elements with the requested tag or all for "*".

func (*Document) InsertChild added in v0.14.0

func (d *Document) InsertChild(parent, child *Node, index int) error

InsertChild attaches or moves child beneath parent at index.

func (*Document) IsConnected

func (d *Document) IsConnected(node *Node) bool

IsConnected はノードがDocumentのルートツリーに接続されているかを返す。

func (*Document) NodeByID

func (d *Document) NodeByID(id NodeID) (*Node, bool)

NodeByID returns the node with the internal document-scoped identifier.

func (*Document) NodeCount

func (d *Document) NodeCount() int

NodeCount returns the number of nodes, including the document root.

func (*Document) OwnedNodeCount added in v0.14.0

func (d *Document) OwnedNodeCount() int

OwnedNodeCount includes detached nodes retained by live Runtime handles.

func (*Document) QuerySelector

func (d *Document) QuerySelector(value string) (*Node, bool)

QuerySelector は対応する単純セレクターに最初に一致する要素を返す。

func (*Document) QuerySelectorAll added in v0.14.0

func (d *Document) QuerySelectorAll(value string) []*Node

QuerySelectorAll returns all connected elements matching a supported selector.

func (*Document) ReadyState added in v0.14.0

func (d *Document) ReadyState() string

ReadyState reports the document loading lifecycle exposed to scripts.

func (*Document) Remove

func (d *Document) Remove(id NodeID) ([]NodeID, bool)

Remove は接続済み要素とその子孫をDocumentから削除し、削除したNodeIDを返す。

func (*Document) RemoveAttribute added in v0.8.0

func (d *Document) RemoveAttribute(id NodeID, name string) bool

RemoveAttribute removes an element attribute and reports whether it existed.

func (*Document) ReplaceChildren added in v0.14.0

func (d *Document) ReplaceChildren(parent *Node, children []*Node) bool

ReplaceChildren atomically moves children beneath parent and detaches the old list.

func (*Document) SetAttribute

func (d *Document) SetAttribute(id NodeID, name, value string) bool

SetAttribute は要素の属性を設定し、値が変化した場合にtrueを返す。

func (*Document) SetReadyState added in v0.14.0

func (d *Document) SetReadyState(state string) bool

SetReadyState advances the document loading lifecycle.

func (*Document) SetTextContent

func (d *Document) SetTextContent(id NodeID, value string) bool

SetTextContent は指定したノードの子を1つのテキストノードへ置き換える。

func (*Document) Snapshot added in v0.14.0

func (d *Document) Snapshot() DocumentSnapshot

Snapshot returns a detached, serializable copy of the current document.

func (*Document) Title

func (d *Document) Title() string

Title returns the normalized text of the first title element.

type DocumentSnapshot added in v0.14.0

type DocumentSnapshot struct {
	Root       NodeSnapshot `json:"root"`
	NextID     NodeID       `json:"nextId"`
	ReadyState string       `json:"readyState,omitempty"`
}

DocumentSnapshot is the pointer-free representation exchanged with an isolated runtime worker. Node IDs remain stable across snapshots.

type Node

type Node struct {
	ID NodeID

	Type NodeType

	TagName string
	Text    string

	Attributes map[string]string

	// ControlValue and ControlChecked hold browser-owned live form state. The
	// corresponding HTML attributes remain the reset defaults.
	ControlValue        string
	ControlValueDirty   bool
	ControlChecked      bool
	ControlCheckedDirty bool

	Parent   *Node
	Children []*Node
	// contains filtered or unexported fields
}

Node is one item in a Growse DOM tree.

func (*Node) Attribute

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

Attribute returns an attribute value from an element node.

func (*Node) TextContent

func (n *Node) TextContent() string

TextContent returns the concatenated text of the node and its descendants.

type NodeID

type NodeID uint64

NodeID uniquely identifies a node within one document.

type NodeSnapshot added in v0.14.0

type NodeSnapshot struct {
	ID                  NodeID            `json:"id"`
	Type                NodeType          `json:"type"`
	TagName             string            `json:"tagName,omitempty"`
	Text                string            `json:"text,omitempty"`
	Attributes          map[string]string `json:"attributes,omitempty"`
	ControlValue        string            `json:"controlValue,omitempty"`
	ControlValueDirty   bool              `json:"controlValueDirty,omitempty"`
	ControlChecked      bool              `json:"controlChecked,omitempty"`
	ControlCheckedDirty bool              `json:"controlCheckedDirty,omitempty"`
	Children            []NodeSnapshot    `json:"children,omitempty"`
}

NodeSnapshot contains only browser-visible DOM state. Parent and document pointers are reconstructed by ApplySnapshot.

type NodeType

type NodeType uint8

NodeType describes the role of a DOM node.

const (
	NodeDocument NodeType = iota
	NodeElement
	NodeText
	NodeDocumentFragment
)

Jump to

Keyboard shortcuts

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