-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathvalidators.js
142 lines (123 loc) · 3.47 KB
/
validators.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
// @flow
// import concat from 'lodash/concat'
import {
camelize,
capitalize,
htmlTags,
hyphenate,
svgElements,
throwError
} from './util'
export function isDomSelector (selector: any): boolean {
if (typeof selector !== 'string') {
return false
}
try {
if (typeof document === 'undefined') {
throwError(
`mount must be run in a browser environment like ` +
`PhantomJS, jsdom or chrome`
)
}
} catch (error) {
throwError(
`mount must be run in a browser environment like ` +
`PhantomJS, jsdom or chrome`
)
}
try {
document.querySelector(selector)
return true
} catch (error) {
return false
}
}
export function isVueComponent (component: any): boolean {
if (typeof component === 'function' && component.options) {
return true
}
if (component === null || typeof component !== 'object') {
return false
}
if (component.extends || component._Ctor) {
return true
}
if (typeof component.template === 'string') {
return true
}
return typeof component.render === 'function'
}
export function componentNeedsCompiling (component: Component): boolean {
return (
component &&
!component.render &&
(component.template || component.extends || component.extendOptions) &&
!component.functional
)
}
export function isRefSelector (refOptionsObject: any): boolean {
if (
typeof refOptionsObject !== 'object' ||
Object.keys(refOptionsObject || {}).length !== 1
) {
return false
}
return typeof refOptionsObject.ref === 'string'
}
export function isNameSelector (nameOptionsObject: any): boolean {
if (typeof nameOptionsObject !== 'object' || nameOptionsObject === null) {
return false
}
return !!nameOptionsObject.name
}
export function isTagSelector (selector: any) {
if (typeof selector !== 'string') {
return false
}
const pseudoSelectors = ['.', '#', '[', ':', '>', ' ']
if (htmlTags.includes(selector) ||
svgElements.includes(selector) ||
selector.split('').some(char => pseudoSelectors.includes(char))) {
/* TODO: Throw error for cases such as find("a > my-component")
const tags = selector.split(/\.|\[|\s|>|#|:/).filter(tag => tag.length > 0)
const componentTags =
tags.filter(tag => {
return !concat(htmlTags, pseudoSelectors, svgElements).includes(tag)
})
componentTags.forEach(tag => {
const selectorWithoutWhitespace = selector.replace(/\s/g, '')
const tagIndex = selectorWithoutWhitespace.indexOf(tag)
if (
pseudoSelectors.includes(
selectorWithoutWhitespace[tagIndex + tag.length]) ||
(tagIndex !== 0 &&
pseudoSelectors.includes(
selectorWithoutWhitespace[tagIndex - 1]))
) {
throwError(
'Pseudo selectors cannot be used with component tag selectors')
}
})
*/
return false
} else {
return true
}
}
export function templateContainsComponent (
template: string,
name: string
): boolean {
return [capitalize, camelize, hyphenate].some(format => {
const re = new RegExp(`<${format(name)}\\s*(\\s|>|(\/>))`, 'g')
return re.test(template)
})
}
export function isPlainObject (obj: any): boolean {
return Object.prototype.toString.call(obj) === '[object Object]'
}
export function isRequiredComponent (name: string): boolean {
return (
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
)
}