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(patchProp): prevent setting state as attribute for custom elements #11165

Merged
merged 2 commits into from
Jun 22, 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
9 changes: 8 additions & 1 deletion packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
inject,
nextTick,
ref,
render,
renderSlot,
} from '../src'

Expand Down Expand Up @@ -137,7 +138,7 @@ describe('defineCustomElement', () => {

describe('props', () => {
const E = defineCustomElement({
props: ['foo', 'bar', 'bazQux'],
props: ['foo', 'bar', 'bazQux', 'value'],
render() {
return [
h('div', null, this.foo),
Expand All @@ -147,6 +148,12 @@ describe('defineCustomElement', () => {
})
customElements.define('my-el-props', E)

test('renders custom element w/ correct object prop value', () => {
render(h('my-el-props', { value: { x: 1 } }), container)
const el = container.children[0]
expect((el as any).value).toEqual({ x: 1 })
})

test('props via attribute', async () => {
// bazQux should map to `baz-qux` attribute
container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
)
// #6007 also set form state as attributes so they work with
// <input type="reset"> or libs / extensions that expect attributes
if (key === 'value' || key === 'checked' || key === 'selected') {
// #11163 custom elements may use value as an prop and set it as object
if (
!el.tagName.includes('-') &&
(key === 'value' || key === 'checked' || key === 'selected')
) {
patchAttr(el, key, nextValue, isSVG, parentComponent, key !== 'value')
}
} else {
Expand Down