-
Notifications
You must be signed in to change notification settings - Fork 96
/
grammar.js
206 lines (188 loc) · 5.63 KB
/
grammar.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
var _ = require('lodash');
var XRegExp = require('xregexp').XRegExp;
var PSEUDOS = {
':active': ':a',
':checked': ':c',
':default': ':d',
':disabled': ':di',
':empty': ':e',
':enabled': ':en',
':first': ':fi',
':first-child': ':fc',
':first-of-type': ':fot',
':fullscreen': ':fs',
':focus': ':f',
':hover': ':h',
':indeterminate': ':ind',
':in-range': ':ir',
':invalid': ':inv',
':last-child': ':lc',
':last-of-type': ':lot',
':left': ':l',
':link': ':li',
':only-child': ':oc',
':only-of-type': ':oot',
':optional': ':o',
':out-of-range': ':oor',
':read-only': ':ro',
':read-write': ':rw',
':required': ':req',
':right': ':r',
':root': ':rt',
':scope': ':s',
':target': ':t',
':valid': ':va',
':visited': ':vi'
};
var PSEUDOS_INVERTED = _.invert(PSEUDOS);
var PSEUDO_REGEX = [];
for (var pseudo in PSEUDOS) {
PSEUDO_REGEX.push(pseudo);
PSEUDO_REGEX.push(PSEUDOS[pseudo]);
}
PSEUDO_REGEX = '(?:' + PSEUDO_REGEX.join('|') + ')(?![a-z])';
// regular grammar to match valid atomic classes
var GRAMMAR = {
'BOUNDARY' : '(?:^|\\s|"|\'|\{)',
'PARENT' : '[a-zA-Z][-_a-zA-Z0-9]+?',
'PARENT_SEP' : '[>_+]',
// all characters allowed to be a prop
'PROP' : '[A-Za-z]+',
// all character allowed to be in values
'VALUES' : '[-_,.#$/%0-9a-zA-Z]+',
'FRACTION' : '(?<numerator>[0-9]+)\\/(?<denominator>[1-9](?:[0-9]+)?)',
'PARAMS' : '\\((?<params>[^)]*)\\)',
'NUMBER' : '-?[0-9]+(?:\.[0-9]+)?|\\.[0-9]+',
'UNIT' : '[a-zA-Z%]+',
'HEX' : '#[0-9a-f]{3}(?:[0-9a-f]{3})?',
'ALPHA' : '\\.\\d{1,2}',
'IMPORTANT' : '!',
// https://regex101.com/r/mM2vT9/8
'NAMED' : '([\\w$]+(?:(?:-(?!\\-))?\\w*)*)',
'PSEUDO' : PSEUDO_REGEX,
'PSEUDO_SIMPLE' : ':[a-z]+',
'BREAKPOINT' : '--(?<breakPoint>[a-z]+)'
};
GRAMMAR.PARENT_SELECTOR = [
// parent (any character that is not a space)
'(?<parent>',
GRAMMAR.PARENT,
')',
// followed by optional pseudo class
'(?<parentPseudo>',
GRAMMAR.PSEUDO,
')?',
// followed by either a descendant or direct symbol
'(?<parentSep>',
GRAMMAR.PARENT_SEP,
')'
].join('');
GRAMMAR.PARENT_SELECTOR_SIMPLE = [
// parent (any character that is not a space)
'(?<parent>',
GRAMMAR.PARENT,
')',
// followed by optional pseudo class
'(?<parentPseudo>',
GRAMMAR.PSEUDO_SIMPLE,
')?',
// followed by either a descendant or direct symbol
'(?<parentSep>',
GRAMMAR.PARENT_SEP,
')'
].join('');
var VALUE_SYNTAXE = XRegExp([
'(?<fraction>',
GRAMMAR.FRACTION,
')',
'|',
'(?:',
'(?<hex>',
GRAMMAR.HEX,
')',
'(?<alpha>',
GRAMMAR.ALPHA,
')?',
'(?!',
GRAMMAR.UNIT,
')',
')',
'|',
'(?<number>',
GRAMMAR.NUMBER,
')',
'(?<unit>',
GRAMMAR.UNIT,
')?',
'|',
'(?<named>',
GRAMMAR.NAMED,
')',
].join(''));
/**
* sort matchers by descending alphabetical order
* this is important so "B" doesn't match "Bgc"
* e.g. Use (Bgc|B) instead of (B|Bgc)
*/
function getSortedKeys(arr) {
return arr.length > 1 ? arr.sort(function (a, b) {
return a > b ? -1 : 1;
}).join('|') : arr.toString();
}
function buildRegex(matchersParams, matchersNoParams) {
matchersParams = matchersParams ? '(?<atomicSelector>' + matchersParams + ')\\((?<atomicValues>' + GRAMMAR.VALUES + ')\\)' : '';
matchersNoParams = matchersNoParams ? '(?<selector>' + matchersNoParams + ')' : '';
return '(?:' + [matchersParams, matchersNoParams].join('|') + ')';
}
function Grammar(rules) {
var matchersParams = [];
var matchersNoParams = [];
var matchersParamsStr;
var matchersNoParamsStr;
rules.forEach(function (rule) {
if (rule.noParams) {
matchersNoParams.push(rule.matcher);
} else {
matchersParams.push(rule.matcher);
}
});
matchersParamsStr = getSortedKeys(matchersParams);
matchersNoParamsStr = getSortedKeys(matchersNoParams);
this.simpleSyntax = buildRegex(GRAMMAR.PROP, matchersNoParamsStr);
this.complexSyntax = buildRegex(matchersParamsStr, matchersNoParamsStr);
}
/**
* get non abbreviated pseudo class string given abbreviated or non abbreviated form
*/
Grammar.getPseudo = function getPseudo(pseudoName)/*:string*/ {
return PSEUDOS[pseudoName] ? pseudoName : PSEUDOS_INVERTED[pseudoName];
};
Grammar.matchValue = function matchValue(value) {
return XRegExp.exec(value, VALUE_SYNTAXE);
};
Grammar.prototype.getSyntax = function getSyntax(isSimple)/*:string*/ {
var syntax = [
// word boundary
GRAMMAR.BOUNDARY,
// optional parent
'(?<parentSelector>',
isSimple ? GRAMMAR.PARENT_SELECTOR_SIMPLE : GRAMMAR.PARENT_SELECTOR,
')?',
// the main syntax
isSimple ? this.simpleSyntax : this.complexSyntax,
'(?<important>',
GRAMMAR.IMPORTANT,
')?',
// optional pseudo
'(?<valuePseudo>',
isSimple ? GRAMMAR.PSEUDO_SIMPLE : GRAMMAR.PSEUDO,
')?',
// optional modifier
'(?:',
GRAMMAR.BREAKPOINT,
')?'
].join('');
return XRegExp(syntax, 'g');
};
module.exports = Grammar;