-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.js
128 lines (119 loc) · 3.12 KB
/
misc.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
var _ = require('./index')
var config = require('../config')
/**
* Assert whether a prop is valid.
*
* @param {Object} prop
* @param {*} value
*/
exports.assertProp = function (prop, value) {
var assertions = prop.assertions
if (!assertions) {
return true
}
var type = assertions.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) {
_.warn(
'Invalid prop: type check failed for ' +
prop.path + '="' + prop.raw + '".' +
' Expected ' + formatType(expectedType) +
', got ' + formatValue(value) + '.'
)
return false
}
var validator = assertions.validator
if (validator) {
if (!validator.call(null, value)) {
_.warn(
'Invalid prop: custom validator check failed for ' +
prop.path + '="' + prop.raw + '"'
)
return false
}
}
return true
}
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)
}
/**
* Check if an element is a component, if yes return its
* component id.
*
* @param {Element} el
* @param {Object} options
* @return {String|undefined}
*/
exports.commonTagRE = /^(div|p|span|img|a|br|ul|ol|li|h1|h2|h3|h4|h5|code|pre)$/
exports.tableElementsRE = /^caption|colgroup|thead|tfoot|tbody|tr|td|th$/
exports.checkComponent = function (el, options) {
var tag = el.tagName.toLowerCase()
if (tag === 'component') {
// dynamic syntax
var exp = el.getAttribute('is')
el.removeAttribute('is')
return exp
} else if (
!exports.commonTagRE.test(tag) &&
_.resolveAsset(options, 'components', tag)
) {
return tag
} else if (
exports.tableElementsRE.test(tag) &&
(tag = _.attr(el, 'component'))
) {
return tag
}
}
/**
* Create an "anchor" for performing dom insertion/removals.
* This is used in a number of scenarios:
* - block instance
* - v-html
* - v-if
* - component
* - repeat
*
* @param {String} content
* @param {Boolean} persist - IE trashes empty textNodes on
* cloneNode(true), so in certain
* cases the anchor needs to be
* non-empty to be persisted in
* templates.
* @return {Comment|Text}
*/
exports.createAnchor = function (content, persist) {
return config.debug
? document.createComment(content)
: document.createTextNode(persist ? ' ' : '')
}