From a11b748fa2e4d3244b0a3f28d4ce1647f2ef9997 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Fri, 31 Aug 2012 18:09:31 -0400 Subject: [PATCH] encoding/xml: parse comments in DOCTYPE R=rsc, n13m3y3r CC=golang-dev https://golang.org/cl/6330061 --- src/pkg/encoding/xml/xml.go | 31 +++++++++++++++++++++++++++++- src/pkg/encoding/xml/xml_test.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/pkg/encoding/xml/xml.go b/src/pkg/encoding/xml/xml.go index 623f4178011..fbd2208e334 100644 --- a/src/pkg/encoding/xml/xml.go +++ b/src/pkg/encoding/xml/xml.go @@ -584,6 +584,7 @@ func (d *Decoder) RawToken() (Token, error) { if inquote == 0 && b == '>' && depth == 0 { break } + HandleB: d.buf.WriteByte(b) switch { case b == inquote: @@ -599,7 +600,35 @@ func (d *Decoder) RawToken() (Token, error) { depth-- case b == '<' && inquote == 0: - depth++ + // Look for ]> +]> + --> --> []> +` + +var directivesWithCommentsTokens = []Token{ + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), +} + +func TestDirectivesWithComments(t *testing.T) { + d := NewDecoder(strings.NewReader(directivesWithCommentsInput)) + + for i, want := range directivesWithCommentsTokens { + have, err := d.Token() + if err != nil { + t.Fatalf("token %d: unexpected error: %s", i, err) + } + if !reflect.DeepEqual(have, want) { + t.Errorf("token %d = %#v want %#v", i, have, want) + } + } +}