{"modulePath":"github.com/wfernandes/cli","version":"v6.1.1+incompatible","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package html\n\nPackage html implements an HTML5-compliant tokenizer and parser.\n\nTokenization is done by creating a Tokenizer for an io.Reader r. It is the caller's responsibility to ensure that r provides UTF-8 encoded HTML.\n\n\tz := html.NewTokenizer(r)\n\nGiven a Tokenizer z, the HTML is tokenized by repeatedly calling z.Next(), which parses the next token and returns its type, or an error:\n\n\tfor {\n\t\ttt := z.Next()\n\t\tif tt == html.ErrorToken {\n\t\t\t// ...\n\t\t\treturn ...\n\t\t}\n\t\t// Process the current token.\n\t}\n\nThere are two APIs for retrieving the current token. The high-level API is to call Token; the low-level API is to call Text or TagName / TagAttr. Both APIs allow optionally calling Raw after Next but before Token, Text, TagName, or TagAttr. In EBNF notation, the valid call sequence per token is:\n\n\tNext {Raw} [ Token | Text | TagName {TagAttr} ]\n\nToken returns an independent data structure that completely describes a token. Entities (such as \"\u0026lt;\") are unescaped, tag names and attribute keys are lower-cased, and attributes are collected into a \\[]Attribute. For example:\n\n\tfor {\n\t\tif z.Next() == html.ErrorToken {\n\t\t\t// Returning io.EOF indicates success.\n\t\t\treturn z.Err()\n\t\t}\n\t\temitToken(z.Token())\n\t}\n\nThe low-level API performs fewer allocations and copies, but the contents of the \\[]byte values returned by Text, TagName and TagAttr may change on the next call to Next. For example, to extract an HTML page's anchor text:\n\n\tdepth := 0\n\tfor {\n\t\ttt := z.Next()\n\t\tswitch tt {\n\t\tcase ErrorToken:\n\t\t\treturn z.Err()\n\t\tcase TextToken:\n\t\t\tif depth \u003e 0 {\n\t\t\t\t// emitBytes should copy the []byte it receives,\n\t\t\t\t// if it doesn't process it immediately.\n\t\t\t\temitBytes(z.Text())\n\t\t\t}\n\t\tcase StartTagToken, EndTagToken:\n\t\t\ttn, _ := z.TagName()\n\t\t\tif len(tn) == 1 \u0026\u0026 tn[0] == 'a' {\n\t\t\t\tif tt == StartTagToken {\n\t\t\t\t\tdepth++\n\t\t\t\t} else {\n\t\t\t\t\tdepth--\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\nParsing is done by calling Parse with an io.Reader, which returns the root of the parse tree (the document element) as a \\*Node. It is the caller's responsibility to ensure that the Reader provides UTF-8 encoded HTML. For example, to process each anchor node in depth-first order:\n\n\tdoc, err := html.Parse(r)\n\tif err != nil {\n\t\t// ...\n\t}\n\tvar f func(*html.Node)\n\tf = func(n *html.Node) {\n\t\tif n.Type == html.ElementNode \u0026\u0026 n.Data == \"a\" {\n\t\t\t// Do something with n...\n\t\t}\n\t\tfor c := n.FirstChild; c != nil; c = c.NextSibling {\n\t\t\tf(c)\n\t\t}\n\t}\n\tf(doc)\n\nThe relevant specifications include: [http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html](http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html) and [http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html](http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html)\n\n## Functions\n\n```go\nfunc EscapeString(s string) string\n```\nEscapeString escapes special characters like \"\\\u003c\" to become \"\u0026lt;\". It escapes only five such characters: \\\u003c, \u003e, \u0026, ' and \". UnescapeString(EscapeString(s)) == s always holds, but the converse isn't always true.\n\n```go\nfunc Render(w io.Writer, n *Node) error\n```\nRender renders the parse tree n to the given writer.\n\nRendering is done on a 'best effort' basis: calling Parse on the output of Render will always result in something similar to the original tree, but it is not necessarily an exact clone unless the original tree was 'well-formed'. 'Well-formed' is not easily specified; the HTML5 specification is complicated.\n\nCalling Parse on arbitrary input typically results in a 'well-formed' parse tree. However, it is possible for Parse to yield a 'badly-formed' parse tree. For example, in a 'well-formed' parse tree, no \\\u003ca\u003e element is a child of another \\\u003ca\u003e element: parsing \"\\\u003ca\u003e\\\u003ca\u003e\" results in two sibling elements. Similarly, in a 'well-formed' parse tree, no \\\u003ca\u003e element is a child of a \\\u003ctable\u003e element: parsing \"\\\u003cp\u003e\\\u003ctable\u003e\\\u003ca\u003e\" results in a \\\u003cp\u003e with two sibling children; the \\\u003ca\u003e is reparented to the \\\u003ctable\u003e's parent. However, calling Parse on \"\\\u003ca\u003e\\\u003ctable\u003e\\\u003ca\u003e\" does not return an error, but the result has an \\\u003ca\u003e element with an \\\u003ca\u003e child, and is therefore not 'well-formed'.\n\nProgrammatically constructed trees are typically also 'well-formed', but it is possible to construct a tree that looks innocuous but, when rendered and re-parsed, results in a different tree. A simple example is that a solitary text node would become a tree containing \\\u003chtml\u003e, \\\u003chead\u003e and \\\u003cbody\u003e elements. Another example is that the programmatic equivalent of \"a\\\u003chead\u003eb\\\u003c/head\u003ec\" becomes \"\\\u003chtml\u003e\\\u003chead\u003e\\\u003chead/\u003e\\\u003cbody\u003eabc\\\u003c/body\u003e\\\u003c/html\u003e\".\n\n```go\nfunc UnescapeString(s string) string\n```\nUnescapeString unescapes entities like \"\u0026lt;\" to become \"\\\u003c\". It unescapes a larger range of entities than EscapeString escapes. For example, \"\u0026aacute;\" unescapes to \"á\", as does \"\u0026#225;\" and \"\u0026xE1;\". UnescapeString(EscapeString(s)) == s always holds, but the converse isn't always true.\n\n## Types\n\n```go\ntype Attribute struct {\n\tNamespace, Key, Val string\n}\n```\nAn Attribute is an attribute namespace-key-value triple. Namespace is non-empty for foreign attributes like xlink, Key is alphabetic (and hence does not contain escapable characters like '\u0026', '\\\u003c' or '\u003e'), and Val is unescaped (it looks like \"a\\\u003cb\" rather than \"a\u0026lt;b\").\n\nNamespace is only used by the parser, not the tokenizer.\n\n```go\ntype Node struct {\n\tParent, FirstChild, LastChild, PrevSibling, NextSibling *Node\n\n\tType      NodeType\n\tDataAtom  atom.Atom\n\tData      string\n\tNamespace string\n\tAttr      []Attribute\n}\n```\nA Node consists of a NodeType and some Data (tag name for element nodes, content for text) and are part of a tree of Nodes. Element nodes may also have a Namespace and contain a slice of Attributes. Data is unescaped, so that it looks like \"a\\\u003cb\" rather than \"a\u0026lt;b\". For element nodes, DataAtom is the atom for Data, or zero if Data is not a known tag name.\n\nAn empty Namespace implies a \"[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)\" namespace. Similarly, \"math\" is short for \"[http://www.w3.org/1998/Math/MathML](http://www.w3.org/1998/Math/MathML)\", and \"svg\" is short for \"[http://www.w3.org/2000/svg](http://www.w3.org/2000/svg)\".\n\n```go\nfunc Parse(r io.Reader) (*Node, error)\n```\nParse returns the parse tree for the HTML from the given Reader. The input is assumed to be UTF-8 encoded.\n\n```go\nfunc ParseFragment(r io.Reader, context *Node) ([]*Node, error)\n```\nParseFragment parses a fragment of HTML and returns the nodes that were found. If the fragment is the InnerHTML for an existing element, pass that element in context.\n\n```go\nfunc (n *Node) AppendChild(c *Node)\n```\nAppendChild adds a node c as a child of n.\n\nIt will panic if c already has a parent or siblings.\n\n```go\nfunc (n *Node) InsertBefore(newChild, oldChild *Node)\n```\nInsertBefore inserts newChild as a child of n, immediately before oldChild in the sequence of n's children. oldChild may be nil, in which case newChild is appended to the end of n's children.\n\nIt will panic if newChild already has a parent or siblings.\n\n```go\nfunc (n *Node) RemoveChild(c *Node)\n```\nRemoveChild removes a node c that is a child of n. Afterwards, c will have no parent and no siblings.\n\nIt will panic if c's parent is not n.\n\n```go\ntype NodeType uint32\n```\nA NodeType is the type of a Node.\n\n```go\nconst (\n\tErrorNode NodeType = iota\n\tTextNode\n\tDocumentNode\n\tElementNode\n\tCommentNode\n\tDoctypeNode\n)\n```\n\n```go\ntype Token struct {\n\tType     TokenType\n\tDataAtom atom.Atom\n\tData     string\n\tAttr     []Attribute\n}\n```\nA Token consists of a TokenType and some Data (tag name for start and end tags, content for text, comments and doctypes). A tag Token may also contain a slice of Attributes. Data is unescaped for all Tokens (it looks like \"a\\\u003cb\" rather than \"a\u0026lt;b\"). For tag Tokens, DataAtom is the atom for Data, or zero if Data is not a known tag name.\n\n```go\nfunc (t Token) String() string\n```\nString returns a string representation of the Token.\n\n```go\ntype TokenType uint32\n```\nA TokenType is the type of a Token.\n\n```go\nconst (\n\t// ErrorToken means that an error occurred during tokenization.\n\tErrorToken TokenType = iota\n\t// TextToken means a text node.\n\tTextToken\n\t// A StartTagToken looks like \u003ca\u003e.\n\tStartTagToken\n\t// An EndTagToken looks like \u003c/a\u003e.\n\tEndTagToken\n\t// A SelfClosingTagToken tag looks like \u003cbr/\u003e.\n\tSelfClosingTagToken\n\t// A CommentToken looks like \u003c!--x--\u003e.\n\tCommentToken\n\t// A DoctypeToken looks like \u003c!DOCTYPE x\u003e\n\tDoctypeToken\n)\n```\n\n```go\nfunc (t TokenType) String() string\n```\nString returns a string representation of the TokenType.\n\n```go\ntype Tokenizer struct {\n\t// contains filtered or unexported fields\n}\n```\nA Tokenizer returns a stream of HTML Tokens.\n\n```go\nfunc NewTokenizer(r io.Reader) *Tokenizer\n```\nNewTokenizer returns a new HTML Tokenizer for the given Reader. The input is assumed to be UTF-8 encoded.\n\n```go\nfunc NewTokenizerFragment(r io.Reader, contextTag string) *Tokenizer\n```\nNewTokenizerFragment returns a new HTML Tokenizer for the given Reader, for tokenizing an exisitng element's InnerHTML fragment. contextTag is that element's tag, such as \"div\" or \"iframe\".\n\nFor example, how the InnerHTML \"a\\\u003cb\" is tokenized depends on whether it is for a \\\u003cp\u003e tag or a \\\u003cscript\u003e tag.\n\nThe input is assumed to be UTF-8 encoded.\n\n```go\nfunc (z *Tokenizer) AllowCDATA(allowCDATA bool)\n```\nAllowCDATA sets whether or not the tokenizer recognizes \\\u003c!\\[CDATA\\[foo]]\u003e as the text \"foo\". The default value is false, which means to recognize it as a bogus comment \"\\\u003c!-- \\[CDATA\\[foo]] --\u003e\" instead.\n\nStrictly speaking, an HTML5 compliant tokenizer should allow CDATA if and only if tokenizing foreign content, such as MathML and SVG. However, tracking foreign-contentness is difficult to do purely in the tokenizer, as opposed to the parser, due to HTML integration points: an \\\u003csvg\u003e element can contain a \\\u003cforeignObject\u003e that is foreign-to-SVG but not foreign-to- HTML. For strict compliance with the HTML5 tokenization algorithm, it is the responsibility of the user of a tokenizer to call AllowCDATA as appropriate. In practice, if using the tokenizer without caring whether MathML or SVG CDATA is text or comments, such as tokenizing HTML to find all the anchor text, it is acceptable to ignore this responsibility.\n\n```go\nfunc (z *Tokenizer) Err() error\n```\nErr returns the error associated with the most recent ErrorToken token. This is typically io.EOF, meaning the end of tokenization.\n\n```go\nfunc (z *Tokenizer) Next() TokenType\n```\nNext scans the next token and returns its type.\n\n```go\nfunc (z *Tokenizer) NextIsNotRawText()\n```\nNextIsNotRawText instructs the tokenizer that the next token should not be considered as 'raw text'. Some elements, such as script and title elements, normally require the next token after the opening tag to be 'raw text' that has no child elements. For example, tokenizing \"\\\u003ctitle\u003ea\\\u003cb\u003ec\\\u003c/b\u003ed\\\u003c/title\u003e\" yields a start tag token for \"\\\u003ctitle\u003e\", a text token for \"a\\\u003cb\u003ec\\\u003c/b\u003ed\", and an end tag token for \"\\\u003c/title\u003e\". There are no distinct start tag or end tag tokens for the \"\\\u003cb\u003e\" and \"\\\u003c/b\u003e\".\n\nThis tokenizer implementation will generally look for raw text at the right times. Strictly speaking, an HTML5 compliant tokenizer should not look for raw text if in foreign content: \\\u003ctitle\u003e generally needs raw text, but a \\\u003ctitle\u003e inside an \\\u003csvg\u003e does not. Another example is that a \\\u003ctextarea\u003e generally needs raw text, but a \\\u003ctextarea\u003e is not allowed as an immediate child of a \\\u003cselect\u003e; in normal parsing, a \\\u003ctextarea\u003e implies \\\u003c/select\u003e, but one cannot close the implicit element when parsing a \\\u003cselect\u003e's InnerHTML. Similarly to AllowCDATA, tracking the correct moment to override raw-text- ness is difficult to do purely in the tokenizer, as opposed to the parser. For strict compliance with the HTML5 tokenization algorithm, it is the responsibility of the user of a tokenizer to call NextIsNotRawText as appropriate. In practice, like AllowCDATA, it is acceptable to ignore this responsibility for basic usage.\n\nNote that this 'raw text' concept is different from the one offered by the Tokenizer.Raw method.\n\n```go\nfunc (z *Tokenizer) Raw() []byte\n```\nRaw returns the unmodified text of the current token. Calling Next, Token, Text, TagName or TagAttr may change the contents of the returned slice.\n\n```go\nfunc (z *Tokenizer) TagAttr() (key, val []byte, moreAttr bool)\n```\nTagAttr returns the lower-cased key and unescaped value of the next unparsed attribute for the current tag token and whether there are more attributes. The contents of the returned slices may change on the next call to Next.\n\n```go\nfunc (z *Tokenizer) TagName() (name []byte, hasAttr bool)\n```\nTagName returns the lower-cased name of a tag token (the \\`img\\` out of \\`\\\u003cIMG SRC=\"foo\"\u003e\\`) and whether the tag has attributes. The contents of the returned slice may change on the next call to Next.\n\n```go\nfunc (z *Tokenizer) Text() []byte\n```\nText returns the unescaped text of a text, comment or doctype token. The contents of the returned slice may change on the next call to Next.\n\n```go\nfunc (z *Tokenizer) Token() Token\n```\nToken returns the next Token. The result's Data and Attr values remain valid after subsequent Next calls.\n\n","path":"github.com/wfernandes/cli/src/code.google.com/p/go.net/html","name":"html","synopsis":"Package html implements an HTML5-compliant tokenizer and parser.","isRedistributable":true}
