dom

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 9 Imported by: 0

README

dom Go Reference

A pure Go implementation of a subset of the WHATWG DOM, backed by golang.org/x/net/html, with CSS selectors from andybalholm/cascadia.

Its main use is asserting on the HTML your handlers return, without a browser.

go get github.com/typelate/dom

Packages

Package Description
spec Interfaces for a subset of the WHATWG DOM. Implemented by dom and browser.
dom Document, Element, Text, and DocumentFragment over html.Node.
domtest Parses HTML strings, io.Readers, and http.Response bodies into spec types.
browser Experimental. The same interfaces over the real browser DOM 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")
	}
}

To assert on a fragment rather than a whole page, parse it in the context of the element it will land in — the parent decides how the markup is interpreted:

rows := domtest.ParseResponseDocumentFragment(t, res.Result(), atom.Tbody)

Notes

  • An invalid CSS selector panics, because queries compile with cascadia.MustCompile.
  • domtest helpers report failures with t.Error and return nil, which does not stop the test. Check the result before using it.
  • Collections are not uniformly live: Children reflects later mutations, while GetElementsByTagName and GetElementsByClassName return a snapshot.

Documentation

Overview

Package dom implements the spec interfaces on golang.org/x/net/html nodes, with CSS selectors from andybalholm/cascadia.

Two behaviors are worth knowing before you start:

  • An invalid CSS selector panics. Queries compile with cascadia.MustCompile, so selectors are expected to be constants.
  • NewNode handles element, text, and document nodes. Any other node type, such as a comment or doctype, panics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNode

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

NewNode wraps an html.Node, returning nil if node is nil. It panics for node types other than element, text, and document.

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 (d *Document) CreateElement(localName string) spec.Element

func (*Document) CreateElementIs

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

func (*Document) CreateTextNode

func (d *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

CloneNode returns a new fragment. A deep clone copies each child; a shallow clone shares them. Note that https://dom.spec.whatwg.org/#dom-node-clonenode says a shallow clone has no children at all.

func (*DocumentFragment) CompareDocumentPosition

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

func (*DocumentFragment) Contains added in v0.9.0

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

func (*DocumentFragment) FirstElementChild

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

func (*DocumentFragment) GetElementsByClassName added in v0.9.0

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

func (*DocumentFragment) GetElementsByTagName added in v0.9.0

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

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.
example command
Command example serves a greeting that one form field changes.
Command example serves a greeting that one form field changes.
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