@@ -80,6 +80,18 @@ func WithoutComments() OutputOption {
8080 }
8181}
8282
83+ func newXMLName (name string ) xml.Name {
84+ if i := strings .IndexByte (name , ':' ); i > 0 {
85+ return xml.Name {
86+ Space : name [:i ],
87+ Local : name [i + 1 :],
88+ }
89+ }
90+ return xml.Name {
91+ Local : name ,
92+ }
93+ }
94+
8395// InnerText returns the text between the start and end tags of the object.
8496func (n * Node ) InnerText () string {
8597 var output func (* strings.Builder , * Node )
@@ -140,15 +152,15 @@ func outputXML(b *strings.Builder, n *Node, preserveSpaces bool, config *outputC
140152 if n .Prefix == "" {
141153 b .WriteString ("<" + n .Data )
142154 } else {
143- b . WriteString ( "<" + n .Prefix + ":" + n .Data )
155+ fmt . Fprintf ( b , "<%s:%s" , n .Prefix , n .Data )
144156 }
145157 }
146158
147159 for _ , attr := range n .Attr {
148160 if attr .Name .Space != "" {
149- b . WriteString ( fmt .Sprintf ( ` %s:%s=` , attr .Name .Space , attr .Name .Local ) )
161+ fmt .Fprintf ( b , ` %s:%s=` , attr .Name .Space , attr .Name .Local )
150162 } else {
151- b . WriteString ( fmt .Sprintf ( ` %s=` , attr .Name .Local ) )
163+ fmt .Fprintf ( b , ` %s=` , attr .Name .Local )
152164 }
153165 b .WriteByte ('"' )
154166 b .WriteString (html .EscapeString (attr .Value ))
@@ -169,9 +181,9 @@ func outputXML(b *strings.Builder, n *Node, preserveSpaces bool, config *outputC
169181 }
170182 if n .Type != DeclarationNode {
171183 if n .Prefix == "" {
172- b . WriteString ( fmt .Sprintf ( "</%s>" , n .Data ) )
184+ fmt .Fprintf ( b , "</%s>" , n .Data )
173185 } else {
174- b . WriteString ( fmt .Sprintf ( "</%s:%s>" , n .Prefix , n .Data ) )
186+ fmt .Fprintf ( b , "</%s:%s>" , n .Prefix , n .Data )
175187 }
176188 }
177189}
@@ -220,69 +232,35 @@ func (n *Node) OutputXMLWithOptions(opts ...OutputOption) string {
220232
221233// AddAttr adds a new attribute specified by 'key' and 'val' to a node 'n'.
222234func AddAttr (n * Node , key , val string ) {
223- var attr Attr
224- if i := strings .Index (key , ":" ); i > 0 {
225- attr = Attr {
226- Name : xml.Name {Space : key [:i ], Local : key [i + 1 :]},
227- Value : val ,
228- }
229- } else {
230- attr = Attr {
231- Name : xml.Name {Local : key },
232- Value : val ,
233- }
235+ attr := Attr {
236+ Name : newXMLName (key ),
237+ Value : val ,
234238 }
235-
236239 n .Attr = append (n .Attr , attr )
237240}
238241
239242// SetAttr allows an attribute value with the specified name to be changed.
240243// If the attribute did not previously exist, it will be created.
241244func (n * Node ) SetAttr (key , value string ) {
242- if i := strings .Index (key , ":" ); i > 0 {
243- space := key [:i ]
244- local := key [i + 1 :]
245- for idx := 0 ; idx < len (n .Attr ); idx ++ {
246- if n .Attr [idx ].Name .Space == space && n .Attr [idx ].Name .Local == local {
247- n .Attr [idx ].Value = value
248- return
249- }
250- }
251-
252- AddAttr (n , key , value )
253- } else {
254- for idx := 0 ; idx < len (n .Attr ); idx ++ {
255- if n .Attr [idx ].Name .Local == key {
256- n .Attr [idx ].Value = value
257- return
258- }
245+ name := newXMLName (key )
246+ for i , attr := range n .Attr {
247+ if attr .Name == name {
248+ n .Attr [i ].Value = value
249+ return
259250 }
260-
261- AddAttr (n , key , value )
262251 }
252+ AddAttr (n , key , value )
263253}
264254
265255// RemoveAttr removes the attribute with the specified name.
266256func (n * Node ) RemoveAttr (key string ) {
267- removeIdx := - 1
268- if i := strings .Index (key , ":" ); i > 0 {
269- space := key [:i ]
270- local := key [i + 1 :]
271- for idx := 0 ; idx < len (n .Attr ); idx ++ {
272- if n .Attr [idx ].Name .Space == space && n .Attr [idx ].Name .Local == local {
273- removeIdx = idx
274- }
275- }
276- } else {
277- for idx := 0 ; idx < len (n .Attr ); idx ++ {
278- if n .Attr [idx ].Name .Local == key {
279- removeIdx = idx
280- }
257+ name := newXMLName (key )
258+ for i , attr := range n .Attr {
259+ if attr .Name == name {
260+ n .Attr = append (n .Attr [:i ], n .Attr [i + 1 :]... )
261+ return
281262 }
282263 }
283- if removeIdx != - 1 {
284- n .Attr = append (n .Attr [:removeIdx ], n .Attr [removeIdx + 1 :]... )
285- }
286264}
287265
288266// AddChild adds a new node 'n' to a node 'parent' as its last child.
0 commit comments