From 0f00cf43cf5eaeeee7f12d523a5f4936f7dc0a84 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 12 May 2022 16:31:16 +0800 Subject: [PATCH] fix(compiler-core): normalize v-bind:style with array literal value fix #5106 --- .../transforms/transformElement.spec.ts | 31 +++++++++++++++++++ .../src/transforms/transformElement.ts | 3 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index a9edc987..b672799a 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -807,6 +807,37 @@ describe('compiler: element transform', () => { }) }) + test(':style with array literal', () => { + const { node, root } = parseWithElementTransform( + `
`, + { + 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( `
`, diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 3e2f74c0..403c5c6e 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -767,10 +767,11 @@ export function buildProps( } if ( styleProp && - !isStaticExp(styleProp.value) && // the static style is compiled into an object, // so use `hasStyleBinding` to ensure that it is a dynamic style binding (hasStyleBinding || + (styleProp.value.type === NodeTypes.SIMPLE_EXPRESSION && + styleProp.value.content.trim()[0] === `[`) || // v-bind:style and style both exist, // v-bind:style with static literal object styleProp.value.type === NodeTypes.JS_ARRAY_EXPRESSION)