Skip to content

Commit a2e35d6

Browse files
authored
fix(runtime-dom): ensure only symbols are explicitly stringified during attribute patching (#11182)
close #11177
1 parent 7936dae commit a2e35d6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/runtime-dom/__tests__/patchAttrs.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,23 @@ describe('runtime-dom: attrs patching', () => {
6969
patchProp(el, 'value', null, symbol)
7070
expect(el.value).toBe(symbol.toString())
7171
})
72+
73+
// #11177
74+
test('should allow setting value to object, leaving stringification to the element/browser', () => {
75+
// normal behavior
76+
const el = document.createElement('div')
77+
const obj = { toString: () => 'foo' }
78+
patchProp(el, 'data-test', null, obj)
79+
expect(el.dataset.test).toBe('foo')
80+
81+
const el2 = document.createElement('div')
82+
let testvalue: null | typeof obj = null
83+
// simulating a web component that implements its own setAttribute handler
84+
el2.setAttribute = (name, value) => {
85+
testvalue = value
86+
}
87+
patchProp(el2, 'data-test', null, obj)
88+
expect(el2.dataset.test).toBe(undefined)
89+
expect(testvalue).toBe(obj)
90+
})
7291
})

packages/runtime-dom/src/modules/attrs.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
NOOP,
33
includeBooleanAttr,
44
isSpecialBooleanAttr,
5+
isSymbol,
56
makeMap,
67
} from '@vue/shared'
78
import {
@@ -37,7 +38,10 @@ export function patchAttr(
3738
el.removeAttribute(key)
3839
} else {
3940
// attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
40-
el.setAttribute(key, isBoolean ? '' : String(value))
41+
el.setAttribute(
42+
key,
43+
isBoolean ? '' : isSymbol(value) ? String(value) : value,
44+
)
4145
}
4246
}
4347
}

0 commit comments

Comments
 (0)