Commit 4a3e000a authored by Roger Peppe's avatar Roger Peppe Committed by roger peppe

encoding/xml: do not escape newlines

There is no need to escape newlines in char data -
it makes the XML larger and harder to read.

Change-Id: I1c1fcee1bdffc705c7428f89ca90af8085d6fb73
Reviewed-on: https://go-review.googlesource.com/9310Reviewed-by: default avatarNigel Tao <nigeltao@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent b0b1a660
......@@ -209,7 +209,7 @@ func (enc *Encoder) EncodeToken(t Token) error {
return err
}
case CharData:
EscapeText(p, t)
escapeText(p, t, false)
case Comment:
if bytes.Contains(t, endComment) {
return fmt.Errorf("xml: EncodeToken of Comment containing --> marker")
......
......@@ -1297,7 +1297,7 @@ var encodeTokenTests = []struct {
toks: []Token{
CharData(" \t\n"),
},
want: ` &#x9;&#xA;`,
want: " &#x9;\n",
}, {
desc: "comment",
toks: []Token{
......
......@@ -1863,6 +1863,13 @@ var (
// EscapeText writes to w the properly escaped XML equivalent
// of the plain text data s.
func EscapeText(w io.Writer, s []byte) error {
return escapeText(w, s, true)
}
// escapeText writes to w the properly escaped XML equivalent
// of the plain text data s. If escapeNewline is true, newline
// characters will be escaped.
func escapeText(w io.Writer, s []byte, escapeNewline bool) error {
var esc []byte
last := 0
for i := 0; i < len(s); {
......@@ -1882,6 +1889,9 @@ func EscapeText(w io.Writer, s []byte) error {
case '\t':
esc = esc_tab
case '\n':
if !escapeNewline {
continue
}
esc = esc_nl
case '\r':
esc = esc_cr
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment