11package xmlquery
22
33import (
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.
8584func (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
104103func (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'.
0 commit comments