]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support complex expression in CSS v-bind() (#5114)
authoredison <daiwei521@126.com>
Fri, 21 Jan 2022 07:53:51 +0000 (15:53 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 21 Jan 2022 07:57:30 +0000 (15:57 +0800)
fix #5109

packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
packages/compiler-sfc/__tests__/cssVars.spec.ts
packages/compiler-sfc/src/cssVars.ts

index 5e31fc5ba4e771af4c24eb997b90eeec851cb885..b44f584b206b4618f1a5b0d4f8f1ede9d9abbee6 100644 (file)
@@ -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/ <script setup> 1`] = `
 "import { useCssVars as _useCssVars, unref as _unref } from 'vue'
 
index 67447310c6ff48ad9e7002c017fd09abec44b453..965c9a5202b5c5a64fefc58fcd51718af6a91115 100644 (file)
@@ -195,6 +195,31 @@ describe('CSS vars injection', () => {
       // color should only be injected once, even if it is twice in style
       expect(content).toMatch(`_useCssVars(_ctx => ({
   "${mockId}-color": (color)
+})`)
+      assertCode(content)
+    })
+
+    test('should work with w/ complex expression', () => {
+      const { content } = compileSFCScript(
+        `<script setup>
+        let a = 100
+        let b = 200
+        </script>\n` +
+          `<style>
+          div {
+            color: v-bind((a + b) / 2 + 'px' );
+          }
+          div {
+            color: v-bind    ((a + b) / 2 + 'px' );
+          }
+          p {
+            color: v-bind(((a + b)) / (2 * a));
+          }
+        </style>`
+      )
+      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)
     })
index 3032e2fbf5da52822bf9bacb7a77a78a96a3511f..da3e164aeaf9bc3e784895656839806c786e9cd7 100644 (file)
@@ -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[],