From e6faf847ccddea703a764af71c5bcfc266d3c91d Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Sat, 1 Sep 2012 10:48:44 -0400 Subject: [PATCH] comment Text() and use bytes.Buffer to concatenate strings --- property.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/property.go b/property.go index 9386008..d1996f3 100644 --- a/property.go +++ b/property.go @@ -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.