Skip to content

Commit fd6d54d

Browse files
committed
Merge PR #127
close #126
2 parents 67437a5 + f72ceb9 commit fd6d54d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

node.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ func (n *Node) InnerText() string {
142142
return b.String()
143143
}
144144

145+
// ChildNodes returns all the child nodes of the current node,
146+
// including text, comments, and char data.
147+
func (n *Node) ChildNodes() []*Node {
148+
var list []*Node
149+
for child := n.FirstChild; child != nil; child = child.NextSibling {
150+
list = append(list, child)
151+
}
152+
return list
153+
}
154+
145155
func (n *Node) sanitizedData(preserveSpaces bool) string {
146156
if preserveSpaces {
147157
return n.Data

node_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ func verifyNodePointers(t *testing.T, n *Node) {
117117
testTrue(t, parent == nil || parent.LastChild == cur)
118118
}
119119

120+
func TestChildNodes(t *testing.T) {
121+
t.Run("Has 3 children", func(t *testing.T) {
122+
node := &Node{Type: ElementNode}
123+
124+
AddChild(node, &Node{Type: CommentNode})
125+
AddChild(node, &Node{Type: ElementNode})
126+
AddChild(node, &Node{Type: TextNode})
127+
128+
children := node.ChildNodes()
129+
130+
testTrue(t, len(children) == 3)
131+
})
132+
}
133+
120134
func TestAddAttr(t *testing.T) {
121135
for _, test := range []struct {
122136
name string

0 commit comments

Comments
 (0)