Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Modify cloneNode to handle shadow dom. #1

Merged
merged 2 commits into from
Apr 25, 2022

Conversation

aschenoni
Copy link

Currently, Percy does not provide support for capturing shadow dom as outlined in this issue: percy#280

@mmun previously did some exploration and created a working modification to Percy's cloning behavior that enables capturing shadow dom in Chrome. This PR contains the modification necessary to allow shadow dom to be properly cloned.

@aschenoni aschenoni requested a review from mmun April 18, 2022 16:00
};

const customOuterHTML = docElement => {
return `<html class="hydrated">${docElement.getInnerHTML()}</html>`;
Copy link

@mmun mmun Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general approach would be to copy the attributes on the root element over instead of hard coding class="hydrated" (which a Stencil-specific thing)

Comment on lines 54 to 55
clonedDocumentElementFragment.head = document.createDocumentFragment();
clonedDocumentElementFragment.documentElement = clonedDocumentElementFragment.firstChild;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is pretty hacky. We should rewrite the logic here to avoid adding properties onto a fragment which don't exist.

const getOuterHTML = docElement => {
let innerHTML = docElement.getInnerHTML();
docElement.textContent = '';
return docElement.outerHTML.replace('</html>', `${innerHTML}</html>`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

Copy link

@mmun mmun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

* This enables us to capture shadow DOM in snapshots. It takes advantage of `attachShadow`'s mode option set to open
* https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#parameters
*/
const deepClone = host => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we know this (not-trivial) walker works ?
do we need any tests ? or is this code copy-pasted from somewhere ?

Copy link

@mmun mmun Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's copied from https://stackoverflow.com/a/55540552 and modified a bit. I don't think this fork is really set up to run tests.

I'd like to see the code tidied up and tested but yeah...

Copy link

@c69-addepar c69-addepar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, but the actual clone function is a bit scary ;)

Copy link

@c69-addepar c69-addepar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, because we have tried this and it works (fixes our tests) 😎

@aschenoni aschenoni merged commit 69640fa into master Apr 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants