package goquery import ( "io" "strings" "golang.org/x/net/html" ) // used to determine if a set (map[*html.Node]bool) should be used // instead of iterating over a slice. The set uses more memory and // is slower than slice iteration for small N. const minNodesForSet = 1000 var nodeNames = []string{ html.ErrorNode: "#error", html.TextNode: "#text", html.DocumentNode: "#document", html.CommentNode: "#comment", } // NodeName returns the node name of the first element in the selection. // It tries to behave in a similar way as the DOM's nodeName property // (https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName). // // Go's net/html package defines the following node types, listed with // the corresponding returned value from this function: // // ErrorNode : #error // TextNode : #text // DocumentNode : #document // ElementNode : the element's tag name // CommentNode : #comment // DoctypeNode : the name of the document type func NodeName(s *Selection) string { if s.Length() == 0 { return "" } return nodeName(s.Get(0)) } // nodeName returns the node name of the given html node. // See NodeName for additional details on behaviour. func nodeName(node *html.Node) string { if node == nil { return "" } switch node.Type { case html.ElementNode, html.DoctypeNode: return node.Data default: if int(node.Type) < len(nodeNames) { return nodeNames[node.Type] } return "" } } // TextOptions controls the behaviour of the Text function. type TextOptions struct { // Separator is inserted between the contents of the text nodes that are // included in the result. It defaults to the empty string, which // concatenates the text nodes without any separator. Separator string // Trim, when true, removes leading and trailing whitespace from the // content of each text node and omits text nodes that are empty once // trimmed. This is useful to discard the insignificant whitespace that // comes from the indentation of the source HTML. Trim bool // Keep, when non-nil, is called for each text node encountered while // traversing the selection. The content of the text node is included in // the result only if Keep returns true. It can be used, for example, to // drop the text of