From 4c0538e081811944edb3afa3d0a1beb71fe9640f Mon Sep 17 00:00:00 2001 From: stfsy Date: Wed, 7 Jul 2021 09:35:03 +0200 Subject: [PATCH] feat: add method to get children of current node --- lib/node.js | 8 ++++++++ test/spec/node.spec.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/node.js b/lib/node.js index 3748c27..5d4e492 100644 --- a/lib/node.js +++ b/lib/node.js @@ -173,6 +173,14 @@ class Node { return this._wrapOrNull(this.get().parent) } + /** + * + * @returns {Array} + */ + get children() { + return this.get().children.map(child => new Node(child)) + } + /** * Returns the previous sibling of the current node * @returns {Node} diff --git a/test/spec/node.spec.js b/test/spec/node.spec.js index df9ea7b..a4c7cab 100644 --- a/test/spec/node.spec.js +++ b/test/spec/node.spec.js @@ -114,6 +114,20 @@ describe('Node', () => { expect(node.length).to.equal(3) }) + it('returns the elements children wrapped as Nodes', () => { + const string = [ + '
', + '', + '', + '', + '
' + ].join('') + + const node = Node.fromString(string) + const children = node.children + expect(children).to.have.length(3) + }) + it('should have the correct tag name', () => { const string = '' const node = Node.fromString(string)