-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathmatches.js
82 lines (70 loc) · 2.21 KB
/
matches.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
import {
DOM_SELECTOR,
COMPONENT_SELECTOR,
FUNCTIONAL_OPTIONS
} from 'shared/consts'
import { isConstructor } from 'shared/validators'
import { capitalize, camelize } from 'shared/util'
function vmMatchesName(vm, name) {
// We want to mirror how Vue resolves component names in SFCs:
// For example, <test-component />, <TestComponent /> and `<testComponent />
// all resolve to the same component
const componentName = (vm.$options && vm.$options.name) || ''
return (
!!name &&
(componentName === name ||
// testComponent -> TestComponent
componentName === capitalize(name) ||
// test-component -> TestComponent
componentName === capitalize(camelize(name)) ||
// same match as above, but the component name vs query
capitalize(camelize(componentName)) === name)
)
}
function vmCtorMatches(vm, component) {
if (
(vm.$options && vm.$options.$_vueTestUtils_original === component) ||
vm.$_vueTestUtils_original === component
) {
return true
}
const Ctor = isConstructor(component)
? component.options._Ctor
: component._Ctor
if (!Ctor) {
return false
}
if (vm.constructor.extendOptions === component) {
return true
}
if (component.functional) {
return Object.keys(vm._Ctor || {}).some(c => {
return component === vm._Ctor[c].extendOptions
})
}
}
export function matches(node, selector) {
if (selector.type === DOM_SELECTOR) {
const element = node instanceof Element ? node : node.elm
return element && element.matches && element.matches(selector.value)
}
const isFunctionalSelector = isConstructor(selector.value)
? selector.value.options.functional
: selector.value.functional
const componentInstance = isFunctionalSelector
? node[FUNCTIONAL_OPTIONS]
: node.child
if (!componentInstance) {
return false
}
if (selector.type === COMPONENT_SELECTOR) {
if (vmCtorMatches(componentInstance, selector.value)) {
return true
}
}
// Fallback to name selector for COMPONENT_SELECTOR for Vue < 2.1
const nameSelector = isConstructor(selector.value)
? selector.value.extendOptions.name
: selector.value.name
return vmMatchesName(componentInstance, nameSelector)
}