diff --git a/node.go b/node.go index 24b7259..d0e6a54 100644 --- a/node.go +++ b/node.go @@ -68,10 +68,16 @@ func (n *Node) InnerText() string { } func outputXML(buf *bytes.Buffer, n *Node) { - if n.Type == TextNode || n.Type == CommentNode { + if n.Type == TextNode { xml.EscapeText(buf, []byte(strings.TrimSpace(n.Data))) return } + if n.Type == CommentNode { + buf.WriteString("") + return + } if n.Type == DeclarationNode { buf.WriteString(" prev.level { addChild(prev, node) + } else if level < prev.level { + for i := prev.level - level; i > 1; i-- { + prev = prev.Parent + } + addSibling(prev.Parent, node) } case xml.ProcInst: // Processing Instruction if prev.Type != DeclarationNode { diff --git a/node_test.go b/node_test.go index c046c24..1936958 100644 --- a/node_test.go +++ b/node_test.go @@ -317,3 +317,30 @@ func TestAttributeWithNamespace(t *testing.T) { t.Fatal("n is nil") } } + +func TestOutputXMLWithCommentNode(t *testing.T) { + s := ` + + + + Robert + A+ + + + ` + doc, _ := Parse(strings.NewReader(s)) + t.Log(doc.OutputXML(true)) + if e, g := "", doc.OutputXML(true); strings.Index(g, e) == -1 { + t.Fatal("missing some comment-node.") + } + n := FindOne(doc, "//class_list") + t.Log(n.OutputXML(false)) + if e, g := "Lenard", n.OutputXML(false); strings.Index(g, e) == -1 { + t.Fatal("missing some comment-node") + } +}