Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: style not applied to polyfillled shadow dom
  • Loading branch information
YunFeng0817 committed Jul 20, 2022
commit 6cec3e6710c4f2bb44bf9fc6c70e502201f30da6
13 changes: 9 additions & 4 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isElement,
isShadowRoot,
maskInputValue,
isNativeShadowDom,
} from './utils';

let _id = 1;
Expand Down Expand Up @@ -248,7 +249,10 @@ export function transformAttribute(
value: string,
): string {
// relative path in attribute
if (name === 'src' || (name === 'href' && value && !(tagName === 'use' && value[0] === '#'))) {
if (
name === 'src' ||
(name === 'href' && value && !(tagName === 'use' && value[0] === '#'))
) {
// href starts with a # is an id pointer for svg
return absoluteToDoc(doc, value);
} else if (name === 'xlink:href' && value && value[0] !== '#') {
Expand Down Expand Up @@ -1025,7 +1029,8 @@ export function serializeNodeWithId(
recordChild = recordChild && !serializedNode.needBlock;
// this property was not needed in replay side
delete serializedNode.needBlock;
if ((n as HTMLElement).shadowRoot) serializedNode.isShadowHost = true;
if ((n as HTMLElement).shadowRoot && isNativeShadowDom())
serializedNode.isShadowHost = true;
}
if (
(serializedNode.type === NodeType.Document ||
Expand Down Expand Up @@ -1075,14 +1080,14 @@ export function serializeNodeWithId(
for (const childN of Array.from(n.shadowRoot.childNodes)) {
const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
if (serializedChildNode) {
serializedChildNode.isShadow = true;
isNativeShadowDom() && (serializedChildNode.isShadow = true);
serializedNode.childNodes.push(serializedChildNode);
}
}
}
}

if (n.parentNode && isShadowRoot(n.parentNode)) {
if (n.parentNode && isShadowRoot(n.parentNode) && isNativeShadowDom()) {
serializedNode.isShadow = true;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export function isShadowRoot(n: Node): n is ShadowRoot {
return Boolean(host?.shadowRoot === n);
}

/**
* To fix the issue https://github.com/rrweb-io/rrweb/issues/933.
* Some websites use polyfilled shadow dom and this function is used to detect this situation.
*/
export function isNativeShadowDom() {
return window.ShadowRoot?.toString().indexOf('[native code]') > -1;
}

export class Mirror implements IMirror<Node> {
private idNodeMap: idNodeMap = new Map();
private nodeMetaMap: nodeMetaMap = new WeakMap();
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb-snapshot/typings/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MaskInputFn, MaskInputOptions, IMirror, serializedNodeWithId } from './types';
export declare function isElement(n: Node): n is Element;
export declare function isShadowRoot(n: Node): n is ShadowRoot;
export declare function isNativeShadowDom(): boolean;
export declare class Mirror implements IMirror<Node> {
private idNodeMap;
private nodeMetaMap;
Expand Down
6 changes: 5 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
needMaskingText,
maskInputValue,
Mirror,
isNativeShadowDom,
} from 'rrweb-snapshot';
import type {
mutationRecord,
Expand Down Expand Up @@ -581,7 +582,10 @@ export default class MutationBuffer {
this.removes.push({
parentId,
id: nodeId,
isShadow: isShadowRoot(m.target) ? true : undefined,
isShadow:
isShadowRoot(m.target) && isNativeShadowDom()
? true
: undefined,
});
}
this.mapRemoves.push(n);
Expand Down
2 changes: 2 additions & 0 deletions packages/rrweb/src/record/shadow-dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
import { initMutationObserver, initScrollObserver } from './observer';
import { patch } from '../utils';
import type { Mirror } from 'rrweb-snapshot';
import { isNativeShadowDom } from 'rrweb-snapshot';

type BypassOptions = Omit<
MutationBufferParam,
Expand Down Expand Up @@ -53,6 +54,7 @@ export class ShadowDomManager {
}

public addShadowRoot(shadowRoot: ShadowRoot, doc: Document) {
if (!isNativeShadowDom()) return;
initMutationObserver(
{
...this.bypassOptions,
Expand Down