mirror of
https://github.com/PuerkitoBio/goquery
synced 2026-04-22 23:37:04 +08:00
add Text() with tests.
This commit is contained in:
+23
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user