Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
feat: add getter for root element
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Jul 7, 2021
1 parent 4c0538e commit acb9c31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ class Node {
return this._wrapOrNull(next)
}

get root() {
let root = this.parent
while (root && root.parent.type !== 'root') {
root = root.parent
}
return Node.of(root)
}

/**
* @private
*/
Expand Down
15 changes: 15 additions & 0 deletions test/spec/node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ describe('Node', () => {
expect(node.length).to.equal(3)
})

it('returns the current DOMS root element', () => {
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 meta = node.find('meta')[0]
const root = meta.root
expect(root.name).to.equal('header')
})

it('returns the elements children wrapped as Nodes', () => {
const string = [
'<header>',
Expand Down

0 comments on commit acb9c31

Please sign in to comment.