comment Text() and use bytes.Buffer to concatenate strings

This commit is contained in:
Martin Angers
2012-09-01 10:48:44 -04:00
parent 6b36d629f0
commit e6faf847cc
+10 -4
View File
@@ -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.