Skip to content

Commit

Permalink
Merge pull request #1 from amplitude/kwallach/perf-create-document
Browse files Browse the repository at this point in the history
perf: only call createHTMLDocument when it is needed
  • Loading branch information
kwalker3690 authored Oct 18, 2023
2 parents 57a940a + 934262f commit 8eab3c2
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import type {
Optional,
addedNodeMutation,
attributeCursor,
mutationRecord,
removedNodeMutation,
textCursor,
} from '@rrweb/types';
import {
serializeNodeWithId,
transformAttribute,
IGNORED_NODE,
Mirror,
getInputType,
ignoreAttribute,
isNativeShadowDom,
isShadowRoot,
needMaskingText,
maskInputValue,
Mirror,
isNativeShadowDom,
getInputType,
needMaskingText,
serializeNodeWithId,
toLowerCase,
transformAttribute,
} from 'rrweb-snapshot';
import type { observerParam, MutationBufferParam } from '../types';
import type {
mutationRecord,
textCursor,
attributeCursor,
removedNodeMutation,
addedNodeMutation,
Optional,
} from '@rrweb/types';
import type { MutationBufferParam, observerParam } from '../types';
import {
isBlocked,
closestElementOfNode,
getShadowHost,
hasShadowRoot,
inDom,
isAncestorRemoved,
isBlocked,
isIgnored,
isSerialized,
hasShadowRoot,
isSerializedIframe,
isSerializedStylesheet,
inDom,
getShadowHost,
closestElementOfNode,
} from '../utils';

type DoubleLinkedListNode = {
Expand Down Expand Up @@ -498,14 +498,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 @@ -599,6 +591,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);
Expand Down

0 comments on commit 8eab3c2

Please sign in to comment.