mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
comment Text() and use bytes.Buffer to concatenate strings
This commit is contained in:
+10
-4
@@ -15,6 +15,8 @@ func (this *Selection) Attr(attrName string) (val string, exists bool) {
|
||||
return getAttributeValue(attrName, this.Nodes[0])
|
||||
}
|
||||
|
||||
// Text() gets the combined text contents of each element in the set of matched
|
||||
// elements, including their descendants.
|
||||
func (this *Selection) Text() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
@@ -34,17 +36,21 @@ func (this *Selection) Length() int {
|
||||
return len(this.Nodes)
|
||||
}
|
||||
|
||||
func getNodeText(node *html.Node) (ret string) {
|
||||
// Get the specified node's text content.
|
||||
func getNodeText(node *html.Node) string {
|
||||
if node.Type == html.TextNode {
|
||||
//ret = strings.Trim(node.Data, " \t\r\n")
|
||||
// Keep newlines and spaces, like jQuery
|
||||
ret = node.Data
|
||||
return node.Data
|
||||
} else if len(node.Child) > 0 {
|
||||
var buf bytes.Buffer
|
||||
for _, c := range node.Child {
|
||||
ret += getNodeText(c)
|
||||
buf.WriteString(getNodeText(c))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
return
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Private function to get the specified attribute's value from a node.
|
||||
|
||||
Reference in New Issue
Block a user