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(runtime-dom): patchDOMProps should not set _value if element is c… #4839

Merged
merged 1 commit into from
Nov 25, 2021
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
34 changes: 34 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,40 @@ describe('runtime-dom: props patching', () => {
expect((el as any)._value).toBe(obj)
})

test('value for custom elements', () => {
class TestElement extends HTMLElement {
constructor() {
super()
}

// intentionally uses _value because this is used in "normal" HTMLElement for storing the object of the set property value
private _value: any
get value() {
return this._value
}

set value(val) {
this._value = val
this.setterCalled++
}

public setterCalled: number = 0
}
window.customElements.define('test-element', TestElement)
const el = document.createElement('test-element') as TestElement
patchProp(el, 'value', null, 'foo')
expect(el.value).toBe('foo')
expect(el.setterCalled).toBe(1)
patchProp(el, 'value', null, null)
expect(el.value).toBe('')
expect(el.setterCalled).toBe(2)
expect(el.getAttribute('value')).toBe(null)
const obj = {}
patchProp(el, 'value', null, obj)
expect(el.value).toBe(obj)
expect(el.setterCalled).toBe(3)
})

// For <input type="text">, setting el.value won't create a `value` attribute
// so we need to add tests for other elements
test('value for non-text input', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export function patchDOMProp(
return
}

if (key === 'value' && el.tagName !== 'PROGRESS') {
if (
key === 'value' &&
el.tagName !== 'PROGRESS' &&
// custom elements may use _value internally
!el.tagName.includes('-')
) {
// store value as _value as well since
// non-string values will be stringified.
el._value = value
Expand Down