Skip to content

Commit 092bdcd

Browse files
authored
fix(compiler-core): fix style binding edge case (#4319)
where static `style` attribute and `:style` with constant binding are used together fix #4317
1 parent aae3725 commit 092bdcd

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
transform,
55
ErrorCodes,
66
BindingTypes,
7-
NodeTransform
7+
NodeTransform,
8+
transformExpression
89
} from '../../src'
910
import {
1011
RESOLVE_COMPONENT,
@@ -773,6 +774,37 @@ describe('compiler: element transform', () => {
773774
})
774775
})
775776

777+
test(`props merging: style w/ transformExpression`, () => {
778+
const { node, root } = parseWithElementTransform(
779+
`<div style="color: green" :style="{ color: 'red' }" />`,
780+
{
781+
nodeTransforms: [transformExpression, transformStyle, transformElement],
782+
directiveTransforms: {
783+
bind: transformBind
784+
},
785+
prefixIdentifiers: true
786+
}
787+
)
788+
expect(root.helpers).toContain(NORMALIZE_STYLE)
789+
expect(node.props).toMatchObject({
790+
type: NodeTypes.JS_OBJECT_EXPRESSION,
791+
properties: [
792+
{
793+
type: NodeTypes.JS_PROPERTY,
794+
key: {
795+
type: NodeTypes.SIMPLE_EXPRESSION,
796+
content: `style`,
797+
isStatic: true
798+
},
799+
value: {
800+
type: NodeTypes.JS_CALL_EXPRESSION,
801+
callee: NORMALIZE_STYLE
802+
}
803+
}
804+
]
805+
})
806+
})
807+
776808
test(`props merging: class`, () => {
777809
const { node, root } = parseWithElementTransform(
778810
`<div class="foo" :class="{ bar: isBar }" />`,

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,10 @@ export function buildProps(
738738
!isStaticExp(styleProp.value) &&
739739
// the static style is compiled into an object,
740740
// so use `hasStyleBinding` to ensure that it is a dynamic style binding
741-
hasStyleBinding
741+
(hasStyleBinding ||
742+
// v-bind:style and style both exist,
743+
// v-bind:style with static literal object
744+
styleProp.value.type === NodeTypes.JS_ARRAY_EXPRESSION)
742745
) {
743746
styleProp.value = createCallExpression(
744747
context.helper(NORMALIZE_STYLE),

0 commit comments

Comments
 (0)