mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
Merge pull request #561 from jvoisin/noclosure
perf: speed up Selection.Text by outlining recursion
This commit is contained in:
+13
-15
@@ -59,27 +59,25 @@ func (s *Selection) SetAttr(attrName, val string) *Selection {
|
||||
// elements, including their descendants.
|
||||
func (s *Selection) Text() string {
|
||||
var builder strings.Builder
|
||||
|
||||
// Slightly optimized vs calling Each: no single selection object created
|
||||
var f func(*html.Node)
|
||||
f = func(n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
// Keep newlines and spaces, like jQuery
|
||||
builder.WriteString(n.Data)
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
f(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, n := range s.Nodes {
|
||||
f(n)
|
||||
s.textHelper(n, &builder)
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func (s *Selection) textHelper(n *html.Node, builder *strings.Builder) {
|
||||
if n.Type == html.TextNode {
|
||||
// Keep newlines and spaces, like jQuery
|
||||
builder.WriteString(n.Data)
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
s.textHelper(c, builder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Size is an alias for Length.
|
||||
func (s *Selection) Size() int {
|
||||
return s.Length()
|
||||
|
||||
Reference in New Issue
Block a user