1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | "use strict";
|
9 | var postcss = require("postcss");
|
10 | var pxtorem = require("..");
|
11 | var basicCSS = ".rule { font-size: 15px }";
|
12 | var filterPropList = require("../lib/filter-prop-list");
|
13 |
|
14 | describe("pxtorem", function() {
|
15 | it("should work on the readme example", function() {
|
16 | var input =
|
17 | "h1 { margin: 0 0 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }";
|
18 | var output =
|
19 | "h1 { margin: 0 0 20px; font-size: 2rem; line-height: 1.2; letter-spacing: 0.0625rem; }";
|
20 | var processed = postcss(pxtorem()).process(input).css;
|
21 |
|
22 | expect(processed).toBe(output);
|
23 | });
|
24 |
|
25 | it("should replace the px unit with rem", function() {
|
26 | var processed = postcss(pxtorem()).process(basicCSS).css;
|
27 | var expected = ".rule { font-size: 0.9375rem }";
|
28 |
|
29 | expect(processed).toBe(expected);
|
30 | });
|
31 |
|
32 | it("should ignore non px properties", function() {
|
33 | var expected = ".rule { font-size: 2em }";
|
34 | var processed = postcss(pxtorem()).process(expected).css;
|
35 |
|
36 | expect(processed).toBe(expected);
|
37 | });
|
38 |
|
39 | it("should handle < 1 values and values without a leading 0 - legacy", function() {
|
40 | var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }";
|
41 | var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }";
|
42 | var options = {
|
43 | propWhiteList: ["margin"]
|
44 | };
|
45 | var processed = postcss(pxtorem(options)).process(rules).css;
|
46 |
|
47 | expect(processed).toBe(expected);
|
48 | });
|
49 |
|
50 | it("should ignore px in custom property names", function() {
|
51 | var rules =
|
52 | ":root { --rem-14px: 14px; } .rule { font-size: var(--rem-14px); }";
|
53 | var expected =
|
54 | ":root { --rem-14px: 0.875rem; } .rule { font-size: var(--rem-14px); }";
|
55 | var options = {
|
56 | propList: ["--*", "font-size"]
|
57 | };
|
58 | var processed = postcss(pxtorem(options)).process(rules).css;
|
59 |
|
60 | expect(processed).toBe(expected);
|
61 | });
|
62 |
|
63 | it("should handle < 1 values and values without a leading 0", function() {
|
64 | var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }";
|
65 | var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }";
|
66 | var options = {
|
67 | propList: ["margin"]
|
68 | };
|
69 | var processed = postcss(pxtorem(options)).process(rules).css;
|
70 |
|
71 | expect(processed).toBe(expected);
|
72 | });
|
73 |
|
74 | it("should not add properties that already exist", function() {
|
75 | var expected = ".rule { font-size: 16px; font-size: 1rem; }";
|
76 | var processed = postcss(pxtorem()).process(expected).css;
|
77 |
|
78 | expect(processed).toBe(expected);
|
79 | });
|
80 |
|
81 | it("should remain unitless if 0", function() {
|
82 | var expected = ".rule { font-size: 0px; font-size: 0; }";
|
83 | var processed = postcss(pxtorem()).process(expected).css;
|
84 |
|
85 | expect(processed).toBe(expected);
|
86 | });
|
87 | });
|
88 |
|
89 | describe("value parsing", function() {
|
90 | it("should not replace values in double quotes or single quotes - legacy", function() {
|
91 | var options = {
|
92 | propWhiteList: []
|
93 | };
|
94 | var rules =
|
95 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }";
|
96 | var expected =
|
97 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }";
|
98 | var processed = postcss(pxtorem(options)).process(rules).css;
|
99 |
|
100 | expect(processed).toBe(expected);
|
101 | });
|
102 |
|
103 | it("should not replace values in double quotes or single quotes", function() {
|
104 | var options = {
|
105 | propList: ["*"]
|
106 | };
|
107 | var rules =
|
108 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }";
|
109 | var expected =
|
110 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }";
|
111 | var processed = postcss(pxtorem(options)).process(rules).css;
|
112 |
|
113 | expect(processed).toBe(expected);
|
114 | });
|
115 |
|
116 | it("should not replace values in `url()` - legacy", function() {
|
117 | var options = {
|
118 | propWhiteList: []
|
119 | };
|
120 | var rules = ".rule { background: url(16px.jpg); font-size: 16px; }";
|
121 | var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }";
|
122 | var processed = postcss(pxtorem(options)).process(rules).css;
|
123 |
|
124 | expect(processed).toBe(expected);
|
125 | });
|
126 |
|
127 | it("should not replace values in `url()`", function() {
|
128 | var options = {
|
129 | propList: ["*"]
|
130 | };
|
131 | var rules = ".rule { background: url(16px.jpg); font-size: 16px; }";
|
132 | var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }";
|
133 | var processed = postcss(pxtorem(options)).process(rules).css;
|
134 |
|
135 | expect(processed).toBe(expected);
|
136 | });
|
137 |
|
138 | it("should not replace values with an uppercase P or X", function() {
|
139 | var options = {
|
140 | propList: ["*"]
|
141 | };
|
142 | var rules =
|
143 | ".rule { margin: 12px calc(100% - 14PX); height: calc(100% - 20px); font-size: 12Px; line-height: 16px; }";
|
144 | var expected =
|
145 | ".rule { margin: 0.75rem calc(100% - 14PX); height: calc(100% - 1.25rem); font-size: 12Px; line-height: 1rem; }";
|
146 | var processed = postcss(pxtorem(options)).process(rules).css;
|
147 |
|
148 | expect(processed).toBe(expected);
|
149 | });
|
150 | });
|
151 |
|
152 | describe("rootValue", function() {
|
153 |
|
154 | it("should replace using a root value of 10 - legacy", function() {
|
155 | var expected = ".rule { font-size: 1.5rem }";
|
156 | var options = {
|
157 | root_value: 10
|
158 | };
|
159 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
160 |
|
161 | expect(processed).toBe(expected);
|
162 | });
|
163 |
|
164 | it("should replace using a root value of 10", function() {
|
165 | var expected = ".rule { font-size: 1.5rem }";
|
166 | var options = {
|
167 | rootValue: 10
|
168 | };
|
169 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
170 |
|
171 | expect(processed).toBe(expected);
|
172 | });
|
173 |
|
174 | it("should replace using different root values with different files", function() {
|
175 | var css2 = ".rule { font-size: 20px }";
|
176 | var expected = ".rule { font-size: 1rem }";
|
177 | var options = {
|
178 | rootValue: function(input) {
|
179 | if (input.from.indexOf("basic.css") !== -1) {
|
180 | return 15;
|
181 | }
|
182 | return 20;
|
183 | }
|
184 | };
|
185 | var processed1 = postcss(pxtorem(options)).process(basicCSS, {
|
186 | from: "/tmp/basic.css"
|
187 | }).css;
|
188 | var processed2 = postcss(pxtorem(options)).process(css2, {
|
189 | from: "/tmp/whatever.css"
|
190 | }).css;
|
191 |
|
192 | expect(processed1).toBe(expected);
|
193 | expect(processed2).toBe(expected);
|
194 | });
|
195 | });
|
196 |
|
197 | describe("unitPrecision", function() {
|
198 |
|
199 | it("should replace using a decimal of 2 places - legacy", function() {
|
200 | var expected = ".rule { font-size: 0.94rem }";
|
201 | var options = {
|
202 | unit_precision: 2
|
203 | };
|
204 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
205 |
|
206 | expect(processed).toBe(expected);
|
207 | });
|
208 |
|
209 | it("should replace using a decimal of 2 places", function() {
|
210 | var expected = ".rule { font-size: 0.94rem }";
|
211 | var options = {
|
212 | unitPrecision: 2
|
213 | };
|
214 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
215 |
|
216 | expect(processed).toBe(expected);
|
217 | });
|
218 | });
|
219 |
|
220 | describe("propWhiteList", function() {
|
221 |
|
222 | it("should only replace properties in the white list - legacy", function() {
|
223 | var expected = ".rule { font-size: 15px }";
|
224 | var options = {
|
225 | prop_white_list: ["font"]
|
226 | };
|
227 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
228 |
|
229 | expect(processed).toBe(expected);
|
230 | });
|
231 |
|
232 | it("should only replace properties in the white list - legacy", function() {
|
233 | var expected = ".rule { font-size: 15px }";
|
234 | var options = {
|
235 | propWhiteList: ["font"]
|
236 | };
|
237 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
238 |
|
239 | expect(processed).toBe(expected);
|
240 | });
|
241 |
|
242 | it("should only replace properties in the white list - legacy", function() {
|
243 | var css = ".rule { margin: 16px; margin-left: 10px }";
|
244 | var expected = ".rule { margin: 1rem; margin-left: 10px }";
|
245 | var options = {
|
246 | propWhiteList: ["margin"]
|
247 | };
|
248 | var processed = postcss(pxtorem(options)).process(css).css;
|
249 |
|
250 | expect(processed).toBe(expected);
|
251 | });
|
252 |
|
253 | it("should only replace properties in the prop list", function() {
|
254 | var css =
|
255 | ".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }";
|
256 | var expected =
|
257 | ".rule { font-size: 1rem; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 1rem }";
|
258 | var options = {
|
259 | propWhiteList: ["*font*", "margin*", "!margin-left", "*-right", "pad"]
|
260 | };
|
261 | var processed = postcss(pxtorem(options)).process(css).css;
|
262 |
|
263 | expect(processed).toBe(expected);
|
264 | });
|
265 |
|
266 | it("should only replace properties in the prop list with wildcard", function() {
|
267 | var css =
|
268 | ".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }";
|
269 | var expected =
|
270 | ".rule { font-size: 16px; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 16px }";
|
271 | var options = {
|
272 | propWhiteList: ["*", "!margin-left", "!*padding*", "!font*"]
|
273 | };
|
274 | var processed = postcss(pxtorem(options)).process(css).css;
|
275 |
|
276 | expect(processed).toBe(expected);
|
277 | });
|
278 |
|
279 | it("should replace all properties when white list is empty", function() {
|
280 | var rules = ".rule { margin: 16px; font-size: 15px }";
|
281 | var expected = ".rule { margin: 1rem; font-size: 0.9375rem }";
|
282 | var options = {
|
283 | propWhiteList: []
|
284 | };
|
285 | var processed = postcss(pxtorem(options)).process(rules).css;
|
286 |
|
287 | expect(processed).toBe(expected);
|
288 | });
|
289 | });
|
290 |
|
291 | describe("selectorBlackList", function() {
|
292 |
|
293 | it("should ignore selectors in the selector black list - legacy", function() {
|
294 | var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }";
|
295 | var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }";
|
296 | var options = {
|
297 | selector_black_list: [".rule2"]
|
298 | };
|
299 | var processed = postcss(pxtorem(options)).process(rules).css;
|
300 |
|
301 | expect(processed).toBe(expected);
|
302 | });
|
303 |
|
304 | it("should ignore selectors in the selector black list", function() {
|
305 | var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }";
|
306 | var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }";
|
307 | var options = {
|
308 | selectorBlackList: [".rule2"]
|
309 | };
|
310 | var processed = postcss(pxtorem(options)).process(rules).css;
|
311 |
|
312 | expect(processed).toBe(expected);
|
313 | });
|
314 |
|
315 | it("should ignore every selector with `body$`", function() {
|
316 | var rules =
|
317 | "body { font-size: 16px; } .class-body$ { font-size: 16px; } .simple-class { font-size: 16px; }";
|
318 | var expected =
|
319 | "body { font-size: 1rem; } .class-body$ { font-size: 16px; } .simple-class { font-size: 1rem; }";
|
320 | var options = {
|
321 | selectorBlackList: ["body$"]
|
322 | };
|
323 | var processed = postcss(pxtorem(options)).process(rules).css;
|
324 |
|
325 | expect(processed).toBe(expected);
|
326 | });
|
327 |
|
328 | it("should only ignore exactly `body`", function() {
|
329 | var rules =
|
330 | "body { font-size: 16px; } .class-body { font-size: 16px; } .simple-class { font-size: 16px; }";
|
331 | var expected =
|
332 | "body { font-size: 16px; } .class-body { font-size: 1rem; } .simple-class { font-size: 1rem; }";
|
333 | var options = {
|
334 | selectorBlackList: [/^body$/]
|
335 | };
|
336 | var processed = postcss(pxtorem(options)).process(rules).css;
|
337 |
|
338 | expect(processed).toBe(expected);
|
339 | });
|
340 | });
|
341 |
|
342 | describe("replace", function() {
|
343 | it("should leave fallback pixel unit with root em value", function() {
|
344 | var options = {
|
345 | replace: false
|
346 | };
|
347 | var processed = postcss(pxtorem(options)).process(basicCSS).css;
|
348 | var expected = ".rule { font-size: 15px; font-size: 0.9375rem }";
|
349 |
|
350 | expect(processed).toBe(expected);
|
351 | });
|
352 | });
|
353 |
|
354 | describe("mediaQuery", function() {
|
355 |
|
356 | it("should replace px in media queries", function() {
|
357 | var options = {
|
358 | media_query: true
|
359 | };
|
360 | var processed = postcss(pxtorem(options)).process(
|
361 | "@media (min-width: 500px) { .rule { font-size: 16px } }"
|
362 | ).css;
|
363 | var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }";
|
364 |
|
365 | expect(processed).toBe(expected);
|
366 | });
|
367 |
|
368 | it("should replace px in media queries", function() {
|
369 | var options = {
|
370 | mediaQuery: true
|
371 | };
|
372 | var processed = postcss(pxtorem(options)).process(
|
373 | "@media (min-width: 500px) { .rule { font-size: 16px } }"
|
374 | ).css;
|
375 | var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }";
|
376 |
|
377 | expect(processed).toBe(expected);
|
378 | });
|
379 | });
|
380 |
|
381 | describe("minPixelValue", function() {
|
382 | it("should not replace values below minPixelValue", function() {
|
383 | var options = {
|
384 | propWhiteList: [],
|
385 | minPixelValue: 2
|
386 | };
|
387 | var rules =
|
388 | ".rule { border: 1px solid #000; font-size: 16px; margin: 1px 10px; }";
|
389 | var expected =
|
390 | ".rule { border: 1px solid #000; font-size: 1rem; margin: 1px 0.625rem; }";
|
391 | var processed = postcss(pxtorem(options)).process(rules).css;
|
392 |
|
393 | expect(processed).toBe(expected);
|
394 | });
|
395 | });
|
396 |
|
397 | describe("filter-prop-list", function() {
|
398 | it('should find "exact" matches from propList', function() {
|
399 | var propList = [
|
400 | "font-size",
|
401 | "margin",
|
402 | "!padding",
|
403 | "*border*",
|
404 | "*",
|
405 | "*y",
|
406 | "!*font*"
|
407 | ];
|
408 | var expected = "font-size,margin";
|
409 | expect(filterPropList.exact(propList).join()).toBe(expected);
|
410 | });
|
411 |
|
412 | it('should find "contain" matches from propList and reduce to string', function() {
|
413 | var propList = [
|
414 | "font-size",
|
415 | "*margin*",
|
416 | "!padding",
|
417 | "*border*",
|
418 | "*",
|
419 | "*y",
|
420 | "!*font*"
|
421 | ];
|
422 | var expected = "margin,border";
|
423 | expect(filterPropList.contain(propList).join()).toBe(expected);
|
424 | });
|
425 |
|
426 | it('should find "start" matches from propList and reduce to string', function() {
|
427 | var propList = [
|
428 | "font-size",
|
429 | "*margin*",
|
430 | "!padding",
|
431 | "border*",
|
432 | "*",
|
433 | "*y",
|
434 | "!*font*"
|
435 | ];
|
436 | var expected = "border";
|
437 | expect(filterPropList.startWith(propList).join()).toBe(expected);
|
438 | });
|
439 |
|
440 | it('should find "end" matches from propList and reduce to string', function() {
|
441 | var propList = [
|
442 | "font-size",
|
443 | "*margin*",
|
444 | "!padding",
|
445 | "border*",
|
446 | "*",
|
447 | "*y",
|
448 | "!*font*"
|
449 | ];
|
450 | var expected = "y";
|
451 | expect(filterPropList.endWith(propList).join()).toBe(expected);
|
452 | });
|
453 |
|
454 | it('should find "not" matches from propList and reduce to string', function() {
|
455 | var propList = [
|
456 | "font-size",
|
457 | "*margin*",
|
458 | "!padding",
|
459 | "border*",
|
460 | "*",
|
461 | "*y",
|
462 | "!*font*"
|
463 | ];
|
464 | var expected = "padding";
|
465 | expect(filterPropList.notExact(propList).join()).toBe(expected);
|
466 | });
|
467 |
|
468 | it('should find "not contain" matches from propList and reduce to string', function() {
|
469 | var propList = [
|
470 | "font-size",
|
471 | "*margin*",
|
472 | "!padding",
|
473 | "!border*",
|
474 | "*",
|
475 | "*y",
|
476 | "!*font*"
|
477 | ];
|
478 | var expected = "font";
|
479 | expect(filterPropList.notContain(propList).join()).toBe(expected);
|
480 | });
|
481 |
|
482 | it('should find "not start" matches from propList and reduce to string', function() {
|
483 | var propList = [
|
484 | "font-size",
|
485 | "*margin*",
|
486 | "!padding",
|
487 | "!border*",
|
488 | "*",
|
489 | "*y",
|
490 | "!*font*"
|
491 | ];
|
492 | var expected = "border";
|
493 | expect(filterPropList.notStartWith(propList).join()).toBe(expected);
|
494 | });
|
495 |
|
496 | it('should find "not end" matches from propList and reduce to string', function() {
|
497 | var propList = [
|
498 | "font-size",
|
499 | "*margin*",
|
500 | "!padding",
|
501 | "!border*",
|
502 | "*",
|
503 | "!*y",
|
504 | "!*font*"
|
505 | ];
|
506 | var expected = "y";
|
507 | expect(filterPropList.notEndWith(propList).join()).toBe(expected);
|
508 | });
|
509 | });
|
510 |
|
511 | describe("exclude", function() {
|
512 | it("should ignore file path with exclude RegEx", function() {
|
513 | var options = {
|
514 | exclude: /exclude/i
|
515 | };
|
516 | var processed = postcss(pxtorem(options)).process(basicCSS, {
|
517 | from: "exclude/path"
|
518 | }).css;
|
519 | expect(processed).toBe(basicCSS);
|
520 | });
|
521 |
|
522 | it("should not ignore file path with exclude String", function() {
|
523 | var options = {
|
524 | exclude: "exclude"
|
525 | };
|
526 | var processed = postcss(pxtorem(options)).process(basicCSS, {
|
527 | from: "exclude/path"
|
528 | }).css;
|
529 | expect(processed).toBe(basicCSS);
|
530 | });
|
531 |
|
532 | it("should not ignore file path with exclude function", function() {
|
533 | var options = {
|
534 | exclude: function(file) {
|
535 | return file.indexOf("exclude") !== -1;
|
536 | }
|
537 | };
|
538 | var processed = postcss(pxtorem(options)).process(basicCSS, {
|
539 | from: "exclude/path"
|
540 | }).css;
|
541 | expect(processed).toBe(basicCSS);
|
542 | });
|
543 | });
|