Documentation
¶
Overview ¶
Package internal contains the data tables and helpers shared by htmlstream and htmlutil.
Index ¶
- Constants
- Variables
- func AppendEscape[S ~string | ~[]byte](dst []byte, s S, t *EscapeTable) []byte
- func DecodeRef(dst, s []byte, attr, xml bool) (_ []byte, n int)
- func DecodeRune[S ~string | ~[]byte](s S) (rune, int)
- func LookupEncoding(label string) (string, bool)
- func LookupEntity(name []byte) (string, bool)
- func ParseDecl(b []byte) (d Decl, n int, err *DeclError, ok bool)
- func QuirksDoctype(data []byte) bool
- type Decl
- type DeclError
- type Doctype
- type Elem
- type EscapeTable
- type Map
Constants ¶
const ( EntityMaxName = 32 // longest name including ';' EntityMaxLegacy = 6 // longest name without ';' EntityMaxValue = 6 // longest UTF-8 value )
const ( NoQuirks = iota LimitedQuirks Quirks )
Document modes selected by doctypes.
const IndexMin = 16
IndexMin is the number of attributes or namespace declarations above which they are looked up in a map rather than by a linear search, so that reading a tag takes time linear in the number of its attributes.
Variables ¶
var ( // EscapeText escapes &, <, > and carriage returns. EscapeText = escapeTable("&<>\r") // EscapeAttr escapes &, <, >, ", ', tabs, newlines and carriage returns. EscapeAttr = escapeTable("&<>\"'\t\n\r") // EscapeXML escapes &, <, >, ", tabs, newlines and carriage returns. EscapeXML = escapeTable("&<>\"\t\n\r") )
var MathMLAttrNames = map[string]string{
"definitionurl": "definitionURL",
}
var SVGAttrNames = map[string]string{
"attributename": "attributeName", "attributetype": "attributeType",
"basefrequency": "baseFrequency", "baseprofile": "baseProfile", "calcmode": "calcMode",
"clippathunits": "clipPathUnits", "diffuseconstant": "diffuseConstant",
"edgemode": "edgeMode", "filterunits": "filterUnits", "glyphref": "glyphRef",
"gradienttransform": "gradientTransform", "gradientunits": "gradientUnits",
"kernelmatrix": "kernelMatrix", "kernelunitlength": "kernelUnitLength",
"keypoints": "keyPoints", "keysplines": "keySplines", "keytimes": "keyTimes",
"lengthadjust": "lengthAdjust", "limitingconeangle": "limitingConeAngle",
"markerheight": "markerHeight", "markerunits": "markerUnits", "markerwidth": "markerWidth",
"maskcontentunits": "maskContentUnits", "maskunits": "maskUnits", "numoctaves": "numOctaves",
"pathlength": "pathLength", "patterncontentunits": "patternContentUnits",
"patterntransform": "patternTransform", "patternunits": "patternUnits",
"pointsatx": "pointsAtX", "pointsaty": "pointsAtY", "pointsatz": "pointsAtZ",
"preservealpha": "preserveAlpha", "preserveaspectratio": "preserveAspectRatio",
"primitiveunits": "primitiveUnits", "refx": "refX", "refy": "refY",
"repeatcount": "repeatCount", "repeatdur": "repeatDur",
"requiredextensions": "requiredExtensions", "requiredfeatures": "requiredFeatures",
"specularconstant": "specularConstant", "specularexponent": "specularExponent",
"spreadmethod": "spreadMethod", "startoffset": "startOffset", "stddeviation": "stdDeviation",
"stitchtiles": "stitchTiles", "surfacescale": "surfaceScale",
"systemlanguage": "systemLanguage", "tablevalues": "tableValues", "targetx": "targetX",
"targety": "targetY", "textlength": "textLength", "viewbox": "viewBox",
"viewtarget": "viewTarget", "xchannelselector": "xChannelSelector",
"ychannelselector": "yChannelSelector", "zoomandpan": "zoomAndPan",
}
var SVGTagNames = map[string]string{
"altglyph": "altGlyph", "altglyphdef": "altGlyphDef", "altglyphitem": "altGlyphItem",
"animatecolor": "animateColor", "animatemotion": "animateMotion",
"animatetransform": "animateTransform", "clippath": "clipPath", "feblend": "feBlend",
"fecolormatrix": "feColorMatrix", "fecomponenttransfer": "feComponentTransfer",
"fecomposite": "feComposite", "feconvolvematrix": "feConvolveMatrix",
"fediffuselighting": "feDiffuseLighting", "fedisplacementmap": "feDisplacementMap",
"fedistantlight": "feDistantLight", "fedropshadow": "feDropShadow", "feflood": "feFlood", "fefunca": "feFuncA",
"fefuncb": "feFuncB", "fefuncg": "feFuncG", "fefuncr": "feFuncR",
"fegaussianblur": "feGaussianBlur", "feimage": "feImage", "femerge": "feMerge",
"femergenode": "feMergeNode", "femorphology": "feMorphology", "feoffset": "feOffset",
"fepointlight": "fePointLight", "fespecularlighting": "feSpecularLighting",
"fespotlight": "feSpotLight", "fetile": "feTile", "feturbulence": "feTurbulence",
"foreignobject": "foreignObject", "glyphref": "glyphRef",
"lineargradient": "linearGradient", "radialgradient": "radialGradient",
"textpath": "textPath",
}
SVGTagNames and SVGAttrNames map lowercase SVG names to the case HTML parsers give them, and MathMLAttrNames does the same for MathML attributes. They are the tables of the HTML Standard (see htmlstream.AdjustSVGTagName and AdjustSVGAttrName). golang.org/x/net/html leaves out feDropShadow.
var SVGURLElems = []string{"a", "feImage", "image", "script", "use"}
SVGURLElems lists the SVG elements with an href (or xlink:href) attribute loaded or followed by browsers, in the case used by SVG.
var TokenMode func(tok any) (mode uint8, xmlSyntax bool)
TokenMode returns the mode (an htmlstream.Mode, as set with htmlstream.Reader.SetMode) of the Reader which read tok, an *htmlstream.Token, and whether tok was read with XML syntax (htmlstream.Reader.SetXMLSyntax, or a document read as HTML in Universal mode; false for implied end tags). The htmlstream package, which imports this one, sets it in an init function.
var URLAttrs = map[string][]string{
"a": {"href"},
"area": {"href"},
"audio": {"src"},
"base": {"href"},
"blockquote": {"cite"},
"body": {"background"},
"button": {"formaction"},
"del": {"cite"},
"embed": {"src"},
"form": {"action"},
"frame": {"src", "longdesc"},
"iframe": {"src", "longdesc"},
"img": {"src", "srcset", "longdesc"},
"input": {"src", "formaction"},
"ins": {"cite"},
"link": {"href", "imagesrcset"},
"object": {"data"},
"q": {"cite"},
"script": {"src"},
"source": {"src", "srcset"},
"table": {"background"},
"tbody": {"background"},
"td": {"background"},
"tfoot": {"background"},
"th": {"background"},
"thead": {"background"},
"tr": {"background"},
"track": {"src"},
"video": {"src", "poster"},
}
URLAttrs lists the attributes of HTML elements whose values are URLs (or srcset lists of URLs), by lowercase element name.
Functions ¶
func AppendEscape ¶
func AppendEscape[S ~string | ~[]byte](dst []byte, s S, t *EscapeTable) []byte
AppendEscape appends s to dst, writing the bytes escaped by t as character references.
func DecodeRef ¶
DecodeRef decodes the character reference at the start of s, which must begin with '&'. It returns the decoded value appended to dst and the number of bytes of s consumed, or n == 0 if s does not start with a reference that should be decoded.
In attribute values (attr), a named reference without a terminating ';' followed by '=' or an alphanumeric character is not decoded. In XML mode (xml), named and numeric references require a terminating ';', and the windows-1252 mapping of C1 references is not applied.
func DecodeRune ¶
DecodeRune is utf8.DecodeRune for strings and byte slices.
func LookupEncoding ¶
LookupEncoding returns the name of the encoding with the label, like "get an encoding" in the Encoding Standard: leading and trailing ASCII whitespace is removed, and labels are compared ASCII case-insensitively.
func LookupEntity ¶
LookupEntity returns the value of the named character reference name (without the leading '&', including the ';' if any).
func ParseDecl ¶
ParseDecl parses the declaration at the start of b like xmlutil.ParseDecl. ok is false if b doesn't start with a declaration.
func QuirksDoctype ¶
QuirksDoctype reports whether the data of a complete doctype (between "<!DOCTYPE" and ">") selects quirks mode in HTML parsers.
Types ¶
type Doctype ¶
type Doctype struct {
Name string // ASCII-lowercased, or empty if missing
Public, System string // identifiers
HasPublic, HasSystem bool
ForceQuirks bool // the tokenizer's force-quirks flag (a malformed doctype)
}
Doctype is a doctype as HTML parsers read it.
func ParseDoctype ¶
ParseDoctype parses the data of a doctype (between "<!DOCTYPE" and ">") like the HTML tokenizer. NUL characters are replaced with U+FFFD.
func (Doctype) QuirksMode ¶
QuirksMode returns the document mode the doctype selects in HTML parsers, following the initial insertion mode (see quirksPublicIDs).
type Elem ¶
type Elem uint32
Elem describes how HTML parsers treat an HTML element.
const ( Void Elem = 1 << iota // no end tag (see htmlutil.IsVoid) RawText // RAWTEXT content RCDATA // RCDATA content Script // script data content Plaintext // PLAINTEXT content Noscript // RAWTEXT content if scripting is enabled OmitEnd // end tag may be omitted Scope // limits the default scope TableScope // limits the table scope ClosesP // start tag closes an open p element Breakout // start tag closes foreign content Heading // h1-h6 Head // allowed in head Special // in the HTML "special" category Formatting // formatting element (reopened after misnested end tags by HTML parsers) Marker // scope marker for the list of active formatting elements Block // block-level: text before and after it is on separate lines when rendered (see htmlutil.IsBlock) NoReconstruct // start tag doesn't reconstruct the active formatting elements TableContent // allowed in a table outside cells (other content is moved before the table) ImpliedEnd // closed by the "generate implied end tags" steps of HTML parsers Hidden // contents aren't rendered as text (see htmlutil.IsHidden) Preformatted // whitespace in the contents is kept when rendered )
type EscapeTable ¶
type EscapeTable [256]uint8
EscapeTable maps bytes to the index of their character reference in escapeRefs, or to 0 for bytes written as is.
type Map ¶
type Map[K comparable, V any] struct { M map[K]V // contains filtered or unexported fields }
Map is a map reused for each tag. Emptying a map with clear takes time proportional to the most entries it has held, so Reset replaces it after it was used for a much larger tag.