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(helpers): only deep merge plain objects #20284

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions packages/vuetify/src/util/__tests__/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ describe('helpers', () => {
it('should use arrayFn function if provided', () => {
expect(mergeDeep({ a: ['foo'] }, { a: ['bar'] }, (a, b) => [...a, ...b])).toEqual({ a: ['foo', 'bar'] })
})

it('should not recursively merge non-plain objects', () => {
const plain = { a: 'foo' }
const div = document.createElement('div')
const span = document.createElement('span')

expect(mergeDeep({ a: plain }, { a: div }).a).toBe(div)
expect(mergeDeep({ a: div }, { a: plain }).a).toBe(plain)
expect(mergeDeep({ a: div }, { a: span }).a).toBe(span)
})
})

describe('destructComputed', () => {
Expand Down
17 changes: 11 additions & 6 deletions packages/vuetify/src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export function isObject (obj: any): obj is Record<string, any> {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj)
}

export function isPlainObject (obj: any): obj is Record<string, any> {
let proto
return obj !== null && typeof obj === 'object' && (
(proto = Object.getPrototypeOf(obj)) === Object.prototype ||
proto === null
)
}

export function refElement (obj?: ComponentPublicInstance<any> | HTMLElement): HTMLElement | undefined {
if (obj && '$el' in obj) {
const el = obj.$el as HTMLElement
Expand Down Expand Up @@ -487,17 +495,14 @@ export function mergeDeep (
const targetProperty = target[key]

// Only continue deep merging if
// both properties are objects
if (
isObject(sourceProperty) &&
isObject(targetProperty)
) {
// both properties are plain objects
if (isPlainObject(sourceProperty) && isPlainObject(targetProperty)) {
out[key] = mergeDeep(sourceProperty, targetProperty, arrayFn)

continue
}

if (Array.isArray(sourceProperty) && Array.isArray(targetProperty) && arrayFn) {
if (arrayFn && Array.isArray(sourceProperty) && Array.isArray(targetProperty)) {
out[key] = arrayFn(sourceProperty, targetProperty)

continue
Expand Down