Skip to content

✨ Shadow DOM serialisation support#1165

Merged
itsjwala merged 107 commits intomasterfrom
shadow-dom
Feb 14, 2023
Merged

✨ Shadow DOM serialisation support#1165
itsjwala merged 107 commits intomasterfrom
shadow-dom

Conversation

@itsjwala
Copy link
Copy Markdown
Contributor

@itsjwala itsjwala commented Jan 5, 2023

Shadow DOM is a critical part of the newer Web Component standard which is growing in adoption day by day. This shadow DOM is not captured in a Percy snapshot by our existing serialization methods. Hence, this PR adds support for capturing it.

Constraints

  1. We can’t serialize the DOM if the shadow root is attached in closed mode since we don’t have access to the HTML that makes up the tree. We need the shadow root to be open.

  2. Making it work across all browsers in the infrastructure.

  3. Shadow DOM capturing only works if the user’s test browser is Chromium 90+

Implementation details

Below is summarised lifecycle of percySnapshot command (explained in detail here)

image

  • Currently serialization of Shadow DOM is only possible by Declarative Shadow DOM API provided by the browser.
    • The APIs are currently somewhat stable in chromium, but there is no sign of their adoption in Firefox. (ref https://caniuse.com/declarative-shadow-dom)
    • Method we're trying to work with is getInnerHTML (We've not polyfilled this method for Firefox)
  • Once serialized the #shadow-root (open) are converted into <template shadowRoot="open"> tags
  • We can then pass this dom snapshot across the wire in our infrastructure browsers to load the Shadow DOM.
  • Though Declarative Shadow DOM and its API are still not supported Firefox/Safari, and thus we need to deserialize this Declarative Shadow DOM back to its original form.
    • We inject a custom polyfill for Declarative Shadow DOM which does this job.

image

@itsjwala itsjwala linked an issue Jan 5, 2023 that may be closed by this pull request
@gdborton

This comment was marked as resolved.

@itsjwala

This comment was marked as resolved.

Comment thread packages/dom/test/helpers.js Outdated
Comment thread packages/dom/test/helpers.js Outdated
@itsjwala itsjwala marked this pull request as ready for review January 10, 2023 13:13
@itsjwala itsjwala force-pushed the shadow-dom branch 3 times, most recently from 6cb6877 to 4b90012 Compare January 11, 2023 10:50
@itsjwala itsjwala linked an issue Jan 11, 2023 that may be closed by this pull request
Comment thread packages/dom/test/serialize-dom.test.js Outdated
Comment thread packages/dom/test/serialize-dom.test.js Outdated
Comment thread packages/dom/test/serialize-dom.test.js Outdated
Comment thread packages/dom/src/serialize-dom.js Outdated
Comment thread packages/dom/src/serialize-dom.js Outdated
Comment thread packages/dom/test/inject-polyfill.test.js
Comment thread packages/core/src/page.js Outdated
@c69-addepar

This comment was marked as resolved.

@itsjwala

This comment was marked as resolved.

@c69-addepar

This comment was marked as resolved.

Samarjeet and others added 4 commits February 6, 2023 21:09
we require document interface to in further steps, and originally document.cloneNode returned document object instead of fragment
Comment thread packages/dom/src/serialize-cssom.js Outdated
Comment thread packages/dom/src/serialize-cssom.js Outdated
Comment thread packages/dom/src/serialize-dom.js Outdated
@itsjwala itsjwala merged commit 66b8192 into master Feb 14, 2023
@itsjwala itsjwala deleted the shadow-dom branch February 14, 2023 12:28
@esetnik esetnik mentioned this pull request Feb 21, 2023
@c69-addepar
Copy link
Copy Markdown

Confirmed:

  • percy/cli 1.19.2 and percy/ember 4.0.2 fix our issues.

Thank you, team !

@mmun
Copy link
Copy Markdown

mmun commented Feb 22, 2023

Thanks again @itsjwala and @samarsault for your work. I hope it brings additional value to all your customers!

By the way, Safari announced recently that their technical preview has enabled declarative shadow DOM APIs by default. I'm not sure if this includes the getInnerHTML({ includeShadowRoots: true }) API though. In any case, there's hope that we'll one day have support for this Percy feature across all browsers.

@itsjwala
Copy link
Copy Markdown
Contributor Author

itsjwala commented Feb 23, 2023

@mmun yes we're keeping an eye on this development for Safari, though it would be particularly be helpful for browsers in infrastructure (we only support chrome/firefox as a test browser so serialization point of view we want Firefox to pickup this development soon)

For now, declarative shadow DOM polyfill - ref is working for Firefox, Safari, and Safari on iPhone platforms in our infrastructure. (we hope to get rid of this polyfill in future 🤞)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

page.evaluate: TypeError: Cannot read properties of null (reading 'sheet') at serializeCSSOM Add Shadow DOM support to @percy/dom

5 participants