fix(custom-element): properly remove hyphenated attribute (#12143)

close #12139
This commit is contained in:
edison 2024-10-11 09:52:06 +08:00 committed by GitHub
parent 35785f3cd7
commit e16e9a7341
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 2 deletions

View File

@ -1386,4 +1386,39 @@ describe('defineCustomElement', () => {
await nextTick() await nextTick()
expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`) expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
}) })
test('hyphenated attr removal', async () => {
const E = defineCustomElement({
props: {
fooBar: {
type: Boolean,
},
},
render() {
return this.fooBar
},
})
customElements.define('el-hyphenated-attr-removal', E)
const toggle = ref(true)
const Comp = {
render() {
return h('el-hyphenated-attr-removal', {
'foo-bar': toggle.value ? '' : null,
})
},
}
render(h(Comp), container)
const el = container.children[0]
expect(el.hasAttribute('foo-bar')).toBe(true)
expect((el as any).outerHTML).toBe(
`<el-hyphenated-attr-removal foo-bar=""></el-hyphenated-attr-removal>`,
)
toggle.value = false
await nextTick()
expect(el.hasAttribute('foo-bar')).toBe(false)
expect((el as any).outerHTML).toBe(
`<el-hyphenated-attr-removal></el-hyphenated-attr-removal>`,
)
})
}) })

View File

@ -8,6 +8,7 @@ export function patchDOMProp(
key: string, key: string,
value: any, value: any,
parentComponent: any, parentComponent: any,
attrName?: string,
): void { ): void {
// __UNSAFE__ // __UNSAFE__
// Reason: potentially setting innerHTML. // Reason: potentially setting innerHTML.
@ -106,5 +107,5 @@ export function patchDOMProp(
) )
} }
} }
needRemove && el.removeAttribute(key) needRemove && el.removeAttribute(attrName || key)
} }

View File

@ -62,7 +62,7 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
(el as VueElement)._isVueCE && (el as VueElement)._isVueCE &&
(/[A-Z]/.test(key) || !isString(nextValue)) (/[A-Z]/.test(key) || !isString(nextValue))
) { ) {
patchDOMProp(el, camelize(key), nextValue, parentComponent) patchDOMProp(el, camelize(key), nextValue, parentComponent, key)
} else { } else {
// special case for <input v-model type="checkbox"> with // special case for <input v-model type="checkbox"> with
// :true-value & :false-value // :true-value & :false-value