Skip to content

Commit

Permalink
Merge PR #65
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Apr 3, 2024
2 parents ef0ed92 + 86d9157 commit 2fdcbd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func newXMLName(name string) xml.Name {
}
}

func (n *Node) Level() int {
return n.level
}

// InnerText returns the text between the start and end tags of the object.
func (n *Node) InnerText() string {
var output func(*strings.Builder, *Node)
Expand Down
23 changes: 23 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,26 @@ func TestOutputXMLWithPreserveSpaceOption(t *testing.T) {
t.Errorf("output was not expected. expected %v but got %v", " Robert ", resultWithoutSpace)
}
}

func TestNodeLevel(t *testing.T) {
s := `<?xml version="1.0" encoding="utf-8"?>
<class_list>
<student>
<name xml:space="preserve"> Robert </name>
<grade>A+</grade>
</student>
</class_list>`
doc, _ := Parse(strings.NewReader(s))
if doc.Level() != 0 {
t.Errorf(`expected "%d", obtained "%d"`, 0, doc.Level())
}
n := FindOne(doc, "/class_list")
if n.Level() != 1 {
t.Errorf(`expected "%d", obtained "%d"`, 1, n.Level())
}
n = FindOne(doc, "/class_list/student/name")
if n.Level() != 3 {
t.Errorf(`expected "%d", obtained "%d"`, 3, n.Level())
}

}

0 comments on commit 2fdcbd5

Please sign in to comment.