Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<!-- a --><!DOCTYPE r><!-- b --><r><!-- c --></r><!-- d -->`
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 := "<!--x-->\r\n<a>t</a>\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)
}
}