Skip to content

Commit e28092b

Browse files
committed
Merge PR #81. Keep \t and \n output when called OutputXML()
close #73
2 parents 8e1a11d + 99bb88d commit e28092b

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

node.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/xml"
66
"fmt"
7+
"html"
78
"strings"
89
)
910

@@ -89,7 +90,7 @@ func outputXML(buf *bytes.Buffer, n *Node, preserveSpaces bool) {
8990
preserveSpaces = calculatePreserveSpaces(n, preserveSpaces)
9091
switch n.Type {
9192
case TextNode:
92-
xml.EscapeText(buf, []byte(n.sanitizedData(preserveSpaces)))
93+
buf.WriteString(html.EscapeString(n.sanitizedData(preserveSpaces)))
9394
return
9495
case CharDataNode:
9596
buf.WriteString("<![CDATA[")
@@ -118,7 +119,7 @@ func outputXML(buf *bytes.Buffer, n *Node, preserveSpaces bool) {
118119
buf.WriteString(fmt.Sprintf(` %s=`, attr.Name.Local))
119120
}
120121
buf.WriteByte('"')
121-
xml.EscapeText(buf, []byte(attr.Value))
122+
buf.WriteString(html.EscapeString(attr.Value))
122123
buf.WriteByte('"')
123124
}
124125
if n.Type == DeclarationNode {

node_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,53 @@ func TestEscapeOutputValue(t *testing.T) {
280280

281281
}
282282

283+
func TestUnnecessaryEscapeOutputValue(t *testing.T) {
284+
data := `<?xml version="1.0" encoding="utf-8"?>
285+
<class_list xml:space="preserve">
286+
<student>
287+
<name> Robert </name>
288+
<grade>A+</grade>
289+
290+
</student>
291+
</class_list>`
292+
293+
root, err := Parse(strings.NewReader(data))
294+
if err != nil {
295+
t.Error(err)
296+
}
297+
298+
escapedInnerText := root.OutputXML(true)
299+
if strings.Contains(escapedInnerText, "&#x9") {
300+
t.Fatal("\\n has been escaped unnecessarily")
301+
}
302+
303+
if strings.Contains(escapedInnerText, "&#xA") {
304+
t.Fatal("\\t has been escaped unnecessarily")
305+
}
306+
307+
}
308+
309+
func TestHtmlUnescapeStringOriginString(t *testing.T) {
310+
// has escape html character and \t
311+
data := `<?xml version="1.0" encoding="utf-8"?>
312+
<example xml:space="preserve"><word>&amp;#48; </word></example>`
313+
314+
root, err := Parse(strings.NewReader(data))
315+
if err != nil {
316+
t.Error(err)
317+
}
318+
319+
escapedInnerText := root.OutputXML(false)
320+
unescapeString := html.UnescapeString(escapedInnerText)
321+
if strings.Contains(unescapeString, "&amp;") {
322+
t.Fatal("&amp; need unescape")
323+
}
324+
if !strings.Contains(escapedInnerText, "&amp;#48;\t\t") {
325+
t.Fatal("Inner Text should keep plain text")
326+
}
327+
328+
}
329+
283330
func TestOutputXMLWithNamespacePrefix(t *testing.T) {
284331
s := `<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body></S:Body></S:Envelope>`
285332
doc, _ := Parse(strings.NewReader(s))

0 commit comments

Comments
 (0)