Skip to content

Commit

Permalink
#66, fix minor bug of preserve white space on Output XML if node or i…
Browse files Browse the repository at this point in the history
…t's parent node has declare a `xml:space="preserve"`.
  • Loading branch information
zhengchun committed Sep 30, 2021
1 parent 3abcaf9 commit 0960309
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 4 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (n *Node) InnerText() string {

func (n *Node) sanitizedData(preserveSpaces bool) string {
if preserveSpaces {
return strings.Trim(n.Data, "\n\t")
return n.Data
}
return strings.TrimSpace(n.Data)
}
Expand Down Expand Up @@ -140,12 +140,13 @@ func outputXML(buf *bytes.Buffer, n *Node, preserveSpaces bool) {

// OutputXML returns the text that including tags name.
func (n *Node) OutputXML(self bool) string {
preserveSpaces := calculatePreserveSpaces(n, false)
var buf bytes.Buffer
if self {
outputXML(&buf, n, false)
outputXML(&buf, n, preserveSpaces)
} else {
for n := n.FirstChild; n != nil; n = n.NextSibling {
outputXML(&buf, n, false)
outputXML(&buf, n, preserveSpaces)
}
}

Expand Down
19 changes: 10 additions & 9 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,16 @@ func TestOutputXMLWithSpaceParent(t *testing.T) {
doc, _ := Parse(strings.NewReader(s))
t.Log(doc.OutputXML(true))

n := FindOne(doc, "/class_list/student/name")
expected := "<name> Robert </name>"
if g := doc.OutputXML(true); strings.Index(g, expected) == -1 {
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
}

output := html.UnescapeString(doc.OutputXML(true))
if strings.Contains(output, "\n") {
t.Errorf("the outputted xml contains newlines")
n := FindOne(doc, "/class_list/student")
output := html.UnescapeString(n.OutputXML(false))
expected = "\n\t\t\t<name> Robert </name>\n\t\t\t<grade>A+</grade>\n\t\t"
if !(output == expected) {
t.Errorf(`expected "%s", obtained "%s"`, expected, output)
}
t.Log(n.OutputXML(false))
}
Expand Down Expand Up @@ -398,20 +399,20 @@ func TestOutputXMLWithSpaceOverwrittenToDefault(t *testing.T) {
doc, _ := Parse(strings.NewReader(s))
t.Log(doc.OutputXML(true))

n := FindOne(doc, "/class_list/student")
expected := `<name xml:space="default">Robert</name>`
if g := doc.OutputXML(false); strings.Index(g, expected) == -1 {
t.Errorf(`expected "%s", obtained "%s"`, expected, g)
}

output := html.UnescapeString(doc.OutputXML(true))
if strings.Contains(output, "\n") {
t.Errorf("the outputted xml contains newlines")
n := FindOne(doc, "/class_list/student")
output := html.UnescapeString(n.OutputXML(false))
expected = "\n\t\t\t<name xml:space=\"default\">Robert</name>\n\t\t\t<grade>A+</grade>\n\t\t"
if !(output == expected) {
t.Errorf(`expected "%s", obtained "%s"`, expected, output)
}
t.Log(n.OutputXML(false))
}


func TestOutputXMLWithXMLInCDATA(t *testing.T) {
s := `<?xml version="1.0" encoding="utf-8"?><node><![CDATA[<greeting>Hello, world!</greeting>]]></node>`
doc, _ := Parse(strings.NewReader(s))
Expand Down

0 comments on commit 0960309

Please sign in to comment.