Skip to content

Commit 188f3ae

Browse files
authoredJun 10, 2024··
fix(runtime-dom): support Symbol for input value bindings (#10608)
close #10597
1 parent 612bbf0 commit 188f3ae

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed
 

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

+16
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ describe('runtime-dom: attrs patching', () => {
5353
patchProp(el, 'onwards', 'a', null)
5454
expect(el.getAttribute('onwards')).toBe(null)
5555
})
56+
57+
// #10597
58+
test('should allow setting attribute to symbol', () => {
59+
const el = document.createElement('div')
60+
const symbol = Symbol('foo')
61+
patchProp(el, 'foo', null, symbol)
62+
expect(el.getAttribute('foo')).toBe(symbol.toString())
63+
})
64+
65+
// #10598
66+
test('should allow setting value to symbol', () => {
67+
const el = document.createElement('input')
68+
const symbol = Symbol('foo')
69+
patchProp(el, 'value', null, symbol)
70+
expect(el.value).toBe(symbol.toString())
71+
})
5672
})

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export function patchAttr(
3636
if (value == null || (isBoolean && !includeBooleanAttr(value))) {
3737
el.removeAttribute(key)
3838
} else {
39-
el.setAttribute(key, isBoolean ? '' : value)
39+
// attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
40+
el.setAttribute(key, isBoolean ? '' : String(value))
4041
}
4142
}
4243
}

‎packages/runtime-dom/src/modules/props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function patchDOMProp(
3838
// compare against its attribute value instead.
3939
const oldValue =
4040
tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
41-
const newValue = value == null ? '' : value
41+
const newValue = value == null ? '' : String(value)
4242
if (oldValue !== newValue || !('_value' in el)) {
4343
el.value = newValue
4444
}

0 commit comments

Comments
 (0)
Please sign in to comment.