Skip to content

Commit

Permalink
[core] Fix deepmerge of DOM elements (#20100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinH authored Mar 16, 2020
1 parent 1d20156 commit bbcc43d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/material-ui-utils/src/deepmerge.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
export function isPlainObject(item) {
return item && typeof item === 'object' && item.constructor === Object;
}

export default function deepmerge(target, source, options = { clone: true }) {
const output = options.clone ? { ...target } : target;

if (isObject(target) && isObject(source)) {
if (isPlainObject(target) && isPlainObject(source)) {
Object.keys(source).forEach(key => {
// Avoid prototype pollution
if (key === '__proto__') {
return;
}

if (isObject(source[key]) && key in target) {
if (isPlainObject(source[key]) && key in target) {
output[key] = deepmerge(target[key], source[key], options);
} else {
output[key] = source[key];
Expand Down
10 changes: 10 additions & 0 deletions packages/material-ui-utils/src/deepmerge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ describe('deepmerge', () => {
});
expect({}.isAdmin).to.equal(undefined);
});

// https://github.com/mui-org/material-ui/issues/20095
it('should not merge DOM elements', () => {
const element = document.createElement('div');
const element2 = document.createElement('div');

const result = deepmerge({ element }, { element: element2 });

expect(result.element).to.equal(element2);
});
});

0 comments on commit bbcc43d

Please sign in to comment.