From 419ba7cefa3d36720a14ea9fc635164797a756e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Pereira?= Date: Wed, 15 Nov 2023 00:58:45 +0100 Subject: [PATCH] Fix a bug selecting a shadowRoot of an element (#4) * Fix a bug selecting a shadowRoot of an element * Increase coverage --- CHANGELOG.md | 4 ++++ cypress/e2e/spec.cy.ts | 31 +++++++++++++++++++++++++++++++ src/selectors/index.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73edaa5..c7e7fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.0.2] - 2023-11-15 + +- Fix a bug selecting a shadowRoot from an element + ## [1.0.0] - 2023-11-14 - Release of `shadow-dom-selector` \ No newline at end of file diff --git a/cypress/e2e/spec.cy.ts b/cypress/e2e/spec.cy.ts index afdbff7..a3faaa4 100644 --- a/cypress/e2e/spec.cy.ts +++ b/cypress/e2e/spec.cy.ts @@ -190,6 +190,15 @@ describe('ShadowDomSelector spec', () => { document.querySelector('section').shadowRoot.querySelector('article').shadowRoot ); + expect( + shadowRootQuerySelector( + document.querySelector('section'), + '$' + ) + ).to.equal( + document.querySelector('section').shadowRoot + ); + expect( shadowRootQuerySelector('section$ .article$ ul$') ).to.null; @@ -210,6 +219,12 @@ describe('ShadowDomSelector spec', () => { 'You can not select a shadowRoot' ); + expect( + () => shadowRootQuerySelector('$') + ).to.throw( + 'You can not select a shadowRoot' + ); + }); }); @@ -527,6 +542,15 @@ describe('ShadowDomSelector spec', () => { document.querySelector('section').shadowRoot.querySelector('article').shadowRoot ); + expect( + await asyncShadowRootQuerySelector( + document.querySelector('section'), + '$' + ) + ).to.equal( + document.querySelector('section').shadowRoot + ); + expect( await asyncShadowRootQuerySelector('#section$ div$', { retries: 1, delay: 0 }) ).to.null; @@ -552,6 +576,13 @@ describe('ShadowDomSelector spec', () => { }); }); + cy.wrap(null).then(() => { + return asyncShadowRootQuerySelector('$') + .catch((error: Error) => { + expect(error.message).to.contain('You can not select a shadowRoot'); + }); + }); + }); }); diff --git a/src/selectors/index.ts b/src/selectors/index.ts index b73a63c..afdf0fd 100644 --- a/src/selectors/index.ts +++ b/src/selectors/index.ts @@ -68,6 +68,18 @@ export function shadowRootQuerySelectorInternal( root: Document | Element ): ShadowRoot | null { + if ( + path.length === 1 && + !path[0].length + ) { + if (root instanceof Document) { + throw new SyntaxError( + getDocumentShadowRootError() + ); + } + return root.shadowRoot; + } + const lastElement = querySelectorInternal( path, root @@ -203,6 +215,22 @@ export async function asyncShadowRootQuerySelectorInternal( delay: number ): Promise { + if ( + path.length === 1 && + !path[0].length + ) { + if (root instanceof Document) { + throw new SyntaxError( + getDocumentShadowRootError() + ); + } + return getPromisableShadowRoot( + root, + retries, + delay + ); + } + const lastElement = await asyncQuerySelectorInternal( path, root,