-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-props.js
351 lines (330 loc) · 9.33 KB
/
compile-props.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import config from '../config'
import { parseDirective } from '../parsers/directive'
import { isSimplePath } from '../parsers/expression'
import { defineReactive } from '../observer/index'
import propDef from '../directives/internal/prop'
import {
warn,
camelize,
hyphenate,
getAttr,
getBindAttr,
isLiteral,
toBoolean,
toNumber,
stripQuotes,
isArray,
isPlainObject,
isObject,
hasOwn
} from '../util/index'
const propBindingModes = config._propBindingModes
const empty = {}
// regexes
const identRE = /^[$_a-zA-Z]+[\w$]*$/
const settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
/**
* Compile props on a root element and return
* a props link function.
*
* @param {Element|DocumentFragment} el
* @param {Array} propOptions
* @return {Function} propsLinkFn
*/
export function compileProps (el, propOptions) {
var props = []
var names = Object.keys(propOptions)
var i = names.length
var options, name, attr, value, path, parsed, prop
while (i--) {
name = names[i]
options = propOptions[name] || empty
if (process.env.NODE_ENV !== 'production' && name === '$data') {
warn('Do not use $data as prop.')
continue
}
// props could contain dashes, which will be
// interpreted as minus calculations by the parser
// so we need to camelize the path here
path = camelize(name)
if (!identRE.test(path)) {
process.env.NODE_ENV !== 'production' && warn(
'Invalid prop key: "' + name + '". Prop keys ' +
'must be valid identifiers.'
)
continue
}
prop = {
name: name,
path: path,
options: options,
mode: propBindingModes.ONE_WAY,
raw: null
}
attr = hyphenate(name)
// first check dynamic version
if ((value = getBindAttr(el, attr)) === null) {
if ((value = getBindAttr(el, attr + '.sync')) !== null) {
prop.mode = propBindingModes.TWO_WAY
} else if ((value = getBindAttr(el, attr + '.once')) !== null) {
prop.mode = propBindingModes.ONE_TIME
}
}
if (value !== null) {
// has dynamic binding!
prop.raw = value
parsed = parseDirective(value)
value = parsed.expression
prop.filters = parsed.filters
// check binding type
if (isLiteral(value) && !parsed.filters) {
// for expressions containing literal numbers and
// booleans, there's no need to setup a prop binding,
// so we can optimize them as a one-time set.
prop.optimizedLiteral = true
} else {
prop.dynamic = true
// check non-settable path for two-way bindings
if (process.env.NODE_ENV !== 'production' &&
prop.mode === propBindingModes.TWO_WAY &&
!settablePathRE.test(value)) {
prop.mode = propBindingModes.ONE_WAY
warn(
'Cannot bind two-way prop with non-settable ' +
'parent path: ' + value
)
}
}
prop.parentPath = value
// warn required two-way
if (
process.env.NODE_ENV !== 'production' &&
options.twoWay &&
prop.mode !== propBindingModes.TWO_WAY
) {
warn(
'Prop "' + name + '" expects a two-way binding type.'
)
}
} else if ((value = getAttr(el, attr)) !== null) {
// has literal binding!
prop.raw = value
} else if (process.env.NODE_ENV !== 'production') {
// check possible camelCase prop usage
var lowerCaseName = path.toLowerCase()
value = /[A-Z\-]/.test(name) && (
el.getAttribute(lowerCaseName) ||
el.getAttribute(':' + lowerCaseName) ||
el.getAttribute('v-bind:' + lowerCaseName) ||
el.getAttribute(':' + lowerCaseName + '.once') ||
el.getAttribute('v-bind:' + lowerCaseName + '.once') ||
el.getAttribute(':' + lowerCaseName + '.sync') ||
el.getAttribute('v-bind:' + lowerCaseName + '.sync')
)
if (value) {
warn(
'Possible usage error for prop `' + lowerCaseName + '` - ' +
'did you mean `' + attr + '`? HTML is case-insensitive, remember to use ' +
'kebab-case for props in templates.'
)
} else if (options.required) {
// warn missing required
warn('Missing required prop: ' + name)
}
}
// push prop
props.push(prop)
}
return makePropsLinkFn(props)
}
/**
* Build a function that applies props to a vm.
*
* @param {Array} props
* @return {Function} propsLinkFn
*/
function makePropsLinkFn (props) {
return function propsLinkFn (vm, scope) {
// store resolved props info
vm._props = {}
var i = props.length
var prop, path, options, value, raw
while (i--) {
prop = props[i]
raw = prop.raw
path = prop.path
options = prop.options
vm._props[path] = prop
if (raw === null) {
// initialize absent prop
initProp(vm, prop, undefined)
} else if (prop.dynamic) {
// dynamic prop
if (prop.mode === propBindingModes.ONE_TIME) {
// one time binding
value = (scope || vm._context || vm).$get(prop.parentPath)
initProp(vm, prop, value)
} else {
if (vm._context) {
// dynamic binding
vm._bindDir({
name: 'prop',
def: propDef,
prop: prop
}, null, null, scope) // el, host, scope
} else {
// root instance
initProp(vm, prop, vm.$get(prop.parentPath))
}
}
} else if (prop.optimizedLiteral) {
// optimized literal, cast it and just set once
var stripped = stripQuotes(raw)
value = stripped === raw
? toBoolean(toNumber(raw))
: stripped
initProp(vm, prop, value)
} else {
// string literal, but we need to cater for
// Boolean props with no value
value = options.type === Boolean && raw === ''
? true
: raw
initProp(vm, prop, value)
}
}
}
}
/**
* Set a prop's initial value on a vm and its data object.
*
* @param {Vue} vm
* @param {Object} prop
* @param {*} value
*/
export function initProp (vm, prop, value) {
const key = prop.path
value = coerceProp(prop, value)
if (value === undefined) {
value = getPropDefaultValue(vm, prop.options)
}
if (assertProp(prop, value)) {
var doNotObserve = !prop.dynamic || isSimplePath(prop.raw)
defineReactive(vm, key, value, doNotObserve)
}
}
/**
* Get the default value of a prop.
*
* @param {Vue} vm
* @param {Object} options
* @return {*}
*/
function getPropDefaultValue (vm, options) {
// no default, return undefined
if (!hasOwn(options, 'default')) {
// absent boolean value defaults to false
return options.type === Boolean
? false
: undefined
}
var def = options.default
// warn against non-factory defaults for Object & Array
if (isObject(def)) {
process.env.NODE_ENV !== 'production' && warn(
'Object/Array as default prop values will be shared ' +
'across multiple instances. Use a factory function ' +
'to return the default value instead.'
)
}
// call factory function for non-Function types
return typeof def === 'function' && options.type !== Function
? def.call(vm)
: def
}
/**
* Assert whether a prop is valid.
*
* @param {Object} prop
* @param {*} value
*/
export function assertProp (prop, value) {
if (
!prop.options.required && ( // non-required
prop.raw === null || // abscent
value == null // null or undefined
)
) {
return true
}
var options = prop.options
var type = options.type
var valid = true
var expectedType
if (type) {
if (type === String) {
expectedType = 'string'
valid = typeof value === expectedType
} else if (type === Number) {
expectedType = 'number'
valid = typeof value === 'number'
} else if (type === Boolean) {
expectedType = 'boolean'
valid = typeof value === 'boolean'
} else if (type === Function) {
expectedType = 'function'
valid = typeof value === 'function'
} else if (type === Object) {
expectedType = 'object'
valid = isPlainObject(value)
} else if (type === Array) {
expectedType = 'array'
valid = isArray(value)
} else {
valid = value instanceof type
}
}
if (!valid) {
process.env.NODE_ENV !== 'production' && warn(
'Invalid prop: type check failed for ' +
prop.path + '="' + prop.raw + '".' +
' Expected ' + formatType(expectedType) +
', got ' + formatValue(value) + '.'
)
return false
}
var validator = options.validator
if (validator) {
if (!validator(value)) {
process.env.NODE_ENV !== 'production' && warn(
'Invalid prop: custom validator check failed for ' +
prop.path + '="' + prop.raw + '"'
)
return false
}
}
return true
}
/**
* Force parsing value with coerce option.
*
* @param {*} value
* @param {Object} options
* @return {*}
*/
export function coerceProp (prop, value) {
var coerce = prop.options.coerce
if (!coerce) {
return value
}
// coerce is a function
return coerce(value)
}
function formatType (val) {
return val
? val.charAt(0).toUpperCase() + val.slice(1)
: 'custom type'
}
function formatValue (val) {
return Object.prototype.toString.call(val).slice(8, -1)
}