-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-props.js
207 lines (195 loc) · 5.89 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
var _ = require('../util')
var dirParser = require('../parsers/directive')
var propDef = require('../directives/internal/prop')
var propBindingModes = require('../config')._propBindingModes
var empty = {}
// regexes
var identRE = require('../parsers/path').identRE
var 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
*/
module.exports = 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 = dirParser.parse(value)
value = parsed.expression
prop.filters = parsed.filters
// check binding type
if (_.isLiteral(value)) {
// 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 = _.attr(el, attr)) !== null) {
// has literal binding!
prop.raw = value
} else if (options.required) {
// warn missing required
process.env.NODE_ENV !== 'production' && _.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, getDefault(vm, options))
} else if (prop.dynamic) {
// dynamic prop
if (vm._context) {
if (prop.mode === propBindingModes.ONE_TIME) {
// one time binding
value = (scope || vm._context).$get(prop.parentPath)
_.initProp(vm, prop, value)
} else {
// dynamic binding
vm._bindDir({
name: 'prop',
def: propDef,
prop: prop
}, null, null, scope) // el, host, scope
}
} else {
process.env.NODE_ENV !== 'production' && _.warn(
'Cannot bind dynamic prop on a root instance' +
' with no parent: ' + prop.name + '="' +
raw + '"'
)
}
} 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)
}
}
}
}
/**
* Get the default value of a prop.
*
* @param {Vue} vm
* @param {Object} options
* @return {*}
*/
function getDefault (vm, options) {
// no default, return undefined
if (!options.hasOwnProperty('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
}