fix(compiler-core): fix style binding edge case (#4319)

where static `style` attribute and `:style` with constant binding are used together

fix #4317
This commit is contained in:
fishDog 2021-08-17 04:37:31 +08:00 committed by GitHub
parent aae3725e57
commit 092bdcdf58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -4,7 +4,8 @@ import {
transform, transform,
ErrorCodes, ErrorCodes,
BindingTypes, BindingTypes,
NodeTransform NodeTransform,
transformExpression
} from '../../src' } from '../../src'
import { import {
RESOLVE_COMPONENT, 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`, () => { test(`props merging: class`, () => {
const { node, root } = parseWithElementTransform( const { node, root } = parseWithElementTransform(
`<div class="foo" :class="{ bar: isBar }" />`, `<div class="foo" :class="{ bar: isBar }" />`,

View File

@ -738,7 +738,10 @@ export function buildProps(
!isStaticExp(styleProp.value) && !isStaticExp(styleProp.value) &&
// the static style is compiled into an object, // the static style is compiled into an object,
// so use `hasStyleBinding` to ensure that it is a dynamic style binding // 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( styleProp.value = createCallExpression(
context.helper(NORMALIZE_STYLE), context.helper(NORMALIZE_STYLE),