]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: do not escape newlines
authorRoger Peppe <rogpeppe@gmail.com>
Fri, 24 Apr 2015 16:20:21 +0000 (17:20 +0100)
committerroger peppe <rogpeppe@gmail.com>
Mon, 27 Apr 2015 15:38:04 +0000 (15:38 +0000)
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/9310
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/encoding/xml/marshal.go
src/encoding/xml/marshal_test.go
src/encoding/xml/xml.go

index a0e2058d89d4ff67ae9685696b71d4fe03f0eac3..d0899c0fa6bd1a0b8e07e7d33600d0ea7b737fa4 100644 (file)
@@ -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")
index 8362421db7433f4861a7693133fba384a56baf58..5e9718c20c790dd3b2e099a0d0b51cfb7e98d33c 100644 (file)
@@ -1297,7 +1297,7 @@ var encodeTokenTests = []struct {
        toks: []Token{
                CharData(" \t\n"),
        },
-       want: ` &#x9;&#xA;`,
+       want: " &#x9;\n",
 }, {
        desc: "comment",
        toks: []Token{
index 0c64cd730d6b4270f95242bde2405ea0ec70f620..00792c4f27ceb24a46be355d10436ea6216fb9af 100644 (file)
@@ -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