From e36f2bd697a6c33aaa480a332db15d884fdb6f83 Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Wed, 30 Aug 2023 13:51:17 -0400 Subject: [PATCH 1/2] only call createHTMLDocument where it is needed --- packages/rrweb/src/record/mutation.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 097d1a8fd5..53eb072c86 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -497,14 +497,6 @@ export default class MutationBuffer { if (isIgnored(m.target, this.mirror)) { return; } - let unattachedDoc; - try { - // avoid upsetting original document from a Content Security point of view - unattachedDoc = document.implementation.createHTMLDocument(); - } catch (e) { - // fallback to more direct method - unattachedDoc = this.doc; - } switch (m.type) { case 'characterData': { const value = m.target.textContent; @@ -597,6 +589,14 @@ export default class MutationBuffer { value, ); if (attributeName === 'style') { + let unattachedDoc; + try { + // avoid upsetting original document from a Content Security point of view + unattachedDoc = document.implementation.createHTMLDocument(); + } catch (e) { + // fallback to more direct method + unattachedDoc = this.doc; + } const old = unattachedDoc.createElement('span'); if (m.oldValue) { old.setAttribute('style', m.oldValue); From 1ad42a7f331ab9409bdc21dc1b33ae77d46d10f6 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Mon, 9 Oct 2023 14:43:15 +0100 Subject: [PATCH 2/2] Perf: create the mutation document once as a 'singleton' as it can be reused --- packages/rrweb/src/record/mutation.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 53eb072c86..c947b7d304 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -188,6 +188,7 @@ export default class MutationBuffer { private shadowDomManager: observerParam['shadowDomManager']; private canvasManager: observerParam['canvasManager']; private processedNodeManager: observerParam['processedNodeManager']; + private unattachedDoc: HTMLDocument; public init(options: MutationBufferParam) { ( @@ -589,15 +590,17 @@ export default class MutationBuffer { value, ); if (attributeName === 'style') { - let unattachedDoc; - try { - // avoid upsetting original document from a Content Security point of view - unattachedDoc = document.implementation.createHTMLDocument(); - } catch (e) { - // fallback to more direct method - unattachedDoc = this.doc; + if (!this.unattachedDoc) { + try { + // avoid upsetting original document from a Content Security point of view + this.unattachedDoc = + document.implementation.createHTMLDocument(); + } catch (e) { + // fallback to more direct method + this.unattachedDoc = this.doc; + } } - const old = unattachedDoc.createElement('span'); + const old = this.unattachedDoc.createElement('span'); if (m.oldValue) { old.setAttribute('style', m.oldValue); }