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
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
@@ -8,7 +8,7 @@ import {
INode,
BuildCache,
} from './types';
import { isElement } from './utils';
import { defineCustomElement, isElement } from './utils';

const tagMap: tagMap = {
script: 'noscript',
@@ -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);
17 changes: 17 additions & 0 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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';
@@ -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;
}

451 changes: 451 additions & 0 deletions packages/rrweb/test/events/shadowDom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,451 @@
import { EventType, eventWithTime, IncrementalSource } from '../../src/types';

const now = Date.now();

const events: eventWithTime[] = [
{ type: EventType.DomContentLoaded, data: {}, timestamp: now },
{ type: EventType.Load, data: {}, timestamp: now + 100},
{
type: EventType.Meta,
data: { href: 'https://localhost', width: 655, height: 846 },
timestamp: now + 100,
},
{
type: 2,
data: {
node: {
type: 0,
childNodes: [
{ type: 1, name: 'html', publicId: '', systemId: '', id: 2 },
{
type: 2,
tagName: 'html',
attributes: { lang: 'EN' },
childNodes: [
{
type: 2,
tagName: 'head',
attributes: {},
childNodes: [
{
type: 2,
tagName: 'meta',
attributes: { charset: 'utf-8' },
childNodes: [],
id: 5,
},
{
type: 2,
tagName: 'title',
attributes: {},
childNodes: [
{
type: 3,
textContent: 'Editable List | Web Components',
id: 7,
},
],
id: 6,
},
],
id: 4,
},
{
type: 2,
tagName: 'body',
attributes: {},
childNodes: [
{
type: 2,
tagName: 'editable-list',
attributes: {
title: 'TODO',
'list-item-0': 'First item on the list',
'list-item-1': 'Second item on the list',
'list-item-2': 'Third item on the list',
'list-item-3': 'Fourth item on the list',
'list-item-4': 'Fifth item on the list',
listitem: 'This will not appear',
'add-item-text': 'Add new list item:',
},
childNodes: [
],
id: 12,
isShadowHost: true,
},
],
id: 10,
},
],
id: 3,
},
],
id: 1,
},
initialOffset: { left: 0, top: 0 },
},
timestamp: now + 100,
},
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Mutation,
texts: [],
attributes: [],
removes: [],
adds: [
{
parentId: 12,
nextId: null,
node: {
type: 2,
tagName: 'div',
attributes: { class: 'editable-list' },
childNodes: [],
id: 14,
isShadow: true,
},
},
{
parentId: 14,
nextId: 21,
node: {
type: 2,
tagName: 'h3',
attributes: {},
childNodes: [],
id: 19,
},
},
{
parentId: 19,
nextId: null,
node: { type: 3, textContent: 'TODO', id: 20 },
},
{
parentId: 14,
nextId: 22,
node: { type: 3, textContent: '\n ', id: 21 },
},
{
parentId: 14,
nextId: 54,
node: {
type: 2,
tagName: 'ul',
attributes: { class: 'item-list' },
childNodes: [],
id: 22,
},
},
{
parentId: 22,
nextId: 24,
node: { type: 3, textContent: '\n \n ', id: 23 },
},
{
parentId: 22,
nextId: 29,
node: {
type: 2,
tagName: 'li',
attributes: {},
childNodes: [],
id: 24,
},
},
{
parentId: 24,
nextId: 26,
node: {
type: 3,
textContent: 'First item on the list\n ',
id: 25,
},
},
{
parentId: 24,
nextId: 28,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-remove-item icon' },
childNodes: [],
id: 26,
},
},
{
parentId: 26,
nextId: null,
node: { type: 3, textContent: '⊖', id: 27 },
},
{
parentId: 24,
nextId: null,
node: { type: 3, textContent: '\n ', id: 28 },
},
{
parentId: 22,
nextId: 30,
node: { type: 3, textContent: '\n \n ', id: 29 },
},
{
parentId: 22,
nextId: 35,
node: {
type: 2,
tagName: 'li',
attributes: {},
childNodes: [],
id: 30,
},
},
{
parentId: 30,
nextId: 32,
node: {
type: 3,
textContent: 'Second item on the list\n ',
id: 31,
},
},
{
parentId: 30,
nextId: 34,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-remove-item icon' },
childNodes: [],
id: 32,
},
},
{
parentId: 32,
nextId: null,
node: { type: 3, textContent: '⊖', id: 33 },
},
{
parentId: 30,
nextId: null,
node: { type: 3, textContent: '\n ', id: 34 },
},
{
parentId: 22,
nextId: 36,
node: { type: 3, textContent: '\n \n ', id: 35 },
},
{
parentId: 22,
nextId: 41,
node: {
type: 2,
tagName: 'li',
attributes: {},
childNodes: [],
id: 36,
},
},
{
parentId: 36,
nextId: 38,
node: {
type: 3,
textContent: 'Third item on the list\n ',
id: 37,
},
},
{
parentId: 36,
nextId: 40,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-remove-item icon' },
childNodes: [],
id: 38,
},
},
{
parentId: 38,
nextId: null,
node: { type: 3, textContent: '⊖', id: 39 },
},
{
parentId: 36,
nextId: null,
node: { type: 3, textContent: '\n ', id: 40 },
},
{
parentId: 22,
nextId: 42,
node: { type: 3, textContent: '\n \n ', id: 41 },
},
{
parentId: 22,
nextId: 47,
node: {
type: 2,
tagName: 'li',
attributes: {},
childNodes: [],
id: 42,
},
},
{
parentId: 42,
nextId: 44,
node: {
type: 3,
textContent: 'Fourth item on the list\n ',
id: 43,
},
},
{
parentId: 42,
nextId: 46,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-remove-item icon' },
childNodes: [],
id: 44,
},
},
{
parentId: 44,
nextId: null,
node: { type: 3, textContent: '⊖', id: 45 },
},
{
parentId: 42,
nextId: null,
node: { type: 3, textContent: '\n ', id: 46 },
},
{
parentId: 22,
nextId: 48,
node: { type: 3, textContent: '\n \n ', id: 47 },
},
{
parentId: 22,
nextId: 53,
node: {
type: 2,
tagName: 'li',
attributes: {},
childNodes: [],
id: 48,
},
},
{
parentId: 48,
nextId: 50,
node: {
type: 3,
textContent: 'Fifth item on the list\n ',
id: 49,
},
},
{
parentId: 48,
nextId: 52,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-remove-item icon' },
childNodes: [],
id: 50,
},
},
{
parentId: 50,
nextId: null,
node: { type: 3, textContent: '⊖', id: 51 },
},
{
parentId: 14,
nextId: 65,
node: {
type: 2,
tagName: 'div',
attributes: {},
childNodes: [],
id: 55,
},
},
{
parentId: 55,
nextId: 57,
node: { type: 3, textContent: '\n ', id: 56 },
},
{
parentId: 55,
nextId: 59,
node: {
type: 2,
tagName: 'label',
attributes: {},
childNodes: [],
id: 57,
},
},
{
parentId: 57,
nextId: null,
node: { type: 3, textContent: 'Add new list item:', id: 58 },
},
{
parentId: 55,
nextId: 60,
node: { type: 3, textContent: '\n ', id: 59 },
},
{
parentId: 55,
nextId: 61,
node: {
type: 2,
tagName: 'input',
attributes: { class: 'add-new-list-item-input', type: 'text' },
childNodes: [],
id: 60,
},
},
{
parentId: 55,
nextId: 62,
node: { type: 3, textContent: '\n ', id: 61 },
},
{
parentId: 55,
nextId: 64,
node: {
type: 2,
tagName: 'button',
attributes: { class: 'editable-list-add-item icon' },
childNodes: [],
id: 62,
},
},
{
parentId: 62,
nextId: null,
node: { type: 3, textContent: '⊕', id: 63 },
},
{
parentId: 55,
nextId: null,
node: { type: 3, textContent: '\n ', id: 64 },
},
{
parentId: 14,
nextId: null,
node: { type: 3, textContent: '\n ', id: 65 },
},
],
},
timestamp: now + 400,
}
];

export default events;
15 changes: 15 additions & 0 deletions packages/rrweb/test/replayer.test.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import {
import styleSheetRuleEvents from './events/style-sheet-rule-events';
import orderingEvents from './events/ordering';
import iframeEvents from './events/iframe';
import shadowDomEvents from './events/shadowDom';

interface ISuite {
code: string;
@@ -399,4 +400,18 @@ describe('replayer', function () {

await assertDomSnapshot(page, __filename, 'ordering-events');
});

it('should have `:defined` web components', async () => {
await page.evaluate(`events = ${JSON.stringify(shadowDomEvents)}`);
const result = await page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
replayer.play();
replayer.pause(1000);
replayer.iframe.contentDocument.querySelectorAll(':not(:defined)').length;
`);
await page.waitForTimeout(200);

expect(result).toEqual(0);
});
});