-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
packages/compiler/__tests__/transforms/transformStyle.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// todo: uncomment if/when vue-next#907 is merged/released | ||
// import { | ||
// baseParse as parse, | ||
// transform, | ||
// CompilerOptions, | ||
// ElementNode, | ||
// NodeTypes, | ||
// VNodeCall, | ||
// transformBind, | ||
// transformElement | ||
// } from '@vue/compiler-core' | ||
// 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', () => { | ||
test.todo('uncomment tests') | ||
// test('should transform into directive node', () => { | ||
// const { node } = transformWithStyleTransform(`<Label style="color: red"/>`) | ||
// expect(node.props[0]).toMatchObject({ | ||
// type: NodeTypes.DIRECTIVE, | ||
// name: `bind`, | ||
// arg: { | ||
// type: NodeTypes.SIMPLE_EXPRESSION, | ||
// content: `style`, | ||
// isStatic: true | ||
// }, | ||
// exp: { | ||
// type: NodeTypes.SIMPLE_EXPRESSION, | ||
// content: `{"color":"red"}`, | ||
// isStatic: false | ||
// } | ||
// }) | ||
// }) | ||
// | ||
// test('working with v-bind transform', () => { | ||
// const { node } = transformWithStyleTransform(`<Label style="color: red"/>`, { | ||
// nodeTransforms: [transformStyle, transformElement], | ||
// directiveTransforms: { | ||
// bind: transformBind | ||
// } | ||
// }) | ||
// expect((node.codegenNode as VNodeCall).props).toMatchObject({ | ||
// type: NodeTypes.JS_OBJECT_EXPRESSION, | ||
// properties: [ | ||
// { | ||
// key: { | ||
// type: NodeTypes.SIMPLE_EXPRESSION, | ||
// content: `style`, | ||
// isStatic: true | ||
// }, | ||
// value: { | ||
// type: NodeTypes.SIMPLE_EXPRESSION, | ||
// content: `{"color":"red"}`, | ||
// isStatic: false | ||
// } | ||
// } | ||
// ] | ||
// }) | ||
// // should not cause the STYLE patchFlag to be attached | ||
// expect((node.codegenNode as VNodeCall).patchFlag).toBeUndefined() | ||
// }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
NodeTransform, | ||
NodeTypes, | ||
createSimpleExpression, | ||
SimpleExpressionNode, | ||
SourceLocation | ||
} from '@vue/compiler-core' | ||
|
||
// 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 | ||
node.props[i] = { | ||
type: NodeTypes.DIRECTIVE, | ||
name: `bind`, | ||
arg: createSimpleExpression(`style`, true, p.loc), | ||
exp: parseInlineCSS(p.value.content, p.loc), | ||
modifiers: [], | ||
loc: p.loc | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const listDelimiterRE = /;(?![^(]*\))/g | ||
const propertyDelimiterRE = /:(.+)/ | ||
|
||
function parseInlineCSS( | ||
cssText: string, | ||
loc: SourceLocation | ||
): SimpleExpressionNode { | ||
const res: Record<string, string> = {} | ||
cssText.split(listDelimiterRE).forEach(item => { | ||
if (item) { | ||
const tmp = item.split(propertyDelimiterRE) | ||
tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim()) | ||
} | ||
}) | ||
return createSimpleExpression(JSON.stringify(res), false, loc, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters