Skip to content

Commit

Permalink
upstream: perf: only call createHTMLDocument where it is needed (#179)
Browse files Browse the repository at this point in the history
* 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 <eoghan@getthere.ie>
Co-authored-by: Michael Dellanoce <mdellanoce@pendo.io>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 02e711d commit 9e07245
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9e07245

Please sign in to comment.