Skip to content

Commit

Permalink
Adding support of shadowrootmode (#1507)
Browse files Browse the repository at this point in the history
* Adding support of shadowrootmode

* Updating tests

* Fixing specs

* Reverting spec changes

* Adding test for shadowroot
  • Loading branch information
rishigupta1599 authored Feb 8, 2024
1 parent 9d95f18 commit a23e339
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/dom/src/inject-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ export function injectDeclarativeShadowDOMPolyfill(ctx) {
scriptEl.setAttribute('data-percy-injected', true);

scriptEl.innerHTML = `
function attachShadow(template, mode){
const shadowRoot = template.parentNode.attachShadow({ mode });
shadowRoot.appendChild(template.content);
template.remove();
}
function reversePolyFill(root=document){
root.querySelectorAll('template[shadowroot]').forEach(template => {
const mode = template.getAttribute('shadowroot');
const shadowRoot = template.parentNode.attachShadow({ mode });
shadowRoot.appendChild(template.content);
template.remove();
attachShadow(template, mode);
});
root.querySelectorAll('template[shadowrootmode]').forEach(template => {
const mode = template.getAttribute('shadowrootmode');
attachShadow(template, mode);
});
root.querySelectorAll('[data-percy-shadow-host]').forEach(shadowHost => reversePolyFill(shadowHost.shadowRoot));
Expand Down
16 changes: 16 additions & 0 deletions packages/dom/test/inject-polyfill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ describe('injectDeclarativeShadowDOMPolyfill', () => {

beforeEach(() => {
dom = withExample(`
<div data-percy-shadow-host>
<template shadowrootmode="open">
<div data-percy-shadow-host>
<template shadowrootmode="open">
</template>
</div>
<div data-percy-shadow-host>
<template shadowrootmode="open">
</template>
</div>
</template>
</div>
<div data-percy-shadow-host>
<template shadowroot="open">
<div data-percy-shadow-host>
<template shadowroot="open">
</template>
</div>
<div data-percy-shadow-host>
<template shadowroot="open">
</template>
</div>
</template>
</div>
`, { withShadow: false });
Expand Down

0 comments on commit a23e339

Please sign in to comment.