Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
469 lines (407 loc) · 12.7 KB

File metadata and controls

469 lines (407 loc) · 12.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
'use strict';
const LazyResult = require('../lib/lazy-result');
const Processor = require('../lib/processor');
const postcss = require('../lib/postcss');
const Result = require('../lib/result');
const parse = require('../lib/parse');
const Root = require('../lib/root');
const path = require('path');
function prs() {
return new Root({ raws: { after: 'ok' } });
}
function str(node, builder) {
builder(node.raws.after + '!');
}
let beforeFix = new Processor([ css => {
css.walkRules( rule => {
if ( !rule.selector.match(/::(before|after)/) ) return;
if ( !rule.some( i => i.prop === 'content' ) ) {
rule.prepend({ prop: 'content', value: '""' });
}
});
}]);
const originWarn = console.warn;
const originError = console.error;
afterAll(() => {
console.warn = originWarn;
console.error = originError;
});
it('adds new plugins', () => {
let a = () => 1;
let processor = new Processor();
processor.use(a);
expect(processor.plugins).toEqual([a]);
});
it('adds new plugin by object', () => {
let a = () => 1;
let processor = new Processor();
processor.use({ postcss: a });
expect(processor.plugins).toEqual([a]);
});
it('adds new plugin by object-function', () => {
let a = () => 1;
let obj = () => 2;
obj.postcss = a;
let processor = new Processor();
processor.use(obj);
expect(processor.plugins).toEqual([a]);
});
it('adds new processors of another postcss instance', () => {
let a = () => 1;
let processor = new Processor();
let other = new Processor([a]);
processor.use(other);
expect(processor.plugins).toEqual([a]);
});
it('adds new processors from object', () => {
let a = () => 1;
let processor = new Processor();
let other = new Processor([a]);
processor.use({ postcss: other });
expect(processor.plugins).toEqual([a]);
});
it('returns itself', () => {
let a = () => 1;
let b = () => 2;
let processor = new Processor();
expect(processor.use(a).use(b).plugins).toEqual([a, b]);
});
it('throws on wrong format', () => {
let pr = new Processor();
expect(() => {
pr.use(1);
}).toThrowError(/1 is not a PostCSS plugin/);
});
it('processes CSS', () => {
let result = beforeFix.process('a::before{top:0}');
expect(result.css).toEqual('a::before{content:"";top:0}');
});
it('processes parsed AST', () => {
let root = parse('a::before{top:0}');
let result = beforeFix.process(root);
expect(result.css).toEqual('a::before{content:"";top:0}');
});
it('processes previous result', () => {
let result = (new Processor()).process('a::before{top:0}');
result = beforeFix.process(result);
expect(result.css).toEqual('a::before{content:"";top:0}');
});
it('takes maps from previous result', () => {
let one = (new Processor()).process('a{}', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let two = (new Processor()).process(one, { to: 'c.css' });
expect(two.map.toJSON().sources).toEqual(['a.css']);
});
it('inlines maps from previous result', () => {
let one = (new Processor()).process('a{}', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let two = (new Processor()).process(one, {
to: 'c.css',
map: { inline: true }
});
expect(two.map).not.toBeDefined();
});
it('throws with file name', () => {
let error;
try {
(new Processor()).process('a {', { from: 'a.css' }).css;
} catch (e) {
if ( e.name === 'CssSyntaxError' ) {
error = e;
} else {
throw e;
}
}
expect(error.file).toEqual(path.resolve('a.css'));
expect(error.message).toMatch(/a.css:1:1: Unclosed block$/);
});
it('allows to replace Root', () => {
let plugin = (css, result) => {
result.root = new Root();
};
let processor = new Processor([plugin]);
expect(processor.process('a {}').css).toEqual('');
});
it('returns LazyResult object', () => {
let result = (new Processor()).process('a{}');
expect(result instanceof LazyResult).toBeTruthy();
expect(result.css).toEqual('a{}');
expect(result.toString()).toEqual('a{}');
});
it('calls all plugins once', () => {
expect.assertions(1);
let calls = '';
let a = () => {
calls += 'a';
};
let b = () => {
calls += 'b';
};
let result = new Processor([a, b]).process('', { from: undefined });
result.css;
result.map;
result.root;
return result.then( () => {
expect(calls).toEqual('ab');
});
});
it('parses, converts and stringifies CSS', () => {
let a = css => expect(css instanceof Root).toBeTruthy();
expect(typeof (new Processor([a])).process('a {}').css).toEqual('string');
});
it('send result to plugins', () => {
expect.assertions(4);
let processor = new Processor();
let a = (css, result) => {
expect(result instanceof Result).toBeTruthy();
expect(result.processor).toEqual(processor);
expect(result.opts).toEqual({ map: true });
expect(result.root).toEqual(css);
};
return processor.use(a).process('a {}', { map: true, from: undefined });
});
it('accepts source map from PostCSS', () => {
let one = (new Processor()).process('a{}', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let two = (new Processor()).process(one.css, {
from: 'b.css',
to: 'c.css',
map: { prev: one.map, inline: false }
});
expect(two.map.toJSON().sources).toEqual(['a.css']);
});
it('supports async plugins', () => {
let starts = 0;
let finish = 0;
let async1 = css => {
return new Promise(resolve => {
starts += 1;
setTimeout(() => {
expect(starts).toEqual(1);
css.append('a {}');
finish += 1;
resolve();
}, 1);
});
};
let async2 = css => {
return new Promise(resolve => {
expect(starts).toEqual(1);
expect(finish).toEqual(1);
starts += 1;
setTimeout(() => {
css.append('b {}');
finish += 1;
resolve();
}, 1);
});
};
return (new Processor([async1, async2])).process('', { from: undefined })
.then( result => {
expect(starts).toEqual(2);
expect(finish).toEqual(2);
expect(result.css).toEqual('a {}b {}');
});
});
it('works async without plugins', () => {
return (new Processor()).process('a {}', { from: undefined })
.then( result => {
expect(result.css).toEqual('a {}');
});
});
it('runs async plugin only once', () => {
expect.assertions(1);
let calls = 0;
let async = () => {
return new Promise( resolve => {
setTimeout(() => {
calls += 1;
resolve();
}, 1);
});
};
let result = (new Processor([async])).process('a {}', { from: undefined });
result.then( () => { });
return result.then( () => {
return result.then( () => {
expect(calls).toEqual(1);
});
});
});
it('supports async errors', done => {
let error = new Error('Async');
let async = () => {
return new Promise( (resolve, reject) => {
reject(error);
});
};
let result = (new Processor([async])).process('', { from: undefined });
result.then( () => {
done.fail();
}).catch( err => {
expect(err).toEqual(error);
return result.catch( err2 => {
expect(err2).toEqual(error);
done();
});
});
});
it('supports sync errors in async mode', done => {
let error = new Error('Async');
let async = () => {
throw error;
};
(new Processor([async])).process('', { from: undefined }).then( () => {
done.fail();
}).catch( err => {
expect(err).toEqual(error);
done();
});
});
it('throws parse error in async', () => {
return (new Processor()).process('a{').catch( err => {
expect(err.message).toEqual('<css input>:1:1: Unclosed block');
});
});
it('throws error on sync method to async plugin', () => {
let async = () => {
return new Promise( resolve => resolve() );
};
expect(() => {
(new Processor([async])).process('a{}').css;
}).toThrowError(/async/);
});
it('throws a sync call in async running', () => {
let async = () => new Promise( done => setTimeout(done, 1) );
let processor = (new Processor([async])).process('a{}');
processor.async();
expect(() => {
processor.sync();
}).toThrowError(/then/);
});
it('checks plugin compatibility', () => {
let plugin = postcss.plugin('test', () => {
return () => {
throw new Error('Er');
};
});
let func = plugin();
func.postcssVersion = '2.1.5';
let processBy = version => {
let processor = new Processor([func]);
processor.version = version;
processor.process('a{}').css;
};
console.error = jest.fn();
expect(() => {
processBy('1.0.0');
}).toThrowError('Er');
expect(console.error.mock.calls.length).toEqual(1);
expect(console.error.mock.calls[0][0]).toEqual(
'Unknown error from PostCSS plugin. ' +
'Your current PostCSS version is 1.0.0, but test uses 2.1.5. ' +
'Perhaps this is the source of the error below.');
expect(() => {
processBy('3.0.0');
}).toThrowError('Er');
expect(console.error.mock.calls.length).toEqual(2);
expect(() => {
processBy('2.0.0');
}).toThrowError('Er');
expect(console.error.mock.calls.length).toEqual(3);
expect(() => {
processBy('2.1.0');
}).toThrowError('Er');
expect(console.error.mock.calls.length).toEqual(3);
});
it('sets last plugin to result', () => {
let plugin1 = function (css, result) {
expect(result.lastPlugin).toBe(plugin1);
};
let plugin2 = function (css, result) {
expect(result.lastPlugin).toBe(plugin2);
};
let processor = new Processor([plugin1, plugin2]);
return processor.process('a{}', { from: undefined }).then( result => {
expect(result.lastPlugin).toBe(plugin2);
});
});
it('uses custom parsers', () => {
let processor = new Processor([]);
return processor.process('a{}', { parser: prs, from: undefined })
.then( result => {
expect(result.css).toEqual('ok');
});
});
it('uses custom parsers from object', () => {
let processor = new Processor([]);
let syntax = { parse: prs, stringify: str };
return processor.process('a{}', { parser: syntax, from: undefined })
.then( result => {
expect(result.css).toEqual('ok');
});
});
it('uses custom stringifier', () => {
let processor = new Processor([]);
return processor.process('a{}', { stringifier: str, from: undefined })
.then( result => {
expect(result.css).toEqual('!');
});
});
it('uses custom stringifier from object', () => {
let processor = new Processor([]);
let syntax = { parse: prs, stringify: str };
return processor.process('', { stringifier: syntax, from: undefined })
.then( result => {
expect(result.css).toEqual('!');
});
});
it('uses custom stringifier with source maps', () => {
let processor = new Processor([]);
return processor.process('a{}', {
map: true, stringifier: str, from: undefined
}).then( result => {
expect(result.css).toMatch(/!\n\/\*# sourceMap/);
});
});
it('uses custom syntax', () => {
let processor = new Processor([]);
return processor.process('a{}', {
syntax: { parse: prs, stringify: str }, from: undefined
}).then( result => {
expect(result.css).toEqual('ok!');
});
});
it('contains PostCSS version', () => {
expect((new Processor()).version).toMatch(/\d+.\d+.\d+/);
});
it('throws on syntax as plugin', () => {
let processor = new Processor();
expect(() => {
processor.use({
parse() { }
});
}).toThrowError(/syntax/);
});
it('warns about missed from', () => {
console.warn = jest.fn();
let processor = new Processor();
processor.process('a{}').css;
expect(console.warn).not.toBeCalled();
return processor.process('a{}').then(() => {
expect(console.warn).toBeCalledWith(
'Without `from` option PostCSS could generate wrong source map ' +
'and will not find Browserslist config. Set it to CSS file path ' +
'or to `undefined` to prevent this warning.'
);
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.