Skip to content

Commit 3ca8317

Browse files
committed
fix(custom-element): fix initial attr type casting for programmtically created elements
fix #4772
1 parent c803eb1 commit 3ca8317

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ describe('defineCustomElement', () => {
172172
expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean 2e1 string`)
173173
})
174174

175+
// #4772
176+
test('attr casting w/ programmatic creation', () => {
177+
const E = defineCustomElement({
178+
props: {
179+
foo: Number
180+
},
181+
render() {
182+
return `foo type: ${typeof this.foo}`
183+
}
184+
})
185+
customElements.define('my-element-programmatic', E)
186+
const el = document.createElement('my-element-programmatic') as any
187+
el.setAttribute('foo', '123')
188+
container.appendChild(el)
189+
expect(el.shadowRoot.innerHTML).toBe(`foo type: number`)
190+
})
191+
175192
test('handling properties set before upgrading', () => {
176193
const E = defineCustomElement({
177194
props: ['foo'],

packages/runtime-dom/src/apiCustomElement.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,6 @@ export class VueElement extends BaseClass {
174174
}
175175
this.attachShadow({ mode: 'open' })
176176
}
177-
178-
// set initial attrs
179-
for (let i = 0; i < this.attributes.length; i++) {
180-
this._setAttr(this.attributes[i].name)
181-
}
182-
// watch future attr changes
183-
new MutationObserver(mutations => {
184-
for (const m of mutations) {
185-
this._setAttr(m.attributeName!)
186-
}
187-
}).observe(this, { attributes: true })
188177
}
189178

190179
connectedCallback() {
@@ -212,9 +201,21 @@ export class VueElement extends BaseClass {
212201
if (this._resolved) {
213202
return
214203
}
204+
this._resolved = true
205+
206+
// set initial attrs
207+
for (let i = 0; i < this.attributes.length; i++) {
208+
this._setAttr(this.attributes[i].name)
209+
}
210+
211+
// watch future attr changes
212+
new MutationObserver(mutations => {
213+
for (const m of mutations) {
214+
this._setAttr(m.attributeName!)
215+
}
216+
}).observe(this, { attributes: true })
215217

216218
const resolve = (def: InnerComponentDef) => {
217-
this._resolved = true
218219
const { props, styles } = def
219220
const hasOptions = !isArray(props)
220221
const rawKeys = props ? (hasOptions ? Object.keys(props) : props) : []

0 commit comments

Comments
 (0)