Skip to content

Commit 95d49bf

Browse files
edison1105yyx990803
authored andcommitted
fix(compiler-sfc): support complex expression in CSS v-bind() (vuejs#5114)
fix vuejs#5109
1 parent 63210fe commit 95d49bf

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

Diff for: packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap

+21
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ return { color, width }
6666
}"
6767
`;
6868
69+
exports[`CSS vars injection codegen should work with w/ complex expression 1`] = `
70+
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
71+
72+
export default {
73+
setup(__props, { expose }) {
74+
expose();
75+
76+
_useCssVars(_ctx => ({
77+
\\"xxxxxxxx-_a___b____2____px__\\": ((_unref(a) + _unref(b)) / 2 + 'px' ),
78+
\\"xxxxxxxx-__a___b______2___a_\\": (((_unref(a) + _unref(b))) / (2 * _unref(a)))
79+
}))
80+
81+
let a = 100
82+
let b = 200
83+
84+
return { a, b }
85+
}
86+
87+
}"
88+
`;
89+
6990
exports[`CSS vars injection codegen w/ <script setup> 1`] = `
7091
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
7192

Diff for: packages/compiler-sfc/__tests__/cssVars.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,31 @@ describe('CSS vars injection', () => {
195195
// color should only be injected once, even if it is twice in style
196196
expect(content).toMatch(`_useCssVars(_ctx => ({
197197
"${mockId}-color": (color)
198+
})`)
199+
assertCode(content)
200+
})
201+
202+
test('should work with w/ complex expression', () => {
203+
const { content } = compileSFCScript(
204+
`<script setup>
205+
let a = 100
206+
let b = 200
207+
</script>\n` +
208+
`<style>
209+
div {
210+
color: v-bind((a + b) / 2 + 'px' );
211+
}
212+
div {
213+
color: v-bind ((a + b) / 2 + 'px' );
214+
}
215+
p {
216+
color: v-bind(((a + b)) / (2 * a));
217+
}
218+
</style>`
219+
)
220+
expect(content).toMatch(`_useCssVars(_ctx => ({
221+
"${mockId}-_a___b____2____px__": ((_unref(a) + _unref(b)) / 2 + 'px' ),
222+
"${mockId}-__a___b______2___a_": (((_unref(a) + _unref(b))) / (2 * _unref(a)))
198223
})`)
199224
assertCode(content)
200225
})

Diff for: packages/compiler-sfc/src/cssVars.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import hash from 'hash-sum'
1313

1414
export const CSS_VARS_HELPER = `useCssVars`
1515
export const cssVarRE =
16-
/\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
16+
/\bv-bind\s*\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^;]*))\s*\)/g
1717

1818
export function genCssVarsFromList(
1919
vars: string[],

0 commit comments

Comments
 (0)