forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-helpers.js
More file actions
140 lines (128 loc) · 3.16 KB
/
runtime-helpers.js
File metadata and controls
140 lines (128 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* @flow */
import { escape, cachedEscape } from 'web/server/util'
import { isObject, extend } from 'shared/util'
import { renderAttr } from 'web/server/modules/attrs'
import { renderClass } from 'web/util/class'
import { genStyle } from 'web/server/modules/style'
import { normalizeStyleBinding } from 'web/util/style'
import {
normalizeChildren,
simpleNormalizeChildren
} from 'core/vdom/helpers/normalize-children'
import {
propsToAttrMap,
isRenderableAttr
} from 'web/server/util'
export function installSSRHelpers (vm: Component) {
if (vm._ssrNode) return
let Ctor = vm.constructor
while (Ctor.super) {
Ctor = Ctor.super
}
Object.assign(Ctor.prototype, {
_ssrEscape: escape,
_ssrNode: renderStringNode,
_ssrList: renderStringList,
_ssrAttr: renderAttr,
_ssrAttrs: renderAttrs,
_ssrDOMProps: renderDOMProps,
_ssrClass: renderSSRClass,
_ssrStyle: renderSSRStyle
})
}
class StringNode {
isString: boolean;
open: string;
close: ?string;
children: ?Array<any>;
constructor (
open: string,
close?: string,
children?: Array<any>,
normalizationType?: number
) {
this.isString = true
this.open = open
this.close = close
if (children) {
this.children = normalizationType === 1
? simpleNormalizeChildren(children)
: normalizationType === 2
? normalizeChildren(children)
: children
} else {
this.children = void 0
}
}
}
function renderStringNode (
open: string,
close?: string,
children?: Array<any>,
normalizationType?: number
): StringNode {
return new StringNode(open, close, children, normalizationType)
}
function renderStringList (
val: any,
render: (
val: any,
keyOrIndex: string | number,
index?: number
) => string
): string {
let ret = ''
let i, l, keys, key
if (Array.isArray(val) || typeof val === 'string') {
for (i = 0, l = val.length; i < l; i++) {
ret += render(val[i], i)
}
} else if (typeof val === 'number') {
for (i = 0; i < val; i++) {
ret += render(i + 1, i)
}
} else if (isObject(val)) {
keys = Object.keys(val)
for (i = 0, l = keys.length; i < l; i++) {
key = keys[i]
ret += render(val[key], key, i)
}
}
return ret
}
function renderAttrs (obj: Object): string {
let res = ''
for (const key in obj) {
res += renderAttr(key, obj[key])
}
return res
}
function renderDOMProps (obj: Object): string {
let res = ''
for (const key in obj) {
const attr = propsToAttrMap[key] || key.toLowerCase()
if (isRenderableAttr(attr)) {
res += renderAttr(attr, obj[key])
}
}
return res
}
function renderSSRClass (
staticClass: ?string,
dynamic: any
): string {
const res = renderClass(staticClass, dynamic)
return res === '' ? res : ` class="${cachedEscape(res)}"`
}
function renderSSRStyle (
staticStyle: ?Object,
dynamic: any,
extra: ?Object
): string {
const style = {}
if (staticStyle) extend(style, staticStyle)
if (dynamic) extend(style, normalizeStyleBinding(dynamic))
if (extra) extend(style, extra)
const res = genStyle(style)
return res === '' ? res : ` style=${JSON.stringify(cachedEscape(res))}`
}