Skip to content

Commit

Permalink
fix(runtime-dom): support Symbol for input value bindings (#10608)
Browse files Browse the repository at this point in the history
close #10597
  • Loading branch information
xw616525957 authored Jun 10, 2024
1 parent 612bbf0 commit 188f3ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/runtime-dom/__tests__/patchAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ describe('runtime-dom: attrs patching', () => {
patchProp(el, 'onwards', 'a', null)
expect(el.getAttribute('onwards')).toBe(null)
})

// #10597
test('should allow setting attribute to symbol', () => {
const el = document.createElement('div')
const symbol = Symbol('foo')
patchProp(el, 'foo', null, symbol)
expect(el.getAttribute('foo')).toBe(symbol.toString())
})

// #10598
test('should allow setting value to symbol', () => {
const el = document.createElement('input')
const symbol = Symbol('foo')
patchProp(el, 'value', null, symbol)
expect(el.value).toBe(symbol.toString())
})
})
3 changes: 2 additions & 1 deletion packages/runtime-dom/src/modules/attrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function patchAttr(
if (value == null || (isBoolean && !includeBooleanAttr(value))) {
el.removeAttribute(key)
} else {
el.setAttribute(key, isBoolean ? '' : value)
// attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
el.setAttribute(key, isBoolean ? '' : String(value))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function patchDOMProp(
// compare against its attribute value instead.
const oldValue =
tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
const newValue = value == null ? '' : value
const newValue = value == null ? '' : String(value)
if (oldValue !== newValue || !('_value' in el)) {
el.value = newValue
}
Expand Down

0 comments on commit 188f3ae

Please sign in to comment.