From 9e07245148cb854886bc4dd6c06c84bbf3c6b3e0 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Fri, 19 Apr 2024 13:38:53 -0230 Subject: [PATCH] upstream: perf: only call `createHTMLDocument` where it is needed (#179) * only call createHTMLDocument where it is needed * Perf: create the mutation document once as a 'singleton' as it can be reused --------- Co-authored-by: Eoghan Murray Co-authored-by: Michael Dellanoce --- packages/rrweb/src/record/mutation.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index f4c23a818c..5661316a2e 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -197,6 +197,7 @@ export default class MutationBuffer { private shadowDomManager: observerParam['shadowDomManager']; private canvasManager: observerParam['canvasManager']; private processedNodeManager: observerParam['processedNodeManager']; + private unattachedDoc: HTMLDocument; public init(options: MutationBufferParam) { ( @@ -517,14 +518,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; @@ -650,7 +643,17 @@ export default class MutationBuffer { this.maskAttributeFn, ); if (attributeName === 'style') { - const old = unattachedDoc.createElement('span'); + 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 = this.unattachedDoc.createElement('span'); if (m.oldValue) { old.setAttribute('style', m.oldValue); }