Skip to content

Commit

Permalink
fix(compiler-core): normalize v-bind:style with array literal value
Browse files Browse the repository at this point in the history
fix #5106
  • Loading branch information
yyx990803 committed May 12, 2022
1 parent 59cf295 commit 0f00cf4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,37 @@ describe('compiler: element transform', () => {
})
})

test(':style with array literal', () => {
const { node, root } = parseWithElementTransform(
`<div :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 }" />`,
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0f00cf4

Please sign in to comment.