From 446c668b07819a63e5875aff748fc094ffdbb8a8 Mon Sep 17 00:00:00 2001 From: yxshee <84263676+yxshee@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:06:54 +0530 Subject: [PATCH] Keep comments and DOCTYPE that precede the root when there is no declaration A comment or directive seen while p.prev was still the document node was added with AddSibling(p.prev, ...), which makes it a sibling of the document node itself, so it was dropped from the tree. A processing instruction in the same position already becomes a child of the document; do the same for comments and directives. --- parse.go | 19 +++++++++++++++++-- parse_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/parse.go b/parse.go index e84bcff..f4dc821 100644 --- a/parse.go +++ b/parse.go @@ -331,7 +331,15 @@ func (p *parser) parse() (*Node, error) { } case xml.Comment: node := &Node{Type: CommentNode, Data: string(tok), level: p.level, LineNumber: lineNumber} - if p.level == p.prev.level { + if p.prev.Type == DocumentNode { + // Before any declaration or element the comment belongs to the + // document, like a processing instruction in that position. + // Attaching it as a sibling of the document node would lose it. + node.level = 1 + AddChild(p.prev, node) + p.prev = node + p.level = 1 + } else if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) @@ -372,7 +380,14 @@ func (p *parser) parse() (*Node, error) { p.level = level case xml.Directive: node := &Node{Type: NotationNode, Data: string(tok), level: p.level, LineNumber: lineNumber} - if p.level == p.prev.level { + if p.prev.Type == DocumentNode { + // Same as for a comment: a DOCTYPE before any declaration or + // element is a child of the document, not a sibling of it. + node.level = 1 + AddChild(p.prev, node) + p.prev = node + p.level = 1 + } else if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) diff --git a/parse_test.go b/parse_test.go index 6c2a658..276b91e 100644 --- a/parse_test.go +++ b/parse_test.go @@ -766,3 +766,52 @@ func TestProcessingInstructionStructure(t *testing.T) { } }) } + +func TestLeadingCommentAndDoctypeWithoutDeclaration(t *testing.T) { + // Without an XML declaration, a comment or DOCTYPE before the document + // element was attached as a sibling of the document node and lost. + s := `` + doc, err := Parse(strings.NewReader(s)) + if err != nil { + t.Fatal(err) + } + var types []NodeType + for n := doc.FirstChild; n != nil; n = n.NextSibling { + types = append(types, n.Type) + } + want := []NodeType{CommentNode, NotationNode, CommentNode, ElementNode, CommentNode} + if len(types) != len(want) { + t.Fatalf("expected document children %v, got %v", want, types) + } + for i := range want { + if types[i] != want[i] { + t.Fatalf("expected document children %v, got %v", want, types) + } + } + if got := doc.OutputXML(true); got != s { + t.Errorf("expected %s, got %s", s, got) + } + if n := len(Find(doc, "//comment()")); n != 4 { + t.Errorf("expected 4 comments, got %d", n) + } + if n := FindOne(doc, "/r"); n == nil || n.FirstChild == nil || n.FirstChild.Type != CommentNode { + t.Errorf("expected /r to keep its comment child") + } +} + +func TestLeadingCommentWithWhitespace(t *testing.T) { + s := "\r\nt\n" + doc, err := Parse(strings.NewReader(s)) + if err != nil { + t.Fatal(err) + } + if doc.FirstChild == nil || doc.FirstChild.Type != CommentNode || doc.FirstChild.Data != "x" { + t.Fatalf("expected the leading comment as the first document child, got %v", doc.OutputXML(true)) + } + if got, want := doc.FirstChild.NextSibling.NextSibling.Data, "a"; got != want { + t.Errorf("expected the element after the comment and the whitespace, got %q", got) + } + if got := FindOne(doc, "/a").InnerText(); got != "t" { + t.Errorf("expected t, got %q", got) + } +}