File tree 2 files changed +30
-12
lines changed
2 files changed +30
-12
lines changed Original file line number Diff line number Diff line change @@ -172,6 +172,23 @@ describe('defineCustomElement', () => {
172
172
expect ( e . shadowRoot ! . innerHTML ) . toBe ( `20 number true boolean 2e1 string` )
173
173
} )
174
174
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
+
175
192
test ( 'handling properties set before upgrading' , ( ) => {
176
193
const E = defineCustomElement ( {
177
194
props : [ 'foo' ] ,
Original file line number Diff line number Diff line change @@ -174,17 +174,6 @@ export class VueElement extends BaseClass {
174
174
}
175
175
this . attachShadow ( { mode : 'open' } )
176
176
}
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 } )
188
177
}
189
178
190
179
connectedCallback ( ) {
@@ -212,9 +201,21 @@ export class VueElement extends BaseClass {
212
201
if ( this . _resolved ) {
213
202
return
214
203
}
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 } )
215
217
216
218
const resolve = ( def : InnerComponentDef ) => {
217
- this . _resolved = true
218
219
const { props, styles } = def
219
220
const hasOptions = ! isArray ( props )
220
221
const rawKeys = props ? ( hasOptions ? Object . keys ( props ) : props ) : [ ]
You can’t perform that action at this time.
0 commit comments