dom

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dom builds a document tree from HTML tokens, recovering from tag soup the way the WHATWG HTML standard's tree construction does for the cases that change what text belongs where: implied end tags (p, li, dd, dt, option, table cells and rows), misnested formatting elements (a simplified adoption agency algorithm), reopening of formatting elements closed by a block, foster parenting of content misplaced in tables, foreign content (SVG, MathML) and the elements that break out of it.

Every node records what the page declares about its display, following the element and its ancestors: the hidden attribute, a template's inert content, SVG display and visibility attributes, and inline style display, visibility, opacity:0 and a fully transparent colour. Nothing is judged: no colour is compared, no size or position measured.

Nesting is bounded: below MaxDepth elements are attached to the deepest allowed node instead of nesting further, and the stack of open elements has a bound of its own. The declared reasons are kept across both bounds.

Index

Constants

View Source
const (
	// DefaultMaxDepth is the deepest an element is nested in the tree.
	// Browsers stop near the same depth (Blink at 512).
	DefaultMaxDepth = 512
	// DefaultMaxStack bounds the stack of open elements. Past it an
	// element is attached but not kept open, and the display reasons it
	// declares are given to the element it is in, so that its text keeps
	// them; text after it in that element may get them too.
	DefaultMaxStack = 4096
)

Defaults for Options.

View Source
const MaxFormatting = 64

MaxFormatting bounds the list of formatting elements to reopen after a block. It is a bound of this library, not of the HTML standard; past it the earliest entry is dropped.

Variables

This section is empty.

Functions

This section is empty.

Types

type Caps added in v0.1.1

type Caps struct {
	// Formatting: more than MaxFormatting formatting elements were in
	// the list of those to reopen; the earliest were dropped from it.
	Formatting bool
	// Clones: the bound on formatting elements reopened after a block
	// was reached; past it they apply but are not put in the tree.
	Clones bool
	// Styles counts style attributes longer than css.MaxLen, of which
	// only the first css.MaxLen bytes were read.
	Styles int
}

Caps reports bounds of the builder, other than nesting, that were reached. Text is never lost to them; what an element contributes besides its text (its spans, the display it declares) may be.

func (*Caps) Add added in v0.1.1

func (c *Caps) Add(o Caps)

Add adds what o reports to c.

func (Caps) Any added in v0.1.1

func (c Caps) Any() bool

Any reports whether a bound was reached.

type Doc

type Doc struct {
	Root *Node

	// Limited reports that a nesting bound was reached and the structure
	// below it was flattened.
	Limited bool
	// Caps reports the other bounds of the builder that were reached.
	Caps Caps
	// Replaced counts the character references to no character (�, a
	// surrogate, a number past U+10FFFF) read as U+FFFD.
	Replaced int
	// contains filtered or unexported fields
}

Doc is a parsed document.

func Parse

func Parse(ctx context.Context, src []byte, opt Options) (*Doc, error)

Parse builds the tree of src, which must be UTF-8. It returns ctx's error if ctx is cancelled.

func (*Doc) Release added in v0.4.0

func (d *Doc) Release()

Release gives the memory of the tree back for the next document to be parsed: d, its nodes and their attributes must not be used after it. Nothing made from the tree that is kept (a walk's result) may share its memory. The blocks are cleared, so that they keep nothing of this document alive. Release on a released Doc does nothing.

type NS

type NS uint8

NS is an element's namespace.

const (
	HTML NS = iota
	SVG
	MathML
)

type Node

type Node struct {
	Type  Type
	Name  string // lowercased element name
	NS    NS
	Attrs []lexer.Attr
	// Text is the text of a text or comment node.
	Text string
	// Start and End are byte offsets of the node in the decoded input,
	// from its start tag to the end of its end tag or of the last thing
	// in it.
	Start, End int
	// Display holds the reasons the page declares that the node is not
	// displayed or has no ink, following inheritance as CSS does. For an
	// element it covers the element and everything in it.
	Display gofastcore.Display
	WS      WS

	Parent, FirstChild, LastChild, Prev, Next *Node
	// contains filtered or unexported fields
}

Node is one node of the tree.

func (*Node) Attr

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

Attr returns the value of the named attribute and whether it is present.

func (*Node) Depth

func (n *Node) Depth() int

Depth returns how deeply the node is nested in its tree.

type Options

type Options struct {
	MaxDepth int
	MaxStack int
	// XHTML reads self-closing tags as empty elements (EPUB content).
	XHTML bool
}

Options bounds the tree.

type Type

type Type uint8

Type is the type of a node.

const (
	ElementNode Type = iota + 1
	TextNode
	CommentNode
	DocumentNode
)

type WS

type WS uint8

WS is the white-space handling in effect for text.

const (
	// Normal collapses runs of white space to one space.
	Normal WS = iota
	// Pre keeps all white space.
	Pre
	// PreLine collapses spaces and keeps line breaks.
	PreLine
)

Jump to

Keyboard shortcuts

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