Skip to content

Commit 22b53ea

Browse files
authored
fix(custom-element): warn when props override native properties (#12125)
close #12124
1 parent 02421cd commit 22b53ea

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ describe('defineCustomElement', () => {
237237
await nextTick()
238238
expect(e.shadowRoot!.innerHTML).toBe('<div>foo2</div><div>bar2</div>')
239239
expect(e.getAttribute('foo')).toBe('foo2')
240+
expect(e.foo).toBe('foo2')
240241
expect(e.hasAttribute('bar')).toBe(false)
241242

242243
// change prop then attr
@@ -2489,6 +2490,76 @@ describe('defineCustomElement', () => {
24892490
expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
24902491
})
24912492

2493+
test('prop name conflicts with native property', () => {
2494+
const E = defineCustomElement({
2495+
props: {
2496+
tagName: {
2497+
type: String,
2498+
},
2499+
},
2500+
render() {
2501+
return this.tagName
2502+
},
2503+
})
2504+
customElements.define('el-attr-tag-name', E)
2505+
container.innerHTML = '<el-attr-tag-name tag-name="foo">'
2506+
const e = container.childNodes[0] as VueElement
2507+
expect(e.shadowRoot!.innerHTML).toBe(`foo`)
2508+
expect(e.tagName).toBe('foo')
2509+
expect(
2510+
`[Vue warn]: Custom element prop "tagName" conflicts with an existing property on the element and will overwrite it.`,
2511+
).toHaveBeenWarned()
2512+
})
2513+
2514+
test('prop name conflicts with reflected native property', async () => {
2515+
const E = defineCustomElement({
2516+
props: {
2517+
id: String,
2518+
},
2519+
render() {
2520+
return this.id
2521+
},
2522+
})
2523+
customElements.define('el-attr-id', E)
2524+
container.innerHTML = '<el-attr-id id="foo">'
2525+
const e = container.childNodes[0] as VueElement
2526+
2527+
e.setAttribute('id', 'bar')
2528+
await nextTick()
2529+
expect(e.shadowRoot!.innerHTML).toBe(`bar`)
2530+
expect(e.id).toBe('bar')
2531+
expect(
2532+
`[Vue warn]: Custom element prop "id" conflicts with an existing property on the element and will overwrite it.`,
2533+
).toHaveBeenWarned()
2534+
})
2535+
2536+
test('native method conflict when rendered by Vue', () => {
2537+
const value = { text: 'foo' }
2538+
const E = defineCustomElement({
2539+
props: {
2540+
hasAttribute: Object,
2541+
},
2542+
render() {
2543+
return this.hasAttribute!.text
2544+
},
2545+
})
2546+
customElements.define('el-vue-has-attribute', E)
2547+
2548+
render(
2549+
h('el-vue-has-attribute', {
2550+
'has-attribute': value,
2551+
}),
2552+
container,
2553+
)
2554+
2555+
const e = container.firstChild as VueElement
2556+
expect(e.shadowRoot!.innerHTML).toBe('foo')
2557+
expect((e as any).hasAttribute).toBe(value)
2558+
expect(
2559+
`[Vue warn]: Custom element prop "hasAttribute" conflicts with an existing property on the element and will overwrite it.`,
2560+
).toHaveBeenWarned()
2561+
})
2562+
24922563
test('hyphenated attr removal', async () => {
24932564
const E = defineCustomElement({
24942565
props: {

‎packages/runtime-dom/src/apiCustomElement.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,14 @@ export class VueElement
477477
}
478478
}
479479

480-
// defining getter/setters on prototype
480+
// define getter/setters for declared props
481481
for (const key of declaredPropKeys.map(camelize)) {
482+
if (__DEV__ && key in Object.getPrototypeOf(this)) {
483+
warn(
484+
`Custom element prop "${key}" conflicts with an existing property ` +
485+
`on the element and will overwrite it.`,
486+
)
487+
}
482488
Object.defineProperty(this, key, {
483489
get(this: VueElement) {
484490
return this._getProp(key)

0 commit comments

Comments
 (0)