2019-09-26 09:02:46 +08:00
|
|
|
import {
|
2019-12-23 08:44:21 +08:00
|
|
|
baseParse as parse,
|
2019-09-26 09:02:46 +08:00
|
|
|
transform,
|
|
|
|
CompilerOptions,
|
|
|
|
ElementNode,
|
2019-10-04 11:30:25 +08:00
|
|
|
NodeTypes,
|
2020-02-12 07:40:21 +08:00
|
|
|
VNodeCall
|
2019-10-04 04:55:14 +08:00
|
|
|
} from '@vue/compiler-core'
|
|
|
|
import { transformBind } from '../../../compiler-core/src/transforms/vBind'
|
|
|
|
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
|
2019-09-26 09:02:46 +08:00
|
|
|
import { transformStyle } from '../../src/transforms/transformStyle'
|
|
|
|
|
|
|
|
function transformWithStyleTransform(
|
|
|
|
template: string,
|
|
|
|
options: CompilerOptions = {}
|
|
|
|
) {
|
|
|
|
const ast = parse(template)
|
|
|
|
transform(ast, {
|
|
|
|
nodeTransforms: [transformStyle],
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
root: ast,
|
|
|
|
node: ast.children[0] as ElementNode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('compiler: style transform', () => {
|
2020-02-21 20:10:13 +08:00
|
|
|
test('should transform into directive node', () => {
|
|
|
|
const { node } = transformWithStyleTransform(`<div style="color: red"/>`)
|
2019-09-26 09:02:46 +08:00
|
|
|
expect(node.props[0]).toMatchObject({
|
|
|
|
type: NodeTypes.DIRECTIVE,
|
|
|
|
name: `bind`,
|
|
|
|
arg: {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2019-09-26 09:02:46 +08:00
|
|
|
content: `style`,
|
|
|
|
isStatic: true
|
|
|
|
},
|
|
|
|
exp: {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2020-02-21 20:10:13 +08:00
|
|
|
content: `{"color":"red"}`,
|
2019-09-26 09:02:46 +08:00
|
|
|
isStatic: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('working with v-bind transform', () => {
|
|
|
|
const { node } = transformWithStyleTransform(`<div style="color: red"/>`, {
|
|
|
|
nodeTransforms: [transformStyle, transformElement],
|
|
|
|
directiveTransforms: {
|
|
|
|
bind: transformBind
|
|
|
|
}
|
|
|
|
})
|
2020-02-12 07:40:21 +08:00
|
|
|
expect((node.codegenNode as VNodeCall).props).toMatchObject({
|
2019-09-26 09:02:46 +08:00
|
|
|
type: NodeTypes.JS_OBJECT_EXPRESSION,
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
key: {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2019-09-26 09:02:46 +08:00
|
|
|
content: `style`,
|
|
|
|
isStatic: true
|
|
|
|
},
|
|
|
|
value: {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2020-02-21 20:10:13 +08:00
|
|
|
content: `{"color":"red"}`,
|
2019-09-26 09:02:46 +08:00
|
|
|
isStatic: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2019-10-23 22:49:16 +08:00
|
|
|
// should not cause the STYLE patchFlag to be attached
|
2020-02-12 07:40:21 +08:00
|
|
|
expect((node.codegenNode as VNodeCall).patchFlag).toBeUndefined()
|
2019-09-26 09:02:46 +08:00
|
|
|
})
|
|
|
|
})
|