From 36d3714531989c21d10233a2f69135071263120c Mon Sep 17 00:00:00 2001 From: egginabucket Date: Tue, 6 Dec 2022 10:03:05 -0800 Subject: [PATCH 1/3] remove redundant Fprintln newline --- query_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query_test.go b/query_test.go index 88741fa..d819a98 100644 --- a/query_test.go +++ b/query_test.go @@ -69,7 +69,7 @@ func TestSelectorCache(t *testing.T) { func TestLoadURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, htmlSample) + fmt.Fprint(w, htmlSample) })) defer ts.Close() From 7103bc61b6b08285d4be4659621de09e9f10209c Mon Sep 17 00:00:00 2001 From: egginabucket Date: Tue, 6 Dec 2022 10:03:54 -0800 Subject: [PATCH 2/3] replace strings.Index == -1 with !Contains --- query_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query_test.go b/query_test.go index d819a98..776cbc5 100644 --- a/query_test.go +++ b/query_test.go @@ -136,7 +136,7 @@ func TestXPath(t *testing.T) { if strings.Index(InnerText(node), "Logo") > 0 { t.Fatal("InnerText() have comment node text") } - if strings.Index(OutputHTML(node, true), "Logo") == -1 { + if !strings.Contains(OutputHTML(node, true), "Logo") { t.Fatal("OutputHTML() shoud have comment node text") } link := FindOne(testDoc, "//a[1]/@href") From 6eee01998cc824123ab6ac7572b5cc74545faa90 Mon Sep 17 00:00:00 2001 From: egginabucket Date: Tue, 6 Dec 2022 10:05:06 -0800 Subject: [PATCH 3/3] replace bytes.Buffer with strings.Builder --- query.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/query.go b/query.go index f6742f5..f90f31c 100644 --- a/query.go +++ b/query.go @@ -5,11 +5,11 @@ package htmlquery import ( "bufio" - "bytes" "fmt" "io" "net/http" "os" + "strings" "github.com/antchfx/xpath" "golang.org/x/net/html" @@ -138,23 +138,23 @@ func Parse(r io.Reader) (*html.Node, error) { // InnerText returns the text between the start and end tags of the object. func InnerText(n *html.Node) string { - var output func(*bytes.Buffer, *html.Node) - output = func(buf *bytes.Buffer, n *html.Node) { + var output func(*strings.Builder, *html.Node) + output = func(b *strings.Builder, n *html.Node) { switch n.Type { case html.TextNode: - buf.WriteString(n.Data) + b.WriteString(n.Data) return case html.CommentNode: return } for child := n.FirstChild; child != nil; child = child.NextSibling { - output(buf, child) + output(b, child) } } - var buf bytes.Buffer - output(&buf, n) - return buf.String() + var b strings.Builder + output(&b, n) + return b.String() } // SelectAttr returns the attribute value with the specified name. @@ -189,15 +189,15 @@ func ExistsAttr(n *html.Node, name string) bool { // OutputHTML returns the text including tags name. func OutputHTML(n *html.Node, self bool) string { - var buf bytes.Buffer + var b strings.Builder if self { - html.Render(&buf, n) + html.Render(&b, n) } else { for n := n.FirstChild; n != nil; n = n.NextSibling { - html.Render(&buf, n) + html.Render(&b, n) } } - return buf.String() + return b.String() } type NodeNavigator struct {