File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
145155func (n * Node ) sanitizedData (preserveSpaces bool ) string {
146156 if preserveSpaces {
147157 return n .Data
Original file line number Diff line number Diff 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+
120134func TestAddAttr (t * testing.T ) {
121135 for _ , test := range []struct {
122136 name string
You can’t perform that action at this time.
0 commit comments