Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils/clone): don't try to clone elements from different window context #4072

Merged
merged 9 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/core/utils/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import cache from '../base/cache';
* @param {Mixed} obj The object/array to clone
* @return {Mixed} A clone of the initial object or array
*/
export default function clone(obj) {
// handle circular references by caching the cloned object and returning it
const seen = cache.get('utils.clone', () => new WeakMap());
if (seen.has(obj)) {
return seen.get(obj);
}

export default function clone(obj, recursed) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
Expand All @@ -26,11 +20,23 @@ export default function clone(obj) {
return obj;
}

// handle circular references by caching the cloned object and returning it
// clear for every new call to clone so we don't return a cached value for
// a different object
const seen = cache.get('utils.clone', () => new Map());
if (!recursed) {
seen.clear();
}

if (recursed && seen.has(obj)) {
return seen.get(obj);
}

if (Array.isArray(obj)) {
const out = [];
seen.set(obj, out);
obj.forEach(value => {
out.push(clone(value));
out.push(clone(value, true));
});
return out;
}
Expand All @@ -39,7 +45,7 @@ export default function clone(obj) {
seen.set(obj, out);
// eslint-disable-next-line guard-for-in
for (const key in obj) {
out[key] = clone(obj[key]);
out[key] = clone(obj[key], true);
}
return out;
}
26 changes: 23 additions & 3 deletions test/core/utils/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('utils.clone', () => {
assert.deepEqual(Cat.prototype.meow, c.meow);
});

it('should clone circular objects', () => {
it('should clone circular objects while keeping the circular reference', () => {
const obj = { cats: true };
obj.child = obj;
const c = clone(obj);
Expand All @@ -107,6 +107,26 @@ describe('utils.clone', () => {
cats: true,
child: c
});
assert.strictEqual(c, c.child);
});

it('should not return the same object when cloned twice', () => {
const obj = { cats: true };
const c1 = clone(obj);
const c2 = clone(obj);

assert.notStrictEqual(c1, c2);
});

it('should not return the same object when nested', () => {
const obj = { dogs: true };
const obj1 = { cats: true, child: { prop: obj } };
const obj2 = { fish: [0, 1, 2], child: { prop: obj } };

const c1 = clone(obj1);
const c2 = clone(obj2);

assert.notStrictEqual(c1.child.prop, c2.child.prop);
});

it('should not clone HTML elements', () => {
Expand All @@ -119,7 +139,7 @@ describe('utils.clone', () => {
obj.cats = false;

assert.equal(c.cats, true);
assert.equal(c.node, obj.node);
assert.strictEqual(c.node, obj.node);
});

it('should not clone HTML elements from different windows', () => {
Expand All @@ -135,6 +155,6 @@ describe('utils.clone', () => {
obj.cats = false;

assert.equal(c.cats, true);
assert.equal(c.node, obj.node);
assert.strictEqual(c.node, obj.node);
});
});