]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): fix style binding edge case (#4319)
authorfishDog <40156382+Bigfish8@users.noreply.github.com>
Mon, 16 Aug 2021 20:37:31 +0000 (04:37 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Aug 2021 20:37:31 +0000 (16:37 -0400)
where static `style` attribute and `:style` with constant binding are used together

fix #4317

packages/compiler-core/__tests__/transforms/transformElement.spec.ts
packages/compiler-core/src/transforms/transformElement.ts

index 811e56013c916172a14eb04f8ffea64221d61511..d2780f35fa7fa5ad2ad07641580aa7d2511276cb 100644 (file)
@@ -4,7 +4,8 @@ import {
   transform,
   ErrorCodes,
   BindingTypes,
-  NodeTransform
+  NodeTransform,
+  transformExpression
 } from '../../src'
 import {
   RESOLVE_COMPONENT,
@@ -773,6 +774,37 @@ describe('compiler: element transform', () => {
     })
   })
 
+  test(`props merging: style w/ transformExpression`, () => {
+    const { node, root } = parseWithElementTransform(
+      `<div style="color: green" :style="{ color: 'red' }" />`,
+      {
+        nodeTransforms: [transformExpression, transformStyle, transformElement],
+        directiveTransforms: {
+          bind: transformBind
+        },
+        prefixIdentifiers: true
+      }
+    )
+    expect(root.helpers).toContain(NORMALIZE_STYLE)
+    expect(node.props).toMatchObject({
+      type: NodeTypes.JS_OBJECT_EXPRESSION,
+      properties: [
+        {
+          type: NodeTypes.JS_PROPERTY,
+          key: {
+            type: NodeTypes.SIMPLE_EXPRESSION,
+            content: `style`,
+            isStatic: true
+          },
+          value: {
+            type: NodeTypes.JS_CALL_EXPRESSION,
+            callee: NORMALIZE_STYLE
+          }
+        }
+      ]
+    })
+  })
+
   test(`props merging: class`, () => {
     const { node, root } = parseWithElementTransform(
       `<div class="foo" :class="{ bar: isBar }" />`,
index 90d33aa8b1f8bf7b48cd9623b643bf397b8ae97d..4b142564f0aa161d33d1fc592031ebdeb17695d5 100644 (file)
@@ -738,7 +738,10 @@ export function buildProps(
             !isStaticExp(styleProp.value) &&
             // the static style is compiled into an object,
             // so use `hasStyleBinding` to ensure that it is a dynamic style binding
-            hasStyleBinding
+            (hasStyleBinding ||
+              // v-bind:style and style both exist,
+              // v-bind:style with static literal object
+              styleProp.value.type === NodeTypes.JS_ARRAY_EXPRESSION)
           ) {
             styleProp.value = createCallExpression(
               context.helper(NORMALIZE_STYLE),