Skip to content

Commit

Permalink
fix #50
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Apr 3, 2024
1 parent 2fdcbd5 commit 4eb49ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
CommentNode
// AttributeNode is an attribute of element.
AttributeNode
// NotationNode is a directive represents in document (for example, <!text...>).
NotationNode
)

type Attr struct {
Expand Down Expand Up @@ -157,6 +159,9 @@ func outputXML(b *strings.Builder, n *Node, preserveSpaces bool, config *outputC
b.WriteString("-->")
}
return
case NotationNode:
fmt.Fprintf(b, "<!%s>", n.Data)
return
case DeclarationNode:
b.WriteString("<?" + n.Data)
default:
Expand Down
8 changes: 8 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,11 @@ func TestNodeLevel(t *testing.T) {
}

}

func TestDirectiveNode(t *testing.T) {
expected := `<!DOCTYPE people_list SYSTEM "example.dtd">`
n := &Node{Data: `DOCTYPE people_list SYSTEM "example.dtd"`, Type: NotationNode}
if v := n.OutputXML(true); expected != v {
t.Errorf(`expected "%s", obtained "%s"`, expected, v)
}
}
11 changes: 11 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ func (p *parser) parse() (*Node, error) {
}
p.prev = node
case xml.Directive:
node := &Node{Type: NotationNode, Data: string(tok), level: p.level}
if p.level == p.prev.level {
AddSibling(p.prev, node)
} else if p.level > p.prev.level {
AddChild(p.prev, node)
} else if p.level < p.prev.level {
for i := p.prev.level - p.level; i > 1; i-- {
p.prev = p.prev.Parent
}
AddSibling(p.prev.Parent, node)
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,36 @@ func TestStreamParser_DefaultNamespace(t *testing.T) {
x = `<Object id="ObjectC">ObjectD</Object>`
testOutputXML(t, "third call result", x, n)
}

func TestDirective(t *testing.T) {
s := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Workspace>
<Workspace xmlns="http://www.qlcplus.org/Workspace" CurrentWindow="FixtureManager">
<Creator>
<Name>Q Light Controller Plus</Name>
<Version>4.12.3</Version>
</Creator>
</Workspace>`
doc, err := Parse(strings.NewReader(s))
if err != nil {
t.Fatal(err.Error())
}

top := doc.FirstChild
n := top.NextSibling.NextSibling
if n == nil {
t.Error("should be not nil, but got nil")
return
}
if v := n.Type; v != NotationNode {
t.Errorf("expected the node type is NotationNode, but got %d", v)
}
if expected, val := `<!DOCTYPE Workspace>`, n.OutputXML(true); expected != val {
t.Errorf("expected %s but got %s", expected, val)
}

list := Find(doc, `//*`)
if m := len(list); m != 4 {
t.Errorf("expected count is 4 but got %d", m)
}
}
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (x *NodeNavigator) NodeType() xpath.NodeType {
switch x.curr.Type {
case CommentNode:
return xpath.CommentNode
case TextNode, CharDataNode:
case TextNode, CharDataNode, NotationNode:
return xpath.TextNode
case DeclarationNode, DocumentNode:
return xpath.RootNode
Expand Down

0 comments on commit 4eb49ad

Please sign in to comment.