Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(replay): Define custom elements (web components) #87

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/rrweb-snapshot/src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
INode,
BuildCache,
} from './types';
import { isElement } from './utils';
import { defineCustomElement, isElement } from './utils';

const tagMap: tagMap = {
script: 'noscript',
Expand Down Expand Up @@ -284,6 +284,9 @@ function buildNode(
*/
if (!node.shadowRoot) {
node.attachShadow({ mode: 'open' });
if (doc.defaultView) {
defineCustomElement(doc.defaultView, tagName);
}
} else {
while (node.shadowRoot.firstChild) {
node.shadowRoot.removeChild(node.shadowRoot.firstChild);
Expand Down
17 changes: 17 additions & 0 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,20 @@ export function getInputValue(

return el.value;
}

/**
* Ensure we define custom elements as you can have css that targets the
* `:defined` pseudo class (e.g. hide until defined)
*/
export function defineCustomElement(w: Window, elementName: string) {
// We need to define custom elements inside of the correct window (i.e.
// inside of the iframe)
try {
// Can only define custom element once
if (!w.customElements.get(elementName)) {
// @ts-ignore HTMLElement exists on window
const CustomElement = w.HTMLElement as CustomElementConstructor;
w.customElements.define(elementName, class extends CustomElement {});
}
} catch {}
}
1 change: 1 addition & 0 deletions packages/rrweb-snapshot/typings/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export declare function maskInputValue({ input, maskInputSelector, unmaskInputSe
export declare function is2DCanvasBlank(canvas: HTMLCanvasElement): boolean;
export declare function getInputType(element: HTMLElement): Lowercase<string> | null;
export declare function getInputValue(el: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLOptionElement, tagName: Uppercase<string>, type: attributes[string]): string;
export declare function defineCustomElement(w: Window, elementName: string): void;
export {};
9 changes: 7 additions & 2 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NodeType,
BuildCache,
createCache,
defineCustomElement,
} from '@sentry-internal/rrweb-snapshot';
import * as mittProxy from 'mitt';
import { polyfill as smoothscrollPolyfill } from './smoothscroll';
Expand Down Expand Up @@ -1458,8 +1459,12 @@ export class Replayer {
if (mutation.node.isShadow) {
// If the parent is attached a shadow dom after it's created, it won't have a shadow root.
if (!hasShadowRoot(parent)) {
((parent as Node) as HTMLElement).attachShadow({ mode: 'open' });
parent = ((parent as Node) as HTMLElement).shadowRoot!;
const parentNode = parent as Node;
(parentNode as HTMLElement).attachShadow({ mode: 'open' });
parent = (parentNode as HTMLElement).shadowRoot!;
if (this.iframe.contentWindow) {
defineCustomElement(this.iframe.contentWindow, parentNode.nodeName);
}
} else parent = parent.shadowRoot;
}

Expand Down
Loading