Skip to content
Merged
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
27 changes: 27 additions & 0 deletions navigator_root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package htmlquery

import (
"strings"
"testing"

"github.com/antchfx/xpath"
)

func TestMoveToRootFromAttribute(t *testing.T) {
doc, err := Parse(strings.NewReader(`<div id="example"><span></span></div>`))
if err != nil {
t.Fatal(err)
}
iter := xpath.MustCompile("//div/@id").Select(CreateXPathNavigator(doc))
if !iter.MoveNext() {
t.Fatal("attribute not found")
}
nav := iter.Current().Copy()
nav.MoveToRoot()
if !nav.MoveToChild() {
t.Fatal("root still has an attribute cursor and cannot move to its child")
}
if nav.NodeType() != xpath.ElementNode || nav.LocalName() != "html" {
t.Fatalf("unexpected child after root navigation: %v", nav)
}
}
1 change: 1 addition & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func (h *NodeNavigator) Copy() xpath.NodeNavigator {

func (h *NodeNavigator) MoveToRoot() {
h.curr = h.root
h.attr = -1
}

func (h *NodeNavigator) MoveToParent() bool {
Expand Down