Skip to content

Commit f45f72c

Browse files
committed
fix: only save to state when contain both double and single quote
1 parent ffc66b8 commit f45f72c

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

packages/vue-generator/src/generator/vue/sfc/generateAttribute.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const handleJSExpressionBinding = (key, value, isJSX, globalHooks) => {
8585
}
8686

8787
// expression 使用 v-bind 绑定
88-
if (expressValue.includes('"')) {
88+
if (expressValue.includes('"') && expressValue.includes("'")) {
8989
let stateKey = `${key}_${randomString()}`
9090
let addSuccess = globalHooks.addState(stateKey, `${stateKey}:${expressValue}`)
9191

@@ -96,7 +96,7 @@ const handleJSExpressionBinding = (key, value, isJSX, globalHooks) => {
9696

9797
return `:${key}="state.${stateKey}"`
9898
} else {
99-
return `:${key}="${expressValue}"`
99+
return `:${key}="${expressValue.replaceAll(/"/g, "'")}"`
100100
}
101101
}
102102

@@ -188,13 +188,13 @@ export const handleLoopAttrHook = (schemaData = {}, globalHooks, config) => {
188188
if (loop?.value && loop?.type) {
189189
source = loop.value.replace(isJSX ? thisRegexp : thisPropsBindRe, '')
190190
} else {
191-
source = JSON.stringify(loop).replaceAll("'", "\\'").replaceAll(/"/g, "'")
191+
source = JSON.stringify(loop)
192192
}
193193

194194
const iterVar = [...loopArgs]
195195

196196
if (!isJSX) {
197-
if (source.includes('"')) {
197+
if (source.includes('"') && source.includes("'")) {
198198
let stateKey = `loop_${randomString()}`
199199
let addSuccess = globalHooks.addState(stateKey, `${stateKey}:${source}`)
200200

@@ -205,7 +205,7 @@ export const handleLoopAttrHook = (schemaData = {}, globalHooks, config) => {
205205

206206
attributes.push(`v-for="(${iterVar.join(',')}) in state.${stateKey}"`)
207207
} else {
208-
attributes.push(`v-for="(${iterVar.join(',')}) in ${source}"`)
208+
attributes.push(`v-for="(${iterVar.join(',')}) in ${source.replaceAll(/"/g, "'")}"`)
209209
}
210210

211211
return

packages/vue-generator/test/testcases/sfc/templateQuote/expected/templateQuote.vue

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
type="primary"
66
text="test"
77
subStr="pri'ma'ry'subStr'"
8-
:customExpressionTest="state.customExpressionTest_vBHN"
8+
:customExpressionTest="{
9+
value: [
10+
{
11+
defaultValue: '{\'class\': \'test-class\', \'id\': \'test-id\'}'
12+
}
13+
]
14+
}"
915
:customAttrTest="{
1016
value: [
1117
{
@@ -52,13 +58,6 @@ const state = vue.reactive({
5258
type: 'danger'
5359
}
5460
],
55-
customExpressionTest_vBHN: {
56-
value: [
57-
{
58-
defaultValue: '{"class": "test-class", "id": "test-id"}'
59-
}
60-
]
61-
},
6261
customAttrTest: { value: [{ defaultValue: "{'class': 'test-class', 'id': 'test-id'}" }] }
6362
})
6463
wrap({ state })

0 commit comments

Comments
 (0)