File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed
packages/mui-utils/src/deepmerge Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
1
2
import { expect } from 'chai' ;
2
3
import { runInNewContext } from 'vm' ;
3
4
import deepmerge from './deepmerge' ;
@@ -122,4 +123,12 @@ describe('deepmerge', () => {
122
123
expect ( result ) . to . deep . equal ( { foo : { baz : 'new test' } } ) ;
123
124
expect ( foo ) . to . deep . equal ( { foo : { baz : 'test' } } ) ;
124
125
} ) ;
126
+
127
+ it ( 'should not deep clone React element' , ( ) => {
128
+ const element = React . createElement ( 'div' , { } , React . createElement ( 'span' ) ) ;
129
+ const element2 = React . createElement ( 'a' ) ;
130
+ const result = deepmerge ( { element } , { element : element2 } ) ;
131
+
132
+ expect ( result . element ) . to . equal ( element2 ) ;
133
+ } ) ;
125
134
} ) ;
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+
1
3
// https://github.com/sindresorhus/is-plain-obj/blob/main/index.js
2
4
export function isPlainObject ( item : unknown ) : item is Record < keyof any , unknown > {
3
5
if ( typeof item !== 'object' || item === null ) {
@@ -19,7 +21,7 @@ export interface DeepmergeOptions {
19
21
}
20
22
21
23
function deepClone < T > ( source : T ) : T | Record < keyof any , unknown > {
22
- if ( ! isPlainObject ( source ) ) {
24
+ if ( React . isValidElement ( source ) || ! isPlainObject ( source ) ) {
23
25
return source ;
24
26
}
25
27
@@ -41,7 +43,9 @@ export default function deepmerge<T>(
41
43
42
44
if ( isPlainObject ( target ) && isPlainObject ( source ) ) {
43
45
Object . keys ( source ) . forEach ( ( key ) => {
44
- if (
46
+ if ( React . isValidElement ( source [ key ] ) ) {
47
+ ( output as Record < keyof any , unknown > ) [ key ] = source [ key ] ;
48
+ } else if (
45
49
isPlainObject ( source [ key ] ) &&
46
50
// Avoid prototype pollution
47
51
Object . prototype . hasOwnProperty . call ( target , key ) &&
You can’t perform that action at this time.
0 commit comments