From acb9c31902ad433558693d0014d3381cd9b142de Mon Sep 17 00:00:00 2001 From: stfsy Date: Wed, 7 Jul 2021 10:30:47 +0200 Subject: [PATCH] feat: add getter for root element --- lib/node.js | 8 ++++++++ test/spec/node.spec.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/node.js b/lib/node.js index 5d4e492..e461138 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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 */ diff --git a/test/spec/node.spec.js b/test/spec/node.spec.js index a4c7cab..f08269c 100644 --- a/test/spec/node.spec.js +++ b/test/spec/node.spec.js @@ -114,6 +114,21 @@ describe('Node', () => { expect(node.length).to.equal(3) }) + it('returns the current DOMS root element', () => { + const string = [ + '
', + '', + '', + '', + '
' + ].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 = [ '
',