spec

package
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package spec defines interfaces for a subset of the WHATWG DOM specification (https://dom.spec.whatwg.org). The dom package provides a pure-Go implementation backed by golang.org/x/net/html, and the browser package provides a WASM implementation backed by the browser DOM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChildNode

type ChildNode interface {
	Node

	IsConnected() bool
	OwnerDocument() Document
	ParentNode() Node
	ParentElement() Element
	PreviousSibling() ChildNode
	NextSibling() ChildNode

	// Length is based on https://dom.spec.whatwg.org/#concept-node-length
	Length() int
}

ChildNode extends Node with tree-position methods for nodes that can be children.

type Comment

type Comment interface {
	Node

	Data() string
	SetData(string)
}

Comment represents a comment node. See https://dom.spec.whatwg.org/#interface-comment.

type Document

type Document interface {
	Node

	ElementQueries

	CreateElement(localName string) Element
	CreateElementIs(localName, is string) Element
	CreateTextNode(text string) Text

	Head() Element
	Body() Element
}

Document represents a document node. See https://dom.spec.whatwg.org/#interface-document.

type DocumentFragment

type DocumentFragment interface {
	Node

	Children() ElementCollection
	FirstElementChild() Element
	LastElementChild() Element
	ChildElementCount() int

	Append(nodes ...Node)
	Prepend(nodes ...Node)
	ReplaceChildren(nodes ...Node)

	QuerySelector(query string) Element
	QuerySelectorAll(query string) NodeList[Element]
	QuerySelectorIterator
}

DocumentFragment represents a minimal document. See https://dom.spec.whatwg.org/#interface-documentfragment.

type DocumentPosition

type DocumentPosition int

DocumentPosition is a bitmask returned by Node.CompareDocumentPosition.

const (
	DocumentPositionDisconnected DocumentPosition = 1 << iota
	DocumentPositionPreceding
	DocumentPositionFollowing
	DocumentPositionContains
	DocumentPositionContainedBy
	DocumentPositionImplementationSpecific
)

DocumentPosition values from https://dom.spec.whatwg.org/#interface-node.

type Element

type Element interface {
	Node
	ChildNode
	ParentNode

	TagName() string
	ID() string
	ClassName() string

	GetAttribute(name string) string
	SetAttribute(name, value string)
	RemoveAttribute(name string)
	ToggleAttribute(name string) bool
	HasAttribute(name string) bool

	Closest(selector string) Element
	Matches(selector string) bool

	SetInnerHTML(s string)
	InnerHTML() string
	SetOuterHTML(s string)
	OuterHTML() string
}

Element is based on https://dom.spec.whatwg.org/#interface-element.

InnerText is omitted due to rendering complexity; implementations may add it via InnerTextSetter.

type ElementCollection

type ElementCollection interface {
	// Length returns the number of elements in the collection.
	Length() int

	// Item returns the element with index from the collection. The elements are sorted in tree order.
	Item(index int) Element

	// NamedItem returns the first element with ID or name from the collection.
	NamedItem(name string) Element
}

ElementCollection is a live collection of elements. See https://dom.spec.whatwg.org/#interface-htmlcollection.

type ElementQueries

type ElementQueries interface {
	Contains(other Node) bool

	GetElementsByTagName(name string) ElementCollection
	GetElementsByClassName(name string) ElementCollection

	QuerySelector(query string) Element
	QuerySelectorAll(query string) NodeList[Element]

	QuerySelectorIterator
}

ElementQueries groups methods for finding elements within a subtree.

type InnerTextSetter

type InnerTextSetter interface {
	SetInnerText(s string)
	InnerText() string
}

InnerTextSetter is an optional interface for implementations that support InnerText.

type Node

type Node interface {
	NodeType() NodeType
	CloneNode(deep bool) Node
	IsSameNode(other Node) bool
	TextContent() string
	CompareDocumentPosition(other Node) DocumentPosition
}

Node is a subset of https://dom.spec.whatwg.org/#interface-node.

Child-management methods (HasChildNodes, ChildNodes, FirstChild, LastChild, Contains, InsertBefore, AppendChild, ReplaceChild, RemoveChild) live on ParentNode instead, keeping leaf types like Text slim.

NodeValue and IsEqualNode are omitted: NodeValue only applies to Text (which has Data) and Attr; IsEqualNode varies per node type.

type NodeList

type NodeList[T Node] interface {
	Length() int
	Item(int) T
}

NodeList is an ordered collection of nodes.

type NodeType

type NodeType int

NodeType values from https://dom.spec.whatwg.org/#interface-node.

const (
	NodeTypeUnknown NodeType = iota
	NodeTypeElement
	NodeTypeAttribute
	NodeTypeText
	NodeTypeCdataSection
	NodeTypeEntityReference
	NodeTypeEntity
	NodeTypeProcessingInstruction
	NodeTypeComment
	NodeTypeDocument
	NodeTypeDocumentType
	NodeTypeDocumentFragment
	NodeTypeNotation
)

func (NodeType) String

func (nt NodeType) String() string

type Normalizer

type Normalizer interface {
	Normalize()
}

Normalizer may be implemented by a Node and should follow https://dom.spec.whatwg.org/#dom-node-normalize

type ParentNode

type ParentNode interface {
	Node

	Children() ElementCollection
	FirstElementChild() Element
	LastElementChild() Element
	ChildElementCount() int

	Prepend(nodes ...Node)
	Append(nodes ...Node)
	ReplaceChildren(nodes ...Node)

	ElementQueries

	HasChildNodes() bool
	ChildNodes() NodeList[Node]
	FirstChild() ChildNode
	LastChild() ChildNode
	InsertBefore(node, child ChildNode) ChildNode
	AppendChild(node ChildNode) ChildNode
	ReplaceChild(node, child ChildNode) ChildNode
	RemoveChild(node ChildNode) ChildNode
}

ParentNode combines https://dom.spec.whatwg.org/#interface-parentnode with the child-management methods from Node that only apply to non-leaf nodes.

type QuerySelectorIterator

type QuerySelectorIterator interface {
	QuerySelectorSequence(query string) iter.Seq[Element]
}

QuerySelectorIterator adds iterator-based query support.

type Text

type Text interface {
	ChildNode

	Data() string
	SetData(string)
}

Text represents a text node. See https://dom.spec.whatwg.org/#interface-text.

Jump to

Keyboard shortcuts

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