-
Notifications
You must be signed in to change notification settings - Fork 174
/
pxtorem-spec.js
543 lines (466 loc) · 16 KB
/
pxtorem-spec.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Jasmine unit tests
// To run tests, run these commands from the project root:
// 1. `npm install -g jasmine-node`
// 2. `jasmine-node spec`
/* global describe, it, expect */
"use strict";
var postcss = require("postcss");
var pxtorem = require("..");
var basicCSS = ".rule { font-size: 15px }";
var filterPropList = require("../lib/filter-prop-list");
describe("pxtorem", function() {
it("should work on the readme example", function() {
var input =
"h1 { margin: 0 0 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }";
var output =
"h1 { margin: 0 0 20px; font-size: 2rem; line-height: 1.2; letter-spacing: 0.0625rem; }";
var processed = postcss(pxtorem()).process(input).css;
expect(processed).toBe(output);
});
it("should replace the px unit with rem", function() {
var processed = postcss(pxtorem()).process(basicCSS).css;
var expected = ".rule { font-size: 0.9375rem }";
expect(processed).toBe(expected);
});
it("should ignore non px properties", function() {
var expected = ".rule { font-size: 2em }";
var processed = postcss(pxtorem()).process(expected).css;
expect(processed).toBe(expected);
});
it("should handle < 1 values and values without a leading 0 - legacy", function() {
var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }";
var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }";
var options = {
propWhiteList: ["margin"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should ignore px in custom property names", function() {
var rules =
":root { --rem-14px: 14px; } .rule { font-size: var(--rem-14px); }";
var expected =
":root { --rem-14px: 0.875rem; } .rule { font-size: var(--rem-14px); }";
var options = {
propList: ["--*", "font-size"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should handle < 1 values and values without a leading 0", function() {
var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }";
var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }";
var options = {
propList: ["margin"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should not add properties that already exist", function() {
var expected = ".rule { font-size: 16px; font-size: 1rem; }";
var processed = postcss(pxtorem()).process(expected).css;
expect(processed).toBe(expected);
});
it("should remain unitless if 0", function() {
var expected = ".rule { font-size: 0px; font-size: 0; }";
var processed = postcss(pxtorem()).process(expected).css;
expect(processed).toBe(expected);
});
});
describe("value parsing", function() {
it("should not replace values in double quotes or single quotes - legacy", function() {
var options = {
propWhiteList: []
};
var rules =
".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }";
var expected =
".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should not replace values in double quotes or single quotes", function() {
var options = {
propList: ["*"]
};
var rules =
".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }";
var expected =
".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should not replace values in `url()` - legacy", function() {
var options = {
propWhiteList: []
};
var rules = ".rule { background: url(16px.jpg); font-size: 16px; }";
var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should not replace values in `url()`", function() {
var options = {
propList: ["*"]
};
var rules = ".rule { background: url(16px.jpg); font-size: 16px; }";
var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should not replace values with an uppercase P or X", function() {
var options = {
propList: ["*"]
};
var rules =
".rule { margin: 12px calc(100% - 14PX); height: calc(100% - 20px); font-size: 12Px; line-height: 16px; }";
var expected =
".rule { margin: 0.75rem calc(100% - 14PX); height: calc(100% - 1.25rem); font-size: 12Px; line-height: 1rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
});
describe("rootValue", function() {
// Deprecate
it("should replace using a root value of 10 - legacy", function() {
var expected = ".rule { font-size: 1.5rem }";
var options = {
root_value: 10
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
it("should replace using a root value of 10", function() {
var expected = ".rule { font-size: 1.5rem }";
var options = {
rootValue: 10
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
it("should replace using different root values with different files", function() {
var css2 = ".rule { font-size: 20px }";
var expected = ".rule { font-size: 1rem }";
var options = {
rootValue: function(input) {
if (input.from.indexOf("basic.css") !== -1) {
return 15;
}
return 20;
}
};
var processed1 = postcss(pxtorem(options)).process(basicCSS, {
from: "/tmp/basic.css"
}).css;
var processed2 = postcss(pxtorem(options)).process(css2, {
from: "/tmp/whatever.css"
}).css;
expect(processed1).toBe(expected);
expect(processed2).toBe(expected);
});
});
describe("unitPrecision", function() {
// Deprecate
it("should replace using a decimal of 2 places - legacy", function() {
var expected = ".rule { font-size: 0.94rem }";
var options = {
unit_precision: 2
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
it("should replace using a decimal of 2 places", function() {
var expected = ".rule { font-size: 0.94rem }";
var options = {
unitPrecision: 2
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
});
describe("propWhiteList", function() {
// Deprecate
it("should only replace properties in the white list - legacy", function() {
var expected = ".rule { font-size: 15px }";
var options = {
prop_white_list: ["font"]
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
it("should only replace properties in the white list - legacy", function() {
var expected = ".rule { font-size: 15px }";
var options = {
propWhiteList: ["font"]
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
expect(processed).toBe(expected);
});
it("should only replace properties in the white list - legacy", function() {
var css = ".rule { margin: 16px; margin-left: 10px }";
var expected = ".rule { margin: 1rem; margin-left: 10px }";
var options = {
propWhiteList: ["margin"]
};
var processed = postcss(pxtorem(options)).process(css).css;
expect(processed).toBe(expected);
});
it("should only replace properties in the prop list", function() {
var css =
".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }";
var expected =
".rule { font-size: 1rem; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 1rem }";
var options = {
propWhiteList: ["*font*", "margin*", "!margin-left", "*-right", "pad"]
};
var processed = postcss(pxtorem(options)).process(css).css;
expect(processed).toBe(expected);
});
it("should only replace properties in the prop list with wildcard", function() {
var css =
".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }";
var expected =
".rule { font-size: 16px; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 16px }";
var options = {
propWhiteList: ["*", "!margin-left", "!*padding*", "!font*"]
};
var processed = postcss(pxtorem(options)).process(css).css;
expect(processed).toBe(expected);
});
it("should replace all properties when white list is empty", function() {
var rules = ".rule { margin: 16px; font-size: 15px }";
var expected = ".rule { margin: 1rem; font-size: 0.9375rem }";
var options = {
propWhiteList: []
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
});
describe("selectorBlackList", function() {
// Deprecate
it("should ignore selectors in the selector black list - legacy", function() {
var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }";
var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }";
var options = {
selector_black_list: [".rule2"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should ignore selectors in the selector black list", function() {
var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }";
var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }";
var options = {
selectorBlackList: [".rule2"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should ignore every selector with `body$`", function() {
var rules =
"body { font-size: 16px; } .class-body$ { font-size: 16px; } .simple-class { font-size: 16px; }";
var expected =
"body { font-size: 1rem; } .class-body$ { font-size: 16px; } .simple-class { font-size: 1rem; }";
var options = {
selectorBlackList: ["body$"]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
it("should only ignore exactly `body`", function() {
var rules =
"body { font-size: 16px; } .class-body { font-size: 16px; } .simple-class { font-size: 16px; }";
var expected =
"body { font-size: 16px; } .class-body { font-size: 1rem; } .simple-class { font-size: 1rem; }";
var options = {
selectorBlackList: [/^body$/]
};
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
});
describe("replace", function() {
it("should leave fallback pixel unit with root em value", function() {
var options = {
replace: false
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;
var expected = ".rule { font-size: 15px; font-size: 0.9375rem }";
expect(processed).toBe(expected);
});
});
describe("mediaQuery", function() {
// Deprecate
it("should replace px in media queries", function() {
var options = {
media_query: true
};
var processed = postcss(pxtorem(options)).process(
"@media (min-width: 500px) { .rule { font-size: 16px } }"
).css;
var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }";
expect(processed).toBe(expected);
});
it("should replace px in media queries", function() {
var options = {
mediaQuery: true
};
var processed = postcss(pxtorem(options)).process(
"@media (min-width: 500px) { .rule { font-size: 16px } }"
).css;
var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }";
expect(processed).toBe(expected);
});
});
describe("minPixelValue", function() {
it("should not replace values below minPixelValue", function() {
var options = {
propWhiteList: [],
minPixelValue: 2
};
var rules =
".rule { border: 1px solid #000; font-size: 16px; margin: 1px 10px; }";
var expected =
".rule { border: 1px solid #000; font-size: 1rem; margin: 1px 0.625rem; }";
var processed = postcss(pxtorem(options)).process(rules).css;
expect(processed).toBe(expected);
});
});
describe("filter-prop-list", function() {
it('should find "exact" matches from propList', function() {
var propList = [
"font-size",
"margin",
"!padding",
"*border*",
"*",
"*y",
"!*font*"
];
var expected = "font-size,margin";
expect(filterPropList.exact(propList).join()).toBe(expected);
});
it('should find "contain" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"*border*",
"*",
"*y",
"!*font*"
];
var expected = "margin,border";
expect(filterPropList.contain(propList).join()).toBe(expected);
});
it('should find "start" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"border*",
"*",
"*y",
"!*font*"
];
var expected = "border";
expect(filterPropList.startWith(propList).join()).toBe(expected);
});
it('should find "end" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"border*",
"*",
"*y",
"!*font*"
];
var expected = "y";
expect(filterPropList.endWith(propList).join()).toBe(expected);
});
it('should find "not" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"border*",
"*",
"*y",
"!*font*"
];
var expected = "padding";
expect(filterPropList.notExact(propList).join()).toBe(expected);
});
it('should find "not contain" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"!border*",
"*",
"*y",
"!*font*"
];
var expected = "font";
expect(filterPropList.notContain(propList).join()).toBe(expected);
});
it('should find "not start" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"!border*",
"*",
"*y",
"!*font*"
];
var expected = "border";
expect(filterPropList.notStartWith(propList).join()).toBe(expected);
});
it('should find "not end" matches from propList and reduce to string', function() {
var propList = [
"font-size",
"*margin*",
"!padding",
"!border*",
"*",
"!*y",
"!*font*"
];
var expected = "y";
expect(filterPropList.notEndWith(propList).join()).toBe(expected);
});
});
describe("exclude", function() {
it("should ignore file path with exclude RegEx", function() {
var options = {
exclude: /exclude/i
};
var processed = postcss(pxtorem(options)).process(basicCSS, {
from: "exclude/path"
}).css;
expect(processed).toBe(basicCSS);
});
it("should not ignore file path with exclude String", function() {
var options = {
exclude: "exclude"
};
var processed = postcss(pxtorem(options)).process(basicCSS, {
from: "exclude/path"
}).css;
expect(processed).toBe(basicCSS);
});
it("should not ignore file path with exclude function", function() {
var options = {
exclude: function(file) {
return file.indexOf("exclude") !== -1;
}
};
var processed = postcss(pxtorem(options)).process(basicCSS, {
from: "exclude/path"
}).css;
expect(processed).toBe(basicCSS);
});
});