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 ¶
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.
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.
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 ¶
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 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.
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.