forked from syntax-tree/unist-util-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
65 lines (48 loc) · 1.28 KB
/
parse.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
'use strict'
module.exports = parse
var Parser = require('css-selector-parser').CssSelectorParser
var zwitch = require('zwitch')
var nthCheck = require('nth-check').default
var nth = ['nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type']
var parser = new Parser()
var compile = zwitch('type', {
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
})
parser.registerAttrEqualityMods('~', '^', '$', '*')
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
parser.registerNestingOperators('>', '+', '~')
function parse(selector) {
if (typeof selector !== 'string') {
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
}
return compile(parser.parse(selector))
}
function selectors(query) {
var selectors = query.selectors
var index = -1
while (++index < selectors.length) {
compile(selectors[index])
}
return query
}
function ruleSet(query) {
return rule(query.rule)
}
function rule(query) {
var pseudos = query.pseudos || []
var index = -1
var pseudo
while (++index < pseudos.length) {
pseudo = pseudos[index]
if (nth.indexOf(pseudo.name) > -1) {
pseudo.value = nthCheck(pseudo.value)
pseudo.valueType = 'function'
}
}
compile(query.rule)
return query
}