Skip to content

Commit

Permalink
Fix #94: Preserve CDATA for unsafe content
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Aug 23, 2024
1 parent 418966b commit a0c4e1e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
_, _ = fmt.Fprint(writer, tagColor(">"))
startTagClosed = true
}
if hasContent && (strings.Contains(str, "&") || strings.Contains(str, "<")) {
str = "<![CDATA[" + str + "]]>"
}
_, _ = fmt.Fprint(writer, str)
case xml.Comment:
if !startTagClosed {
Expand Down
1 change: 1 addition & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestFormatXml(t *testing.T) {
"unformatted12.xml": "formatted12.xml",
"unformatted13.xml": "formatted13.xml",
"unformatted14.xml": "formatted14.xml",
"unformatted15.xml": "formatted15.xml",
}

for unformattedFile, expectedFile := range files {
Expand Down
2 changes: 2 additions & 0 deletions test/data/xml/formatted15.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Top><![CDATA[Ding&Dong=]]></Top>
1 change: 1 addition & 0 deletions test/data/xml/unformatted15.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><Top><![CDATA[Ding&Dong=]]></Top>

0 comments on commit a0c4e1e

Please sign in to comment.