Skip to content

Commit 1701bf3

Browse files
stevijoStefan Mayer
andauthored
fix(runtime-dom): patchDOMProps should not set _value if element is custom element (vuejs#4839)
Co-authored-by: Stefan Mayer <stefan.m.mayer@daimler.com>
1 parent aac0466 commit 1701bf3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@ describe('runtime-dom: props patching', () => {
2525
expect((el as any)._value).toBe(obj)
2626
})
2727

28+
test('value for custom elements', () => {
29+
class TestElement extends HTMLElement {
30+
constructor() {
31+
super()
32+
}
33+
34+
// intentionally uses _value because this is used in "normal" HTMLElement for storing the object of the set property value
35+
private _value: any
36+
get value() {
37+
return this._value
38+
}
39+
40+
set value(val) {
41+
this._value = val
42+
this.setterCalled++
43+
}
44+
45+
public setterCalled: number = 0
46+
}
47+
window.customElements.define('test-element', TestElement)
48+
const el = document.createElement('test-element') as TestElement
49+
patchProp(el, 'value', null, 'foo')
50+
expect(el.value).toBe('foo')
51+
expect(el.setterCalled).toBe(1)
52+
patchProp(el, 'value', null, null)
53+
expect(el.value).toBe('')
54+
expect(el.setterCalled).toBe(2)
55+
expect(el.getAttribute('value')).toBe(null)
56+
const obj = {}
57+
patchProp(el, 'value', null, obj)
58+
expect(el.value).toBe(obj)
59+
expect(el.setterCalled).toBe(3)
60+
})
61+
2862
// For <input type="text">, setting el.value won't create a `value` attribute
2963
// so we need to add tests for other elements
3064
test('value for non-text input', () => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export function patchDOMProp(
2626
return
2727
}
2828

29-
if (key === 'value' && el.tagName !== 'PROGRESS') {
29+
if (
30+
key === 'value' &&
31+
el.tagName !== 'PROGRESS' &&
32+
// custom elements may use _value internally
33+
!el.tagName.includes('-')
34+
) {
3035
// store value as _value as well since
3136
// non-string values will be stringified.
3237
el._value = value

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy