Skip to content

Commit

Permalink
added CollectText
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Jul 4, 2024
1 parent fc8ca88 commit d3d7c47
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
18 changes: 18 additions & 0 deletions attr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dom

import (
"bytes"
"strings"

"golang.org/x/net/html"
Expand Down Expand Up @@ -48,3 +49,20 @@ func HasClass(node *html.Node, expectedClass string) bool {
}
return false
}

// - - - - //

func collectText(n *html.Node, buf *bytes.Buffer) {
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(c, buf)
}
}

func CollectText(node *html.Node) string {
var buf bytes.Buffer
collectText(node, &buf)
return buf.String()
}
23 changes: 23 additions & 0 deletions attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dom

import (
"reflect"
"strings"
"testing"

"golang.org/x/net/html"
Expand Down Expand Up @@ -144,3 +145,25 @@ func TestHasClass(t *testing.T) {
t.Error("expected different output")
}
}

func TestCollectText(t *testing.T) {
input := `
<h2 class="article__title">Hello <span>world</span></h2>
<p>Some description</p>
`

doc, err := html.Parse(strings.NewReader(input))
if err != nil {
t.Fatal(err)
}

heading := FindFirstNode(doc, func(node *html.Node) bool {
return HasClass(node, "article__title")
})

expected := "Hello world"
output := CollectText(heading)
if output != expected {
t.Errorf("expected %q but got %q", expected, output)
}
}
45 changes: 45 additions & 0 deletions examples/selectors/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"log"
"strings"

"github.com/JohannesKaufmann/dom"
"golang.org/x/net/html"
)

func main() {
input := `
<h1>Github</h1>
<div>
<h2 class="repo__name">JohannesKaufmann/dom</h2>
<nav>
<h3>Code</h3>
<h3>Issues</h3>
</nav>
</div>
`

doc, err := html.Parse(strings.NewReader(input))
if err != nil {
log.Fatal(err)
}

// - - - //

headingNodes := dom.FindAllNodes(doc, func(node *html.Node) bool {
name := dom.NodeName(node)
return dom.NameIsHeading(name)
})

nameNode := dom.FindFirstNode(doc, func(node *html.Node) bool {
return dom.HasClass(node, "repo__name")
})
repoName := dom.CollectText(nameNode)

fmt.Printf("count:%d name:%q\n", len(headingNodes), repoName)
// count:4 name:"JohannesKaufmann/dom"
}

0 comments on commit d3d7c47

Please sign in to comment.