From 95d49bf7e7f6236383ce7caa3ea4fef65ef19a25 Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 21 Jan 2022 15:53:51 +0800 Subject: [PATCH] fix(compiler-sfc): support complex expression in CSS v-bind() (#5114) fix #5109 --- .../__snapshots__/cssVars.spec.ts.snap | 21 ++++++++++++++++ .../compiler-sfc/__tests__/cssVars.spec.ts | 25 +++++++++++++++++++ packages/compiler-sfc/src/cssVars.ts | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap index 5e31fc5ba4e..b44f584b206 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap @@ -66,6 +66,27 @@ return { color, width } }" `; +exports[`CSS vars injection codegen should work with w/ complex expression 1`] = ` +"import { useCssVars as _useCssVars, unref as _unref } from 'vue' + +export default { + setup(__props, { expose }) { + expose(); + +_useCssVars(_ctx => ({ + \\"xxxxxxxx-_a___b____2____px__\\": ((_unref(a) + _unref(b)) / 2 + 'px' ), + \\"xxxxxxxx-__a___b______2___a_\\": (((_unref(a) + _unref(b))) / (2 * _unref(a))) +})) + + let a = 100 + let b = 200 + +return { a, b } +} + +}" +`; + exports[`CSS vars injection codegen w/ \n` + + `` + ) + expect(content).toMatch(`_useCssVars(_ctx => ({ + "${mockId}-_a___b____2____px__": ((_unref(a) + _unref(b)) / 2 + 'px' ), + "${mockId}-__a___b______2___a_": (((_unref(a) + _unref(b))) / (2 * _unref(a))) })`) assertCode(content) }) diff --git a/packages/compiler-sfc/src/cssVars.ts b/packages/compiler-sfc/src/cssVars.ts index 3032e2fbf5d..da3e164aeaf 100644 --- a/packages/compiler-sfc/src/cssVars.ts +++ b/packages/compiler-sfc/src/cssVars.ts @@ -13,7 +13,7 @@ import hash from 'hash-sum' export const CSS_VARS_HELPER = `useCssVars` export const cssVarRE = - /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g + /\bv-bind\s*\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^;]*))\s*\)/g export function genCssVarsFromList( vars: string[],