Skip to content

Commit

Permalink
feat: add method to get children of current node
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Jul 7, 2021
1 parent c3e11a8 commit 4c0538e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/node.js
Expand Up @@ -173,6 +173,14 @@ class Node {
return this._wrapOrNull(this.get().parent)
}

/**
*
* @returns {Array<Node>}
*/
get children() {
return this.get().children.map(child => new Node(child))
}

/**
* Returns the previous sibling of the current node
* @returns {Node}
Expand Down
14 changes: 14 additions & 0 deletions test/spec/node.spec.js
Expand Up @@ -114,6 +114,20 @@ describe('Node', () => {
expect(node.length).to.equal(3)
})

it('returns the elements children wrapped as Nodes', () => {
const string = [
'<header>',
'<meta name="viewport" content="width=device-width, user-scalable=no">',
'<meta name="viewport" content="width=device-width, user-scalable=no">',
'<meta name="viewport" content="width=device-width, user-scalable=no">',
'</header>'
].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 = '<meta name="viewport" content="width=device-width, user-scalable=no">'
const node = Node.fromString(string)
Expand Down

0 comments on commit 4c0538e

Please sign in to comment.