From a9cfd55e2b09735a25976d1b008a0a3c767494f8 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Tue, 27 Oct 2020 00:21:30 +0100 Subject: [PATCH] encoding/xml: replace comments inside directives with a space A Directive (like ) can't have other nodes nested inside it (in our data structure representation), so there is no way to preserve comments. The previous behavior was to just elide them, which however might change the semantic meaning of the surrounding markup. Instead, replace them with a space which hopefully has the same semantic effect of the comment. Directives are not actually a node type in the XML spec, which instead specifies each of them separately ( TryBot-Result: Go Bot Trust: Filippo Valsorda Reviewed-by: Katie Hockman --- src/encoding/xml/xml.go | 6 ++++++ src/encoding/xml/xml_test.go | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index c902f1295a..c14954df15 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -768,6 +768,12 @@ func (d *Decoder) rawToken() (Token, error) { } b0, b1 = b1, b } + + // Replace the comment with a space in the returned Directive + // body, so that markup parts that were separated by the comment + // (like a "<" and a "!") don't get joined when re-encoding the + // Directive, taking new semantic meaning. + d.buf.WriteByte(' ') } } return Directive(d.buf.Bytes()), nil diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go index 47d0c39167..19152dbdb6 100644 --- a/src/encoding/xml/xml_test.go +++ b/src/encoding/xml/xml_test.go @@ -802,11 +802,11 @@ var directivesWithCommentsInput = ` var directivesWithCommentsTokens = []Token{ CharData("\n"), - Directive(`DOCTYPE []`), + Directive(`DOCTYPE [ ]`), CharData("\n"), - Directive(`DOCTYPE []`), + Directive(`DOCTYPE [ ]`), CharData("\n"), - Directive(`DOCTYPE []`), + Directive(`DOCTYPE [ ]`), CharData("\n"), } @@ -1051,9 +1051,10 @@ func testRoundTrip(t *testing.T, input string) { func TestRoundTrip(t *testing.T) { tests := map[string]string{ - "leading colon": `<::Test ::foo="bar"><:::Hello>`, - "trailing colon": ``, - "double colon": ``, + "leading colon": `<::Test ::foo="bar"><:::Hello>`, + "trailing colon": ``, + "double colon": ``, + "comments in directives": `--x --> > --x ]>`, } for name, input := range tests { t.Run(name, func(t *testing.T) { testRoundTrip(t, input) }) -- 2.48.1