Skip to content

Commit

Permalink
Fix a bug selecting a shadowRoot of an element (#4)
Browse files Browse the repository at this point in the history
* Fix a bug selecting a shadowRoot of an element

* Increase coverage
  • Loading branch information
elchininet authored Nov 14, 2023
1 parent 73859c4 commit 419ba7c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
31 changes: 31 additions & 0 deletions cypress/e2e/spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -210,6 +219,12 @@ describe('ShadowDomSelector spec', () => {
'You can not select a shadowRoot'
);

expect(
() => shadowRootQuerySelector('$')
).to.throw(
'You can not select a shadowRoot'
);

});
});

Expand Down Expand Up @@ -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;
Expand All @@ -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');
});
});

});
});

Expand Down
28 changes: 28 additions & 0 deletions src/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -203,6 +215,22 @@ export async function asyncShadowRootQuerySelectorInternal(
delay: number
): Promise<ShadowRoot | null> {

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,
Expand Down

0 comments on commit 419ba7c

Please sign in to comment.