parser.js
6.94 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _index = _interopRequireDefault(require("../index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Node creation
var nodeTypes = [['attribute', '[href]', {
attribute: 'href'
}], ['className', '.classy', {
value: 'classy'
}], ['combinator', ' >> ', {
value: '>>',
spaces: {
before: ' ',
after: ' '
}
}], ['comment', '/* comment */', {
value: '/* comment */'
}], ['id', '#test', {
value: 'test'
}], ['nesting', '&'], ['pseudo', '::before', {
value: '::before'
}], ['string', '"wow"', {
value: '"wow"'
}], ['tag', 'button', {
value: 'button'
}], ['universal', '*']];
nodeTypes.forEach(function (type) {
(0, _ava.default)("parser#" + type[0], function (t) {
var node = _index.default[type[0]](type[2] || {});
t.deepEqual(String(node), type[1]);
});
});
(0, _ava.default)('string constants', function (t) {
t.truthy(_index.default.TAG);
t.truthy(_index.default.STRING);
t.truthy(_index.default.SELECTOR);
t.truthy(_index.default.ROOT);
t.truthy(_index.default.PSEUDO);
t.truthy(_index.default.NESTING);
t.truthy(_index.default.ID);
t.truthy(_index.default.COMMENT);
t.truthy(_index.default.COMBINATOR);
t.truthy(_index.default.CLASS);
t.truthy(_index.default.ATTRIBUTE);
t.truthy(_index.default.UNIVERSAL);
});
(0, _ava.default)('construct a whole tree', function (t) {
var root = _index.default.root();
var selector = _index.default.selector();
selector.append(_index.default.id({
value: 'tree'
}));
root.append(selector);
t.deepEqual(String(root), '#tree');
});
(0, _ava.default)('no operation', function (t) {
t.notThrows(function () {
return (0, _index.default)().processSync('h1 h2 h3');
});
});
(0, _ava.default)('empty selector string', function (t) {
t.notThrows(function () {
return (0, _index.default)(function (selectors) {
selectors.walk(function (selector) {
selector.type = 'tag';
});
}).processSync('');
});
});
(0, _ava.default)('async parser', function (t) {
return (0, _index.default)(function (selectors) {
return new Promise(function (res) {
setTimeout(function () {
selectors.first.nodes[0].value = 'bar';
res();
}, 1);
});
}).process('foo').then(function (result) {
t.deepEqual(result, 'bar');
});
});
(0, _ava.default)('parse errors with the async parser', function (t) {
return (0, _index.default)(function (selectors) {
return new Promise(function (res) {
setTimeout(function () {
selectors.first.nodes[0].value = 'bar';
res();
}, 1);
});
}).process('a b: c').catch(function (err) {
return t.truthy(err);
});
});
(0, _ava.default)('parse errors within the async processor', function (t) {
return (0, _index.default)(function (selectors) {
return new Promise(function (res, rej) {
setTimeout(function () {
rej(selectors.error("async error"));
}, 1);
});
}).process('.foo').catch(function (err) {
return t.truthy(err);
});
});
(0, _ava.default)('parse errors within the async processor before the promise returns', function (t) {
return (0, _index.default)(function (selectors) {
throw selectors.error("async error");
}).process('.foo').catch(function (err) {
return t.truthy(err);
});
});
(0, _ava.default)('returning a promise to the sync processor fails', function (t) {
t.throws(function () {
return (0, _index.default)(function () {
return new Promise(function (res) {
setTimeout(function () {
res();
}, 1);
});
}).processSync('.foo');
});
});
(0, _ava.default)('Passing a rule works async', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index.default)(function (root) {
return new Promise(function (res) {
setTimeout(function () {
root.walkClasses(function (node) {
node.value = "bar";
});
res();
}, 1);
});
}).process(rule).then(function (newSel) {
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".bar");
});
});
(0, _ava.default)('Passing a rule with mutation disabled works async', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index.default)(function (root) {
return new Promise(function (res) {
setTimeout(function () {
root.walkClasses(function (node) {
node.value = "bar";
});
res();
}, 1);
});
}).process(rule, {
updateSelector: false
}).then(function (newSel) {
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".foo");
});
});
(0, _ava.default)('Passing a rule with mutation works sync', function (t) {
var rule = {
selector: '.foo'
};
var newSel = (0, _index.default)(function (root) {
root.walkClasses(function (node) {
node.value = "bar";
});
}).processSync(rule, {
updateSelector: true
});
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".bar");
});
(0, _ava.default)('Transform a selector synchronously', function (t) {
var rule = {
selector: '.foo'
};
var count = (0, _index.default)(function (root) {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
return classCount;
}).transformSync(rule, {
updateSelector: true
});
t.deepEqual(count, 1);
t.deepEqual(rule.selector, ".bar");
});
(0, _ava.default)('Transform a selector asynchronously', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index.default)(function (root) {
return new Promise(function (res) {
setTimeout(function () {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
res(classCount);
}, 1);
});
}).transform(rule, {
updateSelector: true
}).then(function (count) {
t.deepEqual(count, 1);
t.deepEqual(rule.selector, ".bar");
});
});
(0, _ava.default)('get AST of a selector synchronously', function (t) {
var rule = {
selector: '.foo'
};
var ast = (0, _index.default)(function (root) {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
return classCount;
}).astSync(rule, {
updateSelector: true
});
t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
t.deepEqual(rule.selector, ".bar");
});
(0, _ava.default)('get AST a selector asynchronously', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index.default)(function (root) {
return new Promise(function (res) {
setTimeout(function () {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
res(classCount);
}, 1);
});
}).ast(rule, {
updateSelector: true
}).then(function (ast) {
t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
t.deepEqual(rule.selector, ".bar");
});
});