Skip to content

Commit 60cf3b8

Browse files
committed
progress
1 parent f635059 commit 60cf3b8

File tree

4 files changed

+123
-23
lines changed

4 files changed

+123
-23
lines changed

src/compiler/codegen/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ function genIfConditions (
141141

142142
const condition = conditions.shift()
143143
if (condition.exp) {
144-
return `(${condition.exp})?${
144+
return `(${condition.exp})?(${
145145
genTernaryExp(condition.block)
146-
}:${
146+
}):(${
147147
genIfConditions(conditions, state, altGen, altEmpty)
148-
}`
148+
})`
149149
} else {
150150
return `${genTernaryExp(condition.block)}`
151151
}

src/server/optimizing-compiler/codegen.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ import {
1313
genChildren,
1414
CodegenState
1515
} from 'compiler/codegen/index'
16+
17+
import {
18+
genAttrSegments,
19+
genDOMPropSegments,
20+
applyModelTransform
21+
} from './modules'
22+
1623
import { optimizability } from './optimizer'
17-
import type { CodegenResult } from 'compiler/codegen/index'
1824

19-
// segment types
20-
const RAW = 0
21-
const INTERPOLATION = 1
22-
const EXPRESSION = 2
25+
import type { CodegenResult } from 'compiler/codegen/index'
2326

24-
type StringSegment = {
27+
export type StringSegment = {
2528
type: number;
2629
value: string;
2730
};
2831

32+
// segment types
33+
export const RAW = 0
34+
export const INTERPOLATION = 1
35+
export const EXPRESSION = 2
36+
2937
export function generate (
3038
ast: ASTElement | void,
3139
options: CompilerOptions
@@ -125,17 +133,6 @@ function elementToSegments (el, state): Array<StringSegment> {
125133
}]
126134
}
127135

128-
// let binding
129-
// // v-show
130-
// if ((binding = el.attrsMap['v-show'])) {
131-
132-
// }
133-
134-
// // v-model
135-
// if ((binding = el.attrsMap['v-model'])) {
136-
137-
// }
138-
139136
const openSegments = elementToOpenTagSegments(el, state)
140137
const childrenSegments = childrenToSegments(el, state)
141138
const { isUnaryTag } = state.options
@@ -146,9 +143,40 @@ function elementToSegments (el, state): Array<StringSegment> {
146143
}
147144

148145
function elementToOpenTagSegments (el, state): Array<StringSegment> {
149-
// TODO: handle v-show, v-html & v-text
150-
// TODO: handle attrs/props/styles/classes
151-
return [{ type: RAW, value: `<${el.tag}>` }]
146+
applyModelTransform(el, state)
147+
let binding
148+
const segments = [{ type: RAW, value: `<${el.tag}` }]
149+
// attrs
150+
if (el.attrs) {
151+
segments.push.apply(segments, genAttrSegments(el.attrs))
152+
}
153+
// domProps
154+
if (el.props) {
155+
segments.push.apply(segments, genDOMPropSegments(el.props, el.attrs))
156+
}
157+
// v-bind="object"
158+
if ((binding = el.attrsMap['v-bind'])) {
159+
segments.push({ type: EXPRESSION, value: `_ssrAttrs(${binding})` })
160+
}
161+
// v-bind.prop="object"
162+
if ((binding = el.attrsMap['v-bind.prop'])) {
163+
segments.push({ type: EXPRESSION, value: `_ssrDOMProps(${binding})` })
164+
}
165+
// class
166+
if (el.staticClass || el.classBinding) {
167+
if (el.staticClass && !el.classBinding) {
168+
segments.push({ type: RAW, value: ` class=${el.staticClass}` })
169+
} else {
170+
// TODO
171+
}
172+
}
173+
// style & v-show
174+
if (el.staticStyle || el.styleBinding) {
175+
// TODO
176+
}
177+
// console.log(segments)
178+
segments.push({ type: RAW, value: `>` })
179+
return segments
152180
}
153181

154182
function childrenToSegments (el, state): Array<StringSegment> {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* @flow */
2+
3+
import {
4+
RAW,
5+
// INTERPOLATION,
6+
EXPRESSION
7+
} from './codegen'
8+
9+
import {
10+
propsToAttrMap,
11+
isRenderableAttr
12+
} from 'web/server/util'
13+
14+
import type { StringSegment } from './codegen'
15+
import type { CodegenState } from 'compiler/codegen/index'
16+
17+
type Attr = { name: string; value: string };
18+
19+
const plainStringRE = /^"(?:[^"\\]|\\.)*"$|^'(?:[^'\\]|\\.)*'$/
20+
21+
// let the model AST transform translate v-model into appropriate
22+
// props bindings
23+
export function applyModelTransform (el: ASTElement, state: CodegenState) {
24+
if (el.directives) {
25+
for (let i = 0; i < el.directives.length; i++) {
26+
const dir = el.directives[i]
27+
if (dir.name === 'model') {
28+
state.directives.model(el, dir, state.warn)
29+
break
30+
}
31+
}
32+
}
33+
}
34+
35+
export function genAttrSegments (
36+
attrs: Array<Attr>
37+
): Array<StringSegment> {
38+
return attrs.map(({ name, value }) => genAttrSegment(name, value))
39+
}
40+
41+
export function genDOMPropSegments (
42+
props: Array<Attr>,
43+
attrs: ?Array<Attr>
44+
): Array<StringSegment> {
45+
const segments = []
46+
props.forEach(({ name, value }) => {
47+
name = propsToAttrMap[name] || name.toLowerCase()
48+
if (isRenderableAttr(name) &&
49+
!(attrs && attrs.some(a => a.name === name))
50+
) {
51+
segments.push(genAttrSegment(name, value))
52+
}
53+
})
54+
return segments
55+
}
56+
57+
function genAttrSegment (name: string, value: string): StringSegment {
58+
if (plainStringRE.test(value)) {
59+
// TODO attr type checks
60+
return {
61+
type: RAW,
62+
value: ` ${name}=${value}`
63+
}
64+
} else {
65+
return {
66+
type: EXPRESSION,
67+
value: `_ssrAttr(${JSON.stringify(name)},${value})`
68+
}
69+
}
70+
}

src/server/optimizing-compiler/runtime-helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { escape } from 'he'
44
import { isObject } from 'shared/util'
5+
import { renderAttr } from 'web/server/modules/attrs'
56

67
export function installSSRHelpers (vm: Component) {
78
let Ctor = vm.constructor
@@ -12,6 +13,7 @@ export function installSSRHelpers (vm: Component) {
1213
Ctor.prototype._ssrNode = createStringNode
1314
Ctor.prototype._ssrList = createStringList
1415
Ctor.prototype._ssrEscape = escape
16+
Ctor.prototype._ssrAttr = renderAttr
1517
}
1618
}
1719

0 commit comments

Comments
 (0)