@@ -70,7 +70,7 @@ export const checkHasSpecialType = (obj) => {
7070 return false
7171}
7272
73- const handleJSExpressionBinding = ( key , value , isJSX ) => {
73+ const handleJSExpressionBinding = ( key , value , isJSX , globalHooks ) => {
7474 const expressValue = value . value . replace ( isJSX ? thisRegexp : thisPropsBindRe , '' )
7575
7676 if ( isJSX ) {
@@ -85,7 +85,19 @@ const handleJSExpressionBinding = (key, value, isJSX) => {
8585 }
8686
8787 // expression 使用 v-bind 绑定
88- return `:${ key } ="${ expressValue } "`
88+ if ( expressValue . includes ( '"' ) ) {
89+ let stateKey = `${ key } _${ randomString ( ) } `
90+ let addSuccess = globalHooks . addState ( stateKey , `${ stateKey } :${ expressValue } ` )
91+
92+ while ( ! addSuccess ) {
93+ stateKey = `${ key } _${ randomString ( ) } `
94+ addSuccess = globalHooks . addState ( stateKey , `${ stateKey } :${ expressValue } ` )
95+ }
96+
97+ return `:${ key } ="state.${ stateKey } "`
98+ } else {
99+ return `:${ key } ="${ expressValue } "`
100+ }
89101}
90102
91103const handleBindI18n = ( key , value , isJSX ) => {
@@ -182,7 +194,19 @@ export const handleLoopAttrHook = (schemaData = {}, globalHooks, config) => {
182194 const iterVar = [ ...loopArgs ]
183195
184196 if ( ! isJSX ) {
185- attributes . push ( `v-for="(${ iterVar . join ( ',' ) } ) in ${ source } "` )
197+ if ( source . includes ( '"' ) ) {
198+ let stateKey = `loop_${ randomString ( ) } `
199+ let addSuccess = globalHooks . addState ( stateKey , `${ stateKey } :${ source } ` )
200+
201+ while ( ! addSuccess ) {
202+ stateKey = `loop_${ randomString ( ) } `
203+ addSuccess = globalHooks . addState ( stateKey , `${ stateKey } :${ source } ` )
204+ }
205+
206+ attributes . push ( `v-for="(${ iterVar . join ( ',' ) } ) in state.${ stateKey } "` )
207+ } else {
208+ attributes . push ( `v-for="(${ iterVar . join ( ',' ) } ) in ${ source } "` )
209+ }
186210
187211 return
188212 }
@@ -352,7 +376,7 @@ export const handleExpressionAttrHook = (schemaData, globalHooks, config) => {
352376 Object . entries ( props ) . forEach ( ( [ key , value ] ) => {
353377 if ( value ?. type === JS_EXPRESSION && ! isOn ( key ) ) {
354378 specialTypeHandler [ JS_RESOURCE ] ( value , globalHooks , config )
355- attributes . push ( handleJSExpressionBinding ( key , value , isJSX ) )
379+ attributes . push ( handleJSExpressionBinding ( key , value , isJSX , globalHooks ) )
356380
357381 delete props [ key ]
358382 }
@@ -384,7 +408,7 @@ export const handleJSFunctionAttrHook = (schemaData, globalHooks, config) => {
384408 functionName = value . value
385409 }
386410
387- attributes . push ( handleJSExpressionBinding ( key , { value : functionName } , isJSX ) )
411+ attributes . push ( handleJSExpressionBinding ( key , { value : functionName } , isJSX , globalHooks ) )
388412
389413 delete props [ key ]
390414 }
@@ -451,11 +475,15 @@ const genStateAccessor = (value, globalHooks) => {
451475 }
452476}
453477
454- const transformObjValue = ( renderKey , value , globalHooks , config , transformObjType ) => {
478+ const transformObjValue = ( renderKey , value , globalHooks , config , transformObjType , shouldConvertQuote = false ) => {
455479 const result = { shouldBindToState : false , res : null }
456480
457481 if ( typeof value === 'string' ) {
458- result . res = `${ renderKey } "${ value . replaceAll ( "'" , "\\'" ) . replaceAll ( / " / g, "'" ) } "`
482+ if ( shouldConvertQuote ) {
483+ result . res = `${ renderKey } '${ value . replaceAll ( / " / g, "'" ) . replaceAll ( / ' / g, "\\'" ) } '`
484+ } else {
485+ result . res = `${ renderKey } "${ value . replaceAll ( "'" , "\\'" ) . replaceAll ( / " / g, "'" ) } "`
486+ }
459487
460488 return result
461489 }
@@ -468,7 +496,11 @@ const transformObjValue = (renderKey, value, globalHooks, config, transformObjTy
468496
469497 if ( specialTypeHandler [ value ?. type ] ) {
470498 const specialVal = specialTypeHandler [ value . type ] ( value , globalHooks , config ) ?. value || ''
471- result . res = `${ renderKey } ${ specialVal } `
499+ if ( shouldConvertQuote ) {
500+ result . res = `${ renderKey } ${ specialVal . replaceAll ( / " / g, "'" ) } `
501+ } else {
502+ result . res = `${ renderKey } ${ specialVal } `
503+ }
472504
473505 if ( specialTypes . includes ( value . type ) ) {
474506 result . shouldBindToState = true
@@ -503,7 +535,7 @@ const transformObjValue = (renderKey, value, globalHooks, config, transformObjTy
503535}
504536
505537const normalKeyRegexp = / ^ [ a - z A - Z _ $ ] [ a - z A - Z 0 - 9 _ $ ] * $ /
506- export const transformObjType = ( obj , globalHooks , config ) => {
538+ export const transformObjType = ( obj , globalHooks , config , shouldConvertQuote = false ) => {
507539 if ( ! obj || typeof obj !== 'object' ) {
508540 return {
509541 res : obj
@@ -527,7 +559,8 @@ export const transformObjType = (obj, globalHooks, config) => {
527559 value ,
528560 globalHooks ,
529561 config ,
530- transformObjType
562+ transformObjType ,
563+ shouldConvertQuote
531564 )
532565
533566 if ( tmpShouldBindToState ) {
@@ -541,7 +574,7 @@ export const transformObjType = (obj, globalHooks, config) => {
541574
542575 // 复杂的 object 类型,需要递归处理
543576 const { res : tempRes , shouldBindToState : tempShouldBindToState } =
544- transformObjType ( value , globalHooks , config ) || { }
577+ transformObjType ( value , globalHooks , config , shouldConvertQuote ) || { }
545578
546579 resStr . push ( `${ renderKey } ${ tempRes } ` )
547580
@@ -570,7 +603,7 @@ export const handleObjBindAttrHook = (schemaData, globalHooks, config) => {
570603 return
571604 }
572605
573- const { res, shouldBindToState } = transformObjType ( value , globalHooks , config )
606+ const { res, shouldBindToState } = transformObjType ( value , globalHooks , config , true )
574607
575608 if ( shouldBindToState && ! isJSX ) {
576609 let stateKey = key
@@ -583,7 +616,7 @@ export const handleObjBindAttrHook = (schemaData, globalHooks, config) => {
583616
584617 attributes . push ( `:${ key } ="state.${ stateKey } "` )
585618 } else {
586- attributes . push ( isJSX ? `${ key } ={${ res } }` : `:${ key } ="${ res . replaceAll ( / " / g , "'" ) } "` )
619+ attributes . push ( isJSX ? `${ key } ={${ res } }` : `:${ key } ="${ res } "` )
587620 }
588621
589622 delete props [ key ]
0 commit comments