9
9
var postcss = require ( 'postcss' ) ;
10
10
var pxtorem = require ( '..' ) ;
11
11
var basicCSS = '.rule { font-size: 15px }' ;
12
+ var filterPropList = require ( '../lib/filter-prop-list' ) ;
12
13
13
14
describe ( 'pxtorem' , function ( ) {
14
15
it ( 'should work on the readme example' , function ( ) {
@@ -33,7 +34,7 @@ describe('pxtorem', function () {
33
34
expect ( processed ) . toBe ( expected ) ;
34
35
} ) ;
35
36
36
- it ( 'should handle < 1 values and values without a leading 0' , function ( ) {
37
+ it ( 'should handle < 1 values and values without a leading 0 - legacy ' , function ( ) {
37
38
var rules = '.rule { margin: 0.5rem .5px -0.2px -.2em }' ;
38
39
var expected = '.rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }' ;
39
40
var options = {
@@ -44,6 +45,17 @@ describe('pxtorem', function () {
44
45
expect ( processed ) . toBe ( expected ) ;
45
46
} ) ;
46
47
48
+ it ( 'should handle < 1 values and values without a leading 0' , function ( ) {
49
+ var rules = '.rule { margin: 0.5rem .5px -0.2px -.2em }' ;
50
+ var expected = '.rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }' ;
51
+ var options = {
52
+ propList : [ 'margin' ]
53
+ } ;
54
+ var processed = postcss ( pxtorem ( options ) ) . process ( rules ) . css ;
55
+
56
+ expect ( processed ) . toBe ( expected ) ;
57
+ } ) ;
58
+
47
59
it ( 'should not add properties that already exist' , function ( ) {
48
60
var expected = '.rule { font-size: 16px; font-size: 1rem; }' ;
49
61
var processed = postcss ( pxtorem ( ) ) . process ( expected ) . css ;
@@ -53,7 +65,7 @@ describe('pxtorem', function () {
53
65
} ) ;
54
66
55
67
describe ( 'value parsing' , function ( ) {
56
- it ( 'should not replace values in double quotes or single quotes' , function ( ) {
68
+ it ( 'should not replace values in double quotes or single quotes - legacy ' , function ( ) {
57
69
var options = {
58
70
propWhiteList : [ ]
59
71
} ;
@@ -64,7 +76,18 @@ describe('value parsing', function () {
64
76
expect ( processed ) . toBe ( expected ) ;
65
77
} ) ;
66
78
67
- it ( 'should not replace values in `url()`' , function ( ) {
79
+ it ( 'should not replace values in double quotes or single quotes' , function ( ) {
80
+ var options = {
81
+ propList : [ '*' ]
82
+ } ;
83
+ var rules = '.rule { content: \'16px\'; font-family: "16px"; font-size: 16px; }' ;
84
+ var expected = '.rule { content: \'16px\'; font-family: "16px"; font-size: 1rem; }' ;
85
+ var processed = postcss ( pxtorem ( options ) ) . process ( rules ) . css ;
86
+
87
+ expect ( processed ) . toBe ( expected ) ;
88
+ } ) ;
89
+
90
+ it ( 'should not replace values in `url()` - legacy' , function ( ) {
68
91
var options = {
69
92
propWhiteList : [ ]
70
93
} ;
@@ -74,6 +97,17 @@ describe('value parsing', function () {
74
97
75
98
expect ( processed ) . toBe ( expected ) ;
76
99
} ) ;
100
+
101
+ it ( 'should not replace values in `url()`' , function ( ) {
102
+ var options = {
103
+ propList : [ '*' ]
104
+ } ;
105
+ var rules = '.rule { background: url(16px.jpg); font-size: 16px; }' ;
106
+ var expected = '.rule { background: url(16px.jpg); font-size: 1rem; }' ;
107
+ var processed = postcss ( pxtorem ( options ) ) . process ( rules ) . css ;
108
+
109
+ expect ( processed ) . toBe ( expected ) ;
110
+ } ) ;
77
111
} ) ;
78
112
79
113
describe ( 'rootValue' , function ( ) {
@@ -134,7 +168,7 @@ describe('propWhiteList', function () {
134
168
expect ( processed ) . toBe ( expected ) ;
135
169
} ) ;
136
170
137
- it ( 'should only replace properties in the white list' , function ( ) {
171
+ it ( 'should only replace properties in the white list - legacy ' , function ( ) {
138
172
var expected = '.rule { font-size: 15px }' ;
139
173
var options = {
140
174
propWhiteList : [ 'font' ]
@@ -144,6 +178,39 @@ describe('propWhiteList', function () {
144
178
expect ( processed ) . toBe ( expected ) ;
145
179
} ) ;
146
180
181
+ it ( 'should only replace properties in the white list - legacy' , function ( ) {
182
+ var css = '.rule { margin: 16px; margin-left: 10px }' ;
183
+ var expected = '.rule { margin: 1rem; margin-left: 10px }' ;
184
+ var options = {
185
+ propWhiteList : [ 'margin' ]
186
+ } ;
187
+ var processed = postcss ( pxtorem ( options ) ) . process ( css ) . css ;
188
+
189
+ expect ( processed ) . toBe ( expected ) ;
190
+ } ) ;
191
+
192
+ it ( 'should only replace properties in the prop list' , function ( ) {
193
+ var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }' ;
194
+ var expected = '.rule { font-size: 1rem; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 1rem }' ;
195
+ var options = {
196
+ propWhiteList : [ '~font' , '^margin' , '!margin-left' , '$-right' , 'pad' ]
197
+ } ;
198
+ var processed = postcss ( pxtorem ( options ) ) . process ( css ) . css ;
199
+
200
+ expect ( processed ) . toBe ( expected ) ;
201
+ } ) ;
202
+
203
+ it ( 'should only replace properties in the prop list with wildcard' , function ( ) {
204
+ var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }' ;
205
+ var expected = '.rule { font-size: 16px; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 16px }' ;
206
+ var options = {
207
+ propWhiteList : [ '*' , '!margin-left' , '!~padding' , '!^font' ]
208
+ } ;
209
+ var processed = postcss ( pxtorem ( options ) ) . process ( css ) . css ;
210
+
211
+ expect ( processed ) . toBe ( expected ) ;
212
+ } ) ;
213
+
147
214
it ( 'should replace all properties when white list is empty' , function ( ) {
148
215
var rules = '.rule { margin: 16px; font-size: 15px }' ;
149
216
var expected = '.rule { margin: 1rem; font-size: 0.9375rem }' ;
@@ -251,3 +318,53 @@ describe('minPixelValue', function () {
251
318
expect ( processed ) . toBe ( expected ) ;
252
319
} ) ;
253
320
} ) ;
321
+
322
+ describe ( 'filter-prop-list' , function ( ) {
323
+ it ( 'should find "exact" matches from propList' , function ( ) {
324
+ var propList = [ 'font-size' , 'margin' , '!padding' , '~border' , '*' , '$y' , '!~font' ] ;
325
+ var expected = 'font-size,margin' ;
326
+ expect ( filterPropList . exact ( propList ) . join ( ) ) . toBe ( expected ) ;
327
+ } ) ;
328
+
329
+ it ( 'should find "contain" matches from propList and reduce to string' , function ( ) {
330
+ var propList = [ 'font-size' , '~margin' , '!padding' , '~border' , '*' , '$y' , '!~font' ] ;
331
+ var expected = 'margin,border' ;
332
+ expect ( filterPropList . contain ( propList ) . join ( ) ) . toBe ( expected ) ;
333
+ } ) ;
334
+
335
+ it ( 'should find "start" matches from propList and reduce to string' , function ( ) {
336
+ var propList = [ 'font-size' , '~margin' , '!padding' , '^border' , '*' , '$y' , '!~font' ] ;
337
+ var expected = 'border' ;
338
+ expect ( filterPropList . start ( propList ) . join ( ) ) . toBe ( expected ) ;
339
+ } ) ;
340
+
341
+ it ( 'should find "end" matches from propList and reduce to string' , function ( ) {
342
+ var propList = [ 'font-size' , '~margin' , '!padding' , '^border' , '*' , '$y' , '!~font' ] ;
343
+ var expected = 'y' ;
344
+ expect ( filterPropList . end ( propList ) . join ( ) ) . toBe ( expected ) ;
345
+ } ) ;
346
+
347
+ it ( 'should find "not" matches from propList and reduce to string' , function ( ) {
348
+ var propList = [ 'font-size' , '~margin' , '!padding' , '^border' , '*' , '$y' , '!~font' ] ;
349
+ var expected = 'padding' ;
350
+ expect ( filterPropList . not ( propList ) . join ( ) ) . toBe ( expected ) ;
351
+ } ) ;
352
+
353
+ it ( 'should find "not contain" matches from propList and reduce to string' , function ( ) {
354
+ var propList = [ 'font-size' , '~margin' , '!padding' , '!^border' , '*' , '$y' , '!~font' ] ;
355
+ var expected = 'font' ;
356
+ expect ( filterPropList . notContain ( propList ) . join ( ) ) . toBe ( expected ) ;
357
+ } ) ;
358
+
359
+ it ( 'should find "not start" matches from propList and reduce to string' , function ( ) {
360
+ var propList = [ 'font-size' , '~margin' , '!padding' , '!^border' , '*' , '$y' , '!~font' ] ;
361
+ var expected = 'border' ;
362
+ expect ( filterPropList . notStart ( propList ) . join ( ) ) . toBe ( expected ) ;
363
+ } ) ;
364
+
365
+ it ( 'should find "not end" matches from propList and reduce to string' , function ( ) {
366
+ var propList = [ 'font-size' , '~margin' , '!padding' , '!^border' , '*' , '!$y' , '!~font' ] ;
367
+ var expected = 'y' ;
368
+ expect ( filterPropList . notEnd ( propList ) . join ( ) ) . toBe ( expected ) ;
369
+ } ) ;
370
+ } ) ;
0 commit comments