refactor: move transformStyle to compiler-dom

This commit is contained in:
Evan You
2019-10-03 16:55:14 -04:00
parent c2fc7e3347
commit 3354837ce1
11 changed files with 11 additions and 8 deletions

View File

@@ -6,7 +6,6 @@ import { isString } from '@vue/shared'
import { transformIf } from './transforms/vIf'
import { transformFor } from './transforms/vFor'
import { transformExpression } from './transforms/transformExpression'
import { transformStyle } from './transforms/transformStyle'
import { transformSlotOutlet } from './transforms/transfromSlotOutlet'
import { transformElement } from './transforms/transformElement'
import { transformOn } from './transforms/vOn'
@@ -54,7 +53,6 @@ export function baseCompile(
: []),
trackSlotScopes,
optimizeText,
transformStyle,
transformSlotOutlet,
transformElement,
...(options.nodeTransforms || []) // user transforms

View File

@@ -1,42 +0,0 @@
import { NodeTransform } from '../transform'
import { NodeTypes, createSimpleExpression } from '../ast'
// Parse inline CSS strings for static style attributes into an object.
// This is a NodeTransform since it works on the static `style` attribute and
// converts it into a dynamic equivalent:
// style="color: red" -> :style='{ "color": "red" }'
// It is then processed by `transformElement` and included in the generated
// props.
export const transformStyle: NodeTransform = (node, context) => {
if (node.type === NodeTypes.ELEMENT) {
node.props.forEach((p, i) => {
if (p.type === NodeTypes.ATTRIBUTE && p.name === 'style' && p.value) {
// replace p with an expression node
const parsed = JSON.stringify(parseInlineCSS(p.value.content))
const exp = context.hoist(createSimpleExpression(parsed, false, p.loc))
node.props[i] = {
type: NodeTypes.DIRECTIVE,
name: `bind`,
arg: createSimpleExpression(`style`, true, p.loc),
exp,
modifiers: [],
loc: p.loc
}
}
})
}
}
const listDelimiterRE = /;(?![^(]*\))/g
const propertyDelimiterRE = /:(.+)/
function parseInlineCSS(cssText: string): Record<string, string> {
const res: Record<string, string> = {}
cssText.split(listDelimiterRE).forEach(function(item) {
if (item) {
const tmp = item.split(propertyDelimiterRE)
tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim())
}
})
return res
}