Merge pull request #17 from shaunduncan/develop

Fix inadvertent recursive calls when doing a SelectNodes()
This commit is contained in:
jimt 2013-10-25 18:11:59 -07:00
commit 6bc115078b
2 changed files with 23 additions and 11 deletions

12
node.go
View File

@ -383,16 +383,14 @@ func (this *Node) SelectNodesRecursive(namespace, name string) []*Node {
} }
func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node, recurse bool) { func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node, recurse bool) {
if (namespace == "*" || cn.Name.Space == namespace) && (name == "*" || cn.Name.Local == name) {
*list = append(*list, cn)
if !recurse {
return
}
}
for _, v := range cn.Children { for _, v := range cn.Children {
if (namespace == "*" || v.Name.Space == namespace) && (name == "*" || v.Name.Local == name) {
*list = append(*list, v)
}
if recurse {
rec_SelectNodes(v, namespace, name, list, recurse) rec_SelectNodes(v, namespace, name, list, recurse)
} }
}
} }
func (this *Node) RemoveNameSpace() { func (this *Node) RemoveNameSpace() {

View File

@ -28,8 +28,7 @@ func TestWildcard(t *testing.T) {
return return
} }
list := doc.SelectNodes("ns", "*") list := doc.SelectNode("", "xml").SelectNodes("ns", "*")
if len(list) != 1 { if len(list) != 1 {
t.Errorf("Wrong number of child elements. Expected 1, got %d.", len(list)) t.Errorf("Wrong number of child elements. Expected 1, got %d.", len(list))
return return
@ -94,11 +93,26 @@ func TestNodeSearch(t *testing.T) {
return return
} }
nodes := doc.SelectNodes("", "item") nodes := doc.SelectNodesRecursive("", "item")
if len(nodes) == 0 { if len(nodes) == 0 {
t.Errorf("SelectNodes(): no nodes found.") t.Errorf("SelectNodes(): no nodes found.")
return return
} }
ch := doc.SelectNode("", "channel")
// Test that SelectNodes doesn't accidentally do recursive
links := ch.SelectNodes("", "link")
if len(links) != 1 {
t.Errorf("SelectNodes(): Expected 1, Got %d", len(links))
return
}
// Test SelectNodesRecursive does indeed get all of them
links = ch.SelectNodesRecursive("", "link")
if len(links) != 8 {
t.Errorf("SelectNodesRecursive(): Expected 8, Got %d", len(links))
return
}
} }
type Image struct { type Image struct {