Skip to content

Commit

Permalink
fix(customElement): warn if attr is tagName
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Oct 8, 2024
1 parent 35785f3 commit b1a12d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,4 +1386,22 @@ describe('defineCustomElement', () => {
await nextTick()
expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
})

test('avoid overriding tagName', async () => {
const E = defineCustomElement({
props: {
tagName: {
type: String,
},
},
render() {
return this.tagName
},
})
customElements.define('el-attr-tag-name', E)
container.innerHTML = '<el-attr-tag-name tag-name="foo">'
const e = container.childNodes[0] as VueElement
expect(e.tagName).toBe(`EL-ATTR-TAG-NAME`)
expect(`[Vue warn]: Failed setting prop "tagName" `).toHaveBeenWarned()
})
})
10 changes: 10 additions & 0 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ export class VueElement

// defining getter/setters on prototype
for (const key of declaredPropKeys.map(camelize)) {
if (key === 'tagName') continue
Object.defineProperty(this, key, {
get() {
return this._getProp(key)
Expand Down Expand Up @@ -490,6 +491,15 @@ export class VueElement
shouldReflect = true,
shouldUpdate = false,
): void {
if (key === 'tagName') {
if (__DEV__) {
warn(
`Failed setting prop "${key}" on <${this.tagName.toLowerCase()}>: ` +
`TypeError: Cannot set property tagName of #<Element> which has only a getter`,
)
}
return
}
if (val !== this._props[key]) {
if (val === REMOVAL) {
delete this._props[key]
Expand Down

0 comments on commit b1a12d9

Please sign in to comment.