dom

package module
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: 9 Imported by: 0

README

dom Go Reference

Pure Go implementation of the WHATWG DOM backed by golang.org/x/net/html. CSS selectors are provided by andybalholm/cascadia.

Packages

Package Description
dom Implements Document, Element, Text, and DocumentFragment using html.Node.
spec Interfaces matching the WHATWG DOM spec. Shared by dom and browser.
domtest Test helpers that parse HTML strings or http.Response bodies into spec types.
browser Experimental. Implements spec interfaces via syscall/js for WASM.

Example

func TestGreeting(t *testing.T) {
	res := httptest.NewRecorder()
	handler.ServeHTTP(res, httptest.NewRequest("GET", "/", nil))

	doc := domtest.ParseResponseDocument(t, res.Result())
	heading := doc.QuerySelector("h1")
	if heading == nil {
		t.Fatal("expected an h1")
	}
	if got := heading.TextContent(); got != "Hello" {
		t.Errorf("heading = %q, want %q", got, "Hello")
	}
}

Documentation

Overview

Package dom implements the spec interfaces using golang.org/x/net/html nodes. CSS selectors are supported via andybalholm/cascadia.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNode

func NewNode(node *html.Node) spec.Node

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

func (*Document) Body

func (d *Document) Body() spec.Element

func (*Document) CloneNode

func (d *Document) CloneNode(deep bool) spec.Node

func (*Document) CompareDocumentPosition

func (d *Document) CompareDocumentPosition(other spec.Node) spec.DocumentPosition

func (*Document) Contains

func (d *Document) Contains(other spec.Node) bool

func (*Document) CreateElement

func (*Document) CreateElement(localName string) spec.Element

func (*Document) CreateElementIs

func (*Document) CreateElementIs(localName, is string) spec.Element

func (*Document) CreateTextNode

func (*Document) CreateTextNode(text string) spec.Text

func (*Document) GetElementsByClassName

func (d *Document) GetElementsByClassName(name string) spec.ElementCollection

func (*Document) GetElementsByTagName

func (d *Document) GetElementsByTagName(name string) spec.ElementCollection

func (*Document) Head

func (d *Document) Head() spec.Element

func (*Document) IsSameNode

func (d *Document) IsSameNode(other spec.Node) bool

func (*Document) NodeType

func (d *Document) NodeType() spec.NodeType

func (*Document) QuerySelector

func (d *Document) QuerySelector(query string) spec.Element

func (*Document) QuerySelectorAll

func (d *Document) QuerySelectorAll(query string) spec.NodeList[spec.Element]

func (*Document) QuerySelectorSequence

func (d *Document) QuerySelectorSequence(query string) iter.Seq[spec.Element]

func (*Document) String

func (d *Document) String() string

func (*Document) TextContent

func (d *Document) TextContent() string

TextContent returns an empty string. The spec says it should return null https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

type DocumentFragment

type DocumentFragment struct {
	// contains filtered or unexported fields
}

func NewDocumentFragment

func NewDocumentFragment(nodes []*html.Node) *DocumentFragment

func (*DocumentFragment) Append

func (d *DocumentFragment) Append(nodes ...spec.Node)

func (*DocumentFragment) ChildElementCount

func (d *DocumentFragment) ChildElementCount() int

func (*DocumentFragment) Children

func (d *DocumentFragment) Children() spec.ElementCollection

func (*DocumentFragment) CloneNode

func (d *DocumentFragment) CloneNode(deep bool) spec.Node

func (*DocumentFragment) CompareDocumentPosition

func (d *DocumentFragment) CompareDocumentPosition(other spec.Node) spec.DocumentPosition

func (*DocumentFragment) FirstElementChild

func (d *DocumentFragment) FirstElementChild() spec.Element

func (*DocumentFragment) IsSameNode

func (d *DocumentFragment) IsSameNode(other spec.Node) bool

func (*DocumentFragment) LastElementChild

func (d *DocumentFragment) LastElementChild() spec.Element

func (*DocumentFragment) NodeType

func (d *DocumentFragment) NodeType() spec.NodeType

func (*DocumentFragment) Prepend

func (d *DocumentFragment) Prepend(nodes ...spec.Node)

func (*DocumentFragment) QuerySelector

func (d *DocumentFragment) QuerySelector(query string) spec.Element

func (*DocumentFragment) QuerySelectorAll

func (d *DocumentFragment) QuerySelectorAll(query string) spec.NodeList[spec.Element]

func (*DocumentFragment) QuerySelectorSequence

func (d *DocumentFragment) QuerySelectorSequence(query string) iter.Seq[spec.Element]

func (*DocumentFragment) ReplaceChildren

func (d *DocumentFragment) ReplaceChildren(nodes ...spec.Node)

func (*DocumentFragment) String

func (d *DocumentFragment) String() string

func (*DocumentFragment) TextContent

func (d *DocumentFragment) TextContent() string

type Element

type Element struct {
	// contains filtered or unexported fields
}

func (*Element) Append

func (e *Element) Append(nodes ...spec.Node)

func (*Element) AppendChild

func (e *Element) AppendChild(node spec.ChildNode) spec.ChildNode

func (*Element) ChildElementCount

func (e *Element) ChildElementCount() int

func (*Element) ChildNodes

func (e *Element) ChildNodes() spec.NodeList[spec.Node]

func (*Element) Children

func (e *Element) Children() spec.ElementCollection

func (*Element) ClassName

func (e *Element) ClassName() string

func (*Element) CloneNode

func (e *Element) CloneNode(deep bool) spec.Node

func (*Element) Closest

func (e *Element) Closest(selector string) spec.Element

func (*Element) CompareDocumentPosition

func (e *Element) CompareDocumentPosition(other spec.Node) spec.DocumentPosition

func (*Element) Contains

func (e *Element) Contains(other spec.Node) bool

func (*Element) FirstChild

func (e *Element) FirstChild() spec.ChildNode

func (*Element) FirstElementChild

func (e *Element) FirstElementChild() spec.Element

func (*Element) GetAttribute

func (e *Element) GetAttribute(name string) string

func (*Element) GetElementsByClassName

func (e *Element) GetElementsByClassName(name string) spec.ElementCollection

func (*Element) GetElementsByTagName

func (e *Element) GetElementsByTagName(name string) spec.ElementCollection

func (*Element) HasAttribute

func (e *Element) HasAttribute(name string) bool

func (*Element) HasChildNodes

func (e *Element) HasChildNodes() bool

func (*Element) ID

func (e *Element) ID() string

func (*Element) InnerHTML

func (e *Element) InnerHTML() string

func (*Element) InsertBefore

func (e *Element) InsertBefore(node, child spec.ChildNode) spec.ChildNode

func (*Element) IsConnected

func (e *Element) IsConnected() bool

func (*Element) IsSameNode

func (e *Element) IsSameNode(other spec.Node) bool

func (*Element) LastChild

func (e *Element) LastChild() spec.ChildNode

func (*Element) LastElementChild

func (e *Element) LastElementChild() spec.Element

func (*Element) Length

func (e *Element) Length() int

func (*Element) Matches

func (e *Element) Matches(selector string) bool

func (*Element) NextSibling

func (e *Element) NextSibling() spec.ChildNode

func (*Element) NodeType

func (e *Element) NodeType() spec.NodeType

func (*Element) OuterHTML

func (e *Element) OuterHTML() string

func (*Element) OwnerDocument

func (e *Element) OwnerDocument() spec.Document

func (*Element) ParentElement

func (e *Element) ParentElement() spec.Element

func (*Element) ParentNode

func (e *Element) ParentNode() spec.Node

func (*Element) Prepend

func (e *Element) Prepend(nodes ...spec.Node)

func (*Element) PreviousSibling

func (e *Element) PreviousSibling() spec.ChildNode

func (*Element) QuerySelector

func (e *Element) QuerySelector(query string) spec.Element

func (*Element) QuerySelectorAll

func (e *Element) QuerySelectorAll(query string) spec.NodeList[spec.Element]

func (*Element) QuerySelectorSequence

func (e *Element) QuerySelectorSequence(query string) iter.Seq[spec.Element]

func (*Element) RemoveAttribute

func (e *Element) RemoveAttribute(name string)

func (*Element) RemoveChild

func (e *Element) RemoveChild(node spec.ChildNode) spec.ChildNode

func (*Element) ReplaceChild

func (e *Element) ReplaceChild(node, child spec.ChildNode) spec.ChildNode

func (*Element) ReplaceChildren

func (e *Element) ReplaceChildren(nodes ...spec.Node)

func (*Element) SetAttribute

func (e *Element) SetAttribute(name, value string)

func (*Element) SetInnerHTML

func (e *Element) SetInnerHTML(s string)

func (*Element) SetOuterHTML

func (e *Element) SetOuterHTML(s string)

func (*Element) String

func (e *Element) String() string

func (*Element) TagName

func (e *Element) TagName() string

func (*Element) TextContent

func (e *Element) TextContent() string

func (*Element) ToggleAttribute

func (e *Element) ToggleAttribute(name string) bool

type Text

type Text struct {
	// contains filtered or unexported fields
}

func (*Text) CloneNode

func (t *Text) CloneNode(_ bool) spec.Node

func (*Text) CompareDocumentPosition

func (t *Text) CompareDocumentPosition(other spec.Node) spec.DocumentPosition

func (*Text) Data

func (t *Text) Data() string

func (*Text) IsConnected

func (t *Text) IsConnected() bool

func (*Text) IsSameNode

func (t *Text) IsSameNode(other spec.Node) bool

func (*Text) Length

func (t *Text) Length() int

func (*Text) NextSibling

func (t *Text) NextSibling() spec.ChildNode

func (*Text) NodeType

func (t *Text) NodeType() spec.NodeType

func (*Text) OwnerDocument

func (t *Text) OwnerDocument() spec.Document

func (*Text) ParentElement

func (t *Text) ParentElement() spec.Element

func (*Text) ParentNode

func (t *Text) ParentNode() spec.Node

func (*Text) PreviousSibling

func (t *Text) PreviousSibling() spec.ChildNode

func (*Text) SetData

func (t *Text) SetData(d string)

func (*Text) String

func (t *Text) String() string

func (*Text) TextContent

func (t *Text) TextContent() string

Directories

Path Synopsis
Package browser implements the spec interfaces using the browser DOM via syscall/js.
Package browser implements the spec interfaces using the browser DOM via syscall/js.
Package domtest provides test helpers that parse HTML into spec types.
Package domtest provides test helpers that parse HTML into spec types.
Package spec defines interfaces for a subset of the WHATWG DOM specification (https://dom.spec.whatwg.org).
Package spec defines interfaces for a subset of the WHATWG DOM specification (https://dom.spec.whatwg.org).

Jump to

Keyboard shortcuts

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