This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathscope-helpers.js
More file actions
74 lines (60 loc) · 1.97 KB
/
scope-helpers.js
File metadata and controls
74 lines (60 loc) · 1.97 KB
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
'use babel'
import slick from 'atom-slick'
const EscapeCharacterRegex = /[-!"#$%&'*+,/:;=?@|^~()<>{}[\]]/g
const cachedMatchesBySelector = {}
const getCachedMatch = (selector, scopeChain) => {
const cachedMatchesByScopeChain = cachedMatchesBySelector[selector]
if (cachedMatchesByScopeChain) {
return cachedMatchesByScopeChain[scopeChain]
}
}
const setCachedMatch = (selector, scopeChain, match) => {
let cachedMatchesByScopeChain = cachedMatchesBySelector[selector]
if (!cachedMatchesByScopeChain) {
cachedMatchesByScopeChain = {}
cachedMatchesBySelector[selector] = cachedMatchesByScopeChain
}
cachedMatchesByScopeChain[scopeChain] = match
cachedMatchesByScopeChain[scopeChain]
}
const parseScopeChain = (scopeChain) => {
scopeChain = scopeChain.replace(EscapeCharacterRegex, (match) => {
return '\\' + match[0]
})
const parsed = slick.parse(scopeChain)[0]
if (!parsed || parsed.length === 0) {
return []
}
const result = []
for (let i = 0; i < parsed.length; i++) {
result.push(parsed[i])
}
return result
}
const selectorForScopeChain = (selectors, scopeChain) => {
for (let i = 0; i < selectors.length; i++) {
const selector = selectors[i]
const cachedMatch = getCachedMatch(selector, scopeChain)
if (cachedMatch != null) {
if (cachedMatch) {
return selector
} else {
continue
}
} else {
const scopes = parseScopeChain(scopeChain)
while (scopes.length > 0) {
if (selector.matches(scopes)) {
setCachedMatch(selector, scopeChain, true)
return selector
}
scopes.pop()
}
setCachedMatch(selector, scopeChain, false)
}
}
return null
}
const selectorsMatchScopeChain = (selectors, scopeChain) => { return selectorForScopeChain(selectors, scopeChain) != null }
const buildScopeChainString = (scopes) => { return `.${scopes.join(' .')}` }
export { selectorsMatchScopeChain, selectorForScopeChain, buildScopeChainString }