Skip to content

Commit 9dbfa11

Browse files
committed
PR #94
2 parents 94cb5ae + b9c0fac commit 9dbfa11

3 files changed

Lines changed: 44 additions & 45 deletions

File tree

node.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package xmlquery
22

33
import (
4-
"bytes"
54
"encoding/xml"
65
"fmt"
76
"html"
@@ -83,22 +82,22 @@ func WithoutComments() OutputOption {
8382

8483
// InnerText returns the text between the start and end tags of the object.
8584
func (n *Node) InnerText() string {
86-
var output func(*bytes.Buffer, *Node)
87-
output = func(buf *bytes.Buffer, n *Node) {
85+
var output func(*strings.Builder, *Node)
86+
output = func(b *strings.Builder, n *Node) {
8887
switch n.Type {
8988
case TextNode, CharDataNode:
90-
buf.WriteString(n.Data)
89+
b.WriteString(n.Data)
9190
case CommentNode:
9291
default:
9392
for child := n.FirstChild; child != nil; child = child.NextSibling {
94-
output(buf, child)
93+
output(b, child)
9594
}
9695
}
9796
}
9897

99-
var buf bytes.Buffer
100-
output(&buf, n)
101-
return buf.String()
98+
var b strings.Builder
99+
output(&b, n)
100+
return b.String()
102101
}
103102

104103
func (n *Node) sanitizedData(preserveSpaces bool) string {
@@ -117,62 +116,62 @@ func calculatePreserveSpaces(n *Node, pastValue bool) bool {
117116
return pastValue
118117
}
119118

120-
func outputXML(buf *bytes.Buffer, n *Node, preserveSpaces bool, config *outputConfiguration) {
119+
func outputXML(b *strings.Builder, n *Node, preserveSpaces bool, config *outputConfiguration) {
121120
preserveSpaces = calculatePreserveSpaces(n, preserveSpaces)
122121
switch n.Type {
123122
case TextNode:
124-
buf.WriteString(html.EscapeString(n.sanitizedData(preserveSpaces)))
123+
b.WriteString(html.EscapeString(n.sanitizedData(preserveSpaces)))
125124
return
126125
case CharDataNode:
127-
buf.WriteString("<![CDATA[")
128-
buf.WriteString(n.Data)
129-
buf.WriteString("]]>")
126+
b.WriteString("<![CDATA[")
127+
b.WriteString(n.Data)
128+
b.WriteString("]]>")
130129
return
131130
case CommentNode:
132131
if !config.skipComments {
133-
buf.WriteString("<!--")
134-
buf.WriteString(n.Data)
135-
buf.WriteString("-->")
132+
b.WriteString("<!--")
133+
b.WriteString(n.Data)
134+
b.WriteString("-->")
136135
}
137136
return
138137
case DeclarationNode:
139-
buf.WriteString("<?" + n.Data)
138+
b.WriteString("<?" + n.Data)
140139
default:
141140
if n.Prefix == "" {
142-
buf.WriteString("<" + n.Data)
141+
b.WriteString("<" + n.Data)
143142
} else {
144-
buf.WriteString("<" + n.Prefix + ":" + n.Data)
143+
b.WriteString("<" + n.Prefix + ":" + n.Data)
145144
}
146145
}
147146

148147
for _, attr := range n.Attr {
149148
if attr.Name.Space != "" {
150-
buf.WriteString(fmt.Sprintf(` %s:%s=`, attr.Name.Space, attr.Name.Local))
149+
b.WriteString(fmt.Sprintf(` %s:%s=`, attr.Name.Space, attr.Name.Local))
151150
} else {
152-
buf.WriteString(fmt.Sprintf(` %s=`, attr.Name.Local))
151+
b.WriteString(fmt.Sprintf(` %s=`, attr.Name.Local))
153152
}
154-
buf.WriteByte('"')
155-
buf.WriteString(html.EscapeString(attr.Value))
156-
buf.WriteByte('"')
153+
b.WriteByte('"')
154+
b.WriteString(html.EscapeString(attr.Value))
155+
b.WriteByte('"')
157156
}
158157
if n.Type == DeclarationNode {
159-
buf.WriteString("?>")
158+
b.WriteString("?>")
160159
} else {
161160
if n.FirstChild != nil || !config.emptyElementTagSupport {
162-
buf.WriteString(">")
161+
b.WriteString(">")
163162
} else {
164-
buf.WriteString("/>")
163+
b.WriteString("/>")
165164
return
166165
}
167166
}
168167
for child := n.FirstChild; child != nil; child = child.NextSibling {
169-
outputXML(buf, child, preserveSpaces, config)
168+
outputXML(b, child, preserveSpaces, config)
170169
}
171170
if n.Type != DeclarationNode {
172171
if n.Prefix == "" {
173-
buf.WriteString(fmt.Sprintf("</%s>", n.Data))
172+
b.WriteString(fmt.Sprintf("</%s>", n.Data))
174173
} else {
175-
buf.WriteString(fmt.Sprintf("</%s:%s>", n.Prefix, n.Data))
174+
b.WriteString(fmt.Sprintf("</%s:%s>", n.Prefix, n.Data))
176175
}
177176
}
178177
}
@@ -185,16 +184,16 @@ func (n *Node) OutputXML(self bool) string {
185184
emptyElementTagSupport: false,
186185
}
187186
preserveSpaces := calculatePreserveSpaces(n, false)
188-
var buf bytes.Buffer
187+
var b strings.Builder
189188
if self && n.Type != DocumentNode {
190-
outputXML(&buf, n, preserveSpaces, config)
189+
outputXML(&b, n, preserveSpaces, config)
191190
} else {
192191
for n := n.FirstChild; n != nil; n = n.NextSibling {
193-
outputXML(&buf, n, preserveSpaces, config)
192+
outputXML(&b, n, preserveSpaces, config)
194193
}
195194
}
196195

197-
return buf.String()
196+
return b.String()
198197
}
199198

200199
// OutputXMLWithOptions returns the text that including tags name.
@@ -207,16 +206,16 @@ func (n *Node) OutputXMLWithOptions(opts ...OutputOption) string {
207206
}
208207

209208
preserveSpaces := calculatePreserveSpaces(n, false)
210-
var buf bytes.Buffer
209+
var b strings.Builder
211210
if config.printSelf && n.Type != DocumentNode {
212-
outputXML(&buf, n, preserveSpaces, config)
211+
outputXML(&b, n, preserveSpaces, config)
213212
} else {
214213
for n := n.FirstChild; n != nil; n = n.NextSibling {
215-
outputXML(&buf, n, preserveSpaces, config)
214+
outputXML(&b, n, preserveSpaces, config)
216215
}
217216
}
218217

219-
return buf.String()
218+
return b.String()
220219
}
221220

222221
// AddAttr adds a new attribute specified by 'key' and 'val' to a node 'n'.

node_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,12 @@ func TestOutputXMLWithCommentNode(t *testing.T) {
437437
</class_list>`
438438
doc, _ := Parse(strings.NewReader(s))
439439
t.Log(doc.OutputXML(true))
440-
if e, g := "<!-- Students grades are updated bi-monthly -->", doc.OutputXML(true); strings.Index(g, e) == -1 {
440+
if e, g := "<!-- Students grades are updated bi-monthly -->", doc.OutputXML(true); !strings.Contains(g, e) {
441441
t.Fatal("missing some comment-node.")
442442
}
443443
n := FindOne(doc, "//class_list")
444444
t.Log(n.OutputXML(false))
445-
if e, g := "<name>Lenard</name>", n.OutputXML(false); strings.Index(g, e) == -1 {
445+
if e, g := "<name>Lenard</name>", n.OutputXML(false); !strings.Contains(g, e) {
446446
t.Fatal("missing some comment-node")
447447
}
448448
}
@@ -459,7 +459,7 @@ func TestOutputXMLWithSpaceParent(t *testing.T) {
459459
t.Log(doc.OutputXML(true))
460460

461461
expected := "<name> Robert </name>"
462-
if g := doc.OutputXML(true); strings.Index(g, expected) == -1 {
462+
if g := doc.OutputXML(true); !strings.Contains(g, expected) {
463463
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
464464
}
465465

@@ -485,7 +485,7 @@ func TestOutputXMLWithSpaceDirect(t *testing.T) {
485485

486486
n := FindOne(doc, "/class_list/student/name")
487487
expected := `<name xml:space="preserve"> Robert </name>`
488-
if g := doc.OutputXML(false); strings.Index(g, expected) == -1 {
488+
if g := doc.OutputXML(false); !strings.Contains(g, expected) {
489489
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
490490
}
491491

@@ -509,7 +509,7 @@ func TestOutputXMLWithSpaceOverwrittenToPreserve(t *testing.T) {
509509

510510
n := FindOne(doc, "/class_list/student")
511511
expected := `<name xml:space="preserve"> Robert </name>`
512-
if g := n.OutputXML(false); strings.Index(g, expected) == -1 {
512+
if g := n.OutputXML(false); !strings.Contains(g, expected) {
513513
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
514514
}
515515

@@ -532,7 +532,7 @@ func TestOutputXMLWithSpaceOverwrittenToDefault(t *testing.T) {
532532
t.Log(doc.OutputXML(true))
533533

534534
expected := `<name xml:space="default">Robert</name>`
535-
if g := doc.OutputXML(false); strings.Index(g, expected) == -1 {
535+
if g := doc.OutputXML(false); !strings.Contains(g, expected) {
536536
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
537537
}
538538

parse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestNamespaceURL(t *testing.T) {
116116
if strings.Index(top.InnerText(), "author") > 0 {
117117
t.Fatalf("InnerText() include comment node text")
118118
}
119-
if strings.Index(top.OutputXML(true), "author") == -1 {
119+
if !strings.Contains(top.OutputXML(true), "author") {
120120
t.Fatal("OutputXML shoud include comment node,but not")
121121
}
122122
}

0 commit comments

Comments
 (0)