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(customElement): properly remove hyphenated attribute #12143

Merged
merged 3 commits into from
Oct 11, 2024
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
35 changes: 35 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,39 @@ describe('defineCustomElement', () => {
await nextTick()
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>`,
)
})
})
3 changes: 2 additions & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function patchDOMProp(
key: string,
value: any,
parentComponent: any,
attrName?: string,
): void {
// __UNSAFE__
// Reason: potentially setting innerHTML.
Expand Down Expand Up @@ -106,5 +107,5 @@ export function patchDOMProp(
)
}
}
needRemove && el.removeAttribute(key)
needRemove && el.removeAttribute(attrName || key)
}
2 changes: 1 addition & 1 deletion packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
(el as VueElement)._isVueCE &&
(/[A-Z]/.test(key) || !isString(nextValue))
) {
patchDOMProp(el, camelize(key), nextValue, parentComponent)
patchDOMProp(el, camelize(key), nextValue, parentComponent, key)
} else {
// special case for <input v-model type="checkbox"> with
// :true-value & :false-value
Expand Down