2019-10-03 16:55:14 -04:00
|
|
|
import {
|
|
|
|
NodeTransform,
|
|
|
|
NodeTypes,
|
2020-02-04 18:37:23 -05:00
|
|
|
createSimpleExpression,
|
|
|
|
SimpleExpressionNode,
|
|
|
|
SourceLocation
|
2019-10-03 16:55:14 -04:00
|
|
|
} from '@vue/compiler-core'
|
2020-05-06 11:22:49 -04:00
|
|
|
import { parseStringStyle } from '@vue/shared'
|
2019-09-25 14:13:33 -04:00
|
|
|
|
2019-09-26 10:13:46 -04:00
|
|
|
// 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.
|
2019-09-25 14:13:33 -04:00
|
|
|
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
|
|
|
|
node.props[i] = {
|
|
|
|
type: NodeTypes.DIRECTIVE,
|
|
|
|
name: `bind`,
|
2019-09-27 11:42:02 -04:00
|
|
|
arg: createSimpleExpression(`style`, true, p.loc),
|
2020-02-21 13:10:13 +01:00
|
|
|
exp: parseInlineCSS(p.value.content, p.loc),
|
2019-09-25 14:13:33 -04:00
|
|
|
modifiers: [],
|
|
|
|
loc: p.loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:22:49 -04:00
|
|
|
const parseInlineCSS = (
|
2020-02-04 18:37:23 -05:00
|
|
|
cssText: string,
|
|
|
|
loc: SourceLocation
|
2020-05-06 11:22:49 -04:00
|
|
|
): SimpleExpressionNode => {
|
|
|
|
const normalized = parseStringStyle(cssText)
|
|
|
|
return createSimpleExpression(JSON.stringify(normalized), false, loc, true)
|
2019-09-25 14:13:33 -04:00
|
|
|
}
|