Skip to content

Commit

Permalink
Replace Array.from with clean implementation
Browse files Browse the repository at this point in the history
This work is to try to provide support where rrweb might be included
in applications with various tools that might override Array.from
so that the 2nd parameter (the map function) will always work for
rrweb.
  • Loading branch information
mdellanoce authored and colingm committed May 6, 2024
1 parent f1e6a51 commit f36ecc9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/eleven-bobcats-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'rrweb-snapshot': patch
'rrweb': patch
---

try to provide clean implementation for Array.from to support older libraries that might drop the 2nd parameter
13 changes: 13 additions & 0 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ let takeFullSnapshot!: (isCheckout?: boolean) => void;
let canvasManager!: CanvasManager;
let recording = false;

// Multiple tools (i.e. MooTools, Prototype.js) override Array.from and drop support for the 2nd parameter
// Try to pull a clean implementation from a newly created iframe
try {
if (Array.from([1], x => x * 2)[0] !== 2) {
const cleanFrame: HTMLIFrameElement = document.createElement('iframe');
document.body.appendChild(cleanFrame);
Array.from = cleanFrame.contentWindow?.Array.from || Array.from;
document.body.removeChild(cleanFrame);
}
} catch (err) {
console.debug('Unable to override Array.from', err);
}

const mirror = createMirror();
function record<T = eventWithTime>(
options: recordOptions<T> = {},
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/record/stylesheet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
adoptedStyleSheetParam,
attributeMutation,
mutationCallBack,
styleSheetAddRule,
} from '@rrweb/types';
import { StyleSheetMirror } from '../utils';

Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export type missingNodeMap = {
declare global {
interface Window {
FontFace: typeof FontFace;
Array: typeof Array;
}
}

Expand Down

0 comments on commit f36ecc9

Please sign in to comment.