From 6b36d629f0d6d68cbe53e6d43cf9079bd9c0eea4 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Sat, 1 Sep 2012 10:45:47 -0400 Subject: [PATCH] add Text() with tests. --- property.go | 23 +++++++++++++++++++++++ property_test.go | 30 ++++++++++++++++++++++++++++++ type_test.go | 11 +++++++++++ 3 files changed, 64 insertions(+) diff --git a/property.go b/property.go index 211f9e0..9386008 100644 --- a/property.go +++ b/property.go @@ -1,6 +1,7 @@ package goquery import ( + "bytes" "exp/html" ) @@ -14,6 +15,15 @@ func (this *Selection) Attr(attrName string) (val string, exists bool) { return getAttributeValue(attrName, this.Nodes[0]) } +func (this *Selection) Text() string { + var buf bytes.Buffer + + for _, n := range this.Nodes { + buf.WriteString(getNodeText(n)) + } + return buf.String() +} + // Size() is an alias for Length(). func (this *Selection) Size() int { return this.Length() @@ -24,6 +34,19 @@ func (this *Selection) Length() int { return len(this.Nodes) } +func getNodeText(node *html.Node) (ret string) { + if node.Type == html.TextNode { + //ret = strings.Trim(node.Data, " \t\r\n") + // Keep newlines and spaces, like jQuery + ret = node.Data + } else if len(node.Child) > 0 { + for _, c := range node.Child { + ret += getNodeText(c) + } + } + return +} + // Private function to get the specified attribute's value from a node. func getAttributeValue(attrName string, n *html.Node) (val string, exists bool) { if n == nil { diff --git a/property_test.go b/property_test.go index d15c704..dbd68ef 100644 --- a/property_test.go +++ b/property_test.go @@ -1,6 +1,8 @@ package goquery import ( + "regexp" + "strings" "testing" ) @@ -19,3 +21,31 @@ func TestAttrNotExist(t *testing.T) { t.Errorf("Expected no value for the href attribute, got %v.", val) } } + +func TestText(t *testing.T) { + txt := Doc().Find("h1").Text() + if strings.Trim(txt, " \n\r\t") != "Provok.in" { + t.Errorf("Expected text to be Provok.in, found %s.", txt) + } +} + +func TestText2(t *testing.T) { + txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text() + if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil { + t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt) + if e != nil { + t.Logf("Error: %s.", e.Error()) + } + } +} + +/*func TestText3(t *testing.T) { + txt := Doc().Find(".pvk-gutter").First().Text() + if ok, e := regexp.MatchString(`^\s+$`, txt); !ok || e != nil { + t.Errorf("Expected spaces, found %v.", txt) + if e != nil { + t.Logf("Error: %s.", e.Error()) + } + } +} +*/ diff --git a/type_test.go b/type_test.go index 507954e..b7305b5 100644 --- a/type_test.go +++ b/type_test.go @@ -28,6 +28,17 @@ func EnsureDocLoaded() { } } +func printNode(n *html.Node, t *testing.T) { + t.Logf("Type: %v, Data: %v\n", n.Type, n.Data) + for _, c := range n.Child { + printNode(c, t) + } +} + +func TestPrintAll(t *testing.T) { + //printNode(Doc().Root, t) +} + func TestNewDocument(t *testing.T) { if f, e := os.Open("./testdata/page.html"); e != nil { t.Error(e.Error())