-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
232 lines (216 loc) · 5.88 KB
/
component.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
import { warn } from './debug'
import { resolveAsset } from './options'
import { getAttr, getBindAttr } from './dom'
import { isArray, isPlainObject, isObject, hasOwn } from './lang'
export const commonTagRE = /^(div|p|span|img|a|b|i|br|ul|ol|li|h1|h2|h3|h4|h5|h6|code|pre|table|th|td|tr|form|label|input|select|option|nav|article|section|header|footer)$/
export const reservedTagRE = /^(slot|partial|component)$/
let isUnknownElement
if (process.env.NODE_ENV !== 'production') {
isUnknownElement = function (el, tag) {
if (tag.indexOf('-') > -1) {
// http://stackoverflow.com/a/28210364/1070244
return (
el.constructor === window.HTMLUnknownElement ||
el.constructor === window.HTMLElement
)
} else {
return (
/HTMLUnknownElement/.test(el.toString()) &&
// Chrome returns unknown for several HTML5 elements.
// https://code.google.com/p/chromium/issues/detail?id=540526
!/^(data|time|rtc|rb)$/.test(tag)
)
}
}
}
/**
* Check if an element is a component, if yes return its
* component id.
*
* @param {Element} el
* @param {Object} options
* @return {Object|undefined}
*/
export function checkComponentAttr (el, options) {
var tag = el.tagName.toLowerCase()
var hasAttrs = el.hasAttributes()
if (!commonTagRE.test(tag) && !reservedTagRE.test(tag)) {
if (resolveAsset(options, 'components', tag)) {
return { id: tag }
} else {
var is = hasAttrs && getIsBinding(el)
if (is) {
return is
} else if (process.env.NODE_ENV !== 'production') {
var expectedTag =
options._componentNameMap &&
options._componentNameMap[tag]
if (expectedTag) {
warn(
'Unknown custom element: <' + tag + '> - ' +
'did you mean <' + expectedTag + '>? ' +
'HTML is case-insensitive, remember to use kebab-case in templates.'
)
} else if (isUnknownElement(el, tag)) {
warn(
'Unknown custom element: <' + tag + '> - did you ' +
'register the component correctly? For recursive components, ' +
'make sure to provide the "name" option.'
)
}
}
}
} else if (hasAttrs) {
return getIsBinding(el)
}
}
/**
* Get "is" binding from an element.
*
* @param {Element} el
* @return {Object|undefined}
*/
function getIsBinding (el) {
// dynamic syntax
var exp = getAttr(el, 'is')
if (exp != null) {
return { id: exp }
} else {
exp = getBindAttr(el, 'is')
if (exp != null) {
return { id: exp, dynamic: true }
}
}
}
/**
* 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)
}
vm[key] = vm._data[key] = assertProp(prop, value)
? value
: undefined
}
/**
* 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)
}