99var postcss = require ( 'postcss' ) ;
1010var pxtorem = require ( '..' ) ;
1111var basicCSS = '.rule { font-size: 15px }' ;
12+ var filterPropList = require ( '../lib/filter-prop-list' ) ;
1213
1314describe ( 'pxtorem' , function ( ) {
1415 it ( 'should work on the readme example' , function ( ) {
@@ -33,7 +34,7 @@ describe('pxtorem', function () {
3334 expect ( processed ) . toBe ( expected ) ;
3435 } ) ;
3536
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 ( ) {
3738 var rules = '.rule { margin: 0.5rem .5px -0.2px -.2em }' ;
3839 var expected = '.rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }' ;
3940 var options = {
@@ -44,6 +45,17 @@ describe('pxtorem', function () {
4445 expect ( processed ) . toBe ( expected ) ;
4546 } ) ;
4647
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+
4759 it ( 'should not add properties that already exist' , function ( ) {
4860 var expected = '.rule { font-size: 16px; font-size: 1rem; }' ;
4961 var processed = postcss ( pxtorem ( ) ) . process ( expected ) . css ;
@@ -53,7 +65,7 @@ describe('pxtorem', function () {
5365} ) ;
5466
5567describe ( '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 ( ) {
5769 var options = {
5870 propWhiteList : [ ]
5971 } ;
@@ -64,7 +76,18 @@ describe('value parsing', function () {
6476 expect ( processed ) . toBe ( expected ) ;
6577 } ) ;
6678
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 ( ) {
6891 var options = {
6992 propWhiteList : [ ]
7093 } ;
@@ -74,6 +97,17 @@ describe('value parsing', function () {
7497
7598 expect ( processed ) . toBe ( expected ) ;
7699 } ) ;
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+ } ) ;
77111} ) ;
78112
79113describe ( 'rootValue' , function ( ) {
@@ -134,7 +168,7 @@ describe('propWhiteList', function () {
134168 expect ( processed ) . toBe ( expected ) ;
135169 } ) ;
136170
137- it ( 'should only replace properties in the white list' , function ( ) {
171+ it ( 'should only replace properties in the white list - legacy ' , function ( ) {
138172 var expected = '.rule { font-size: 15px }' ;
139173 var options = {
140174 propWhiteList : [ 'font' ]
@@ -144,6 +178,39 @@ describe('propWhiteList', function () {
144178 expect ( processed ) . toBe ( expected ) ;
145179 } ) ;
146180
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+
147214 it ( 'should replace all properties when white list is empty' , function ( ) {
148215 var rules = '.rule { margin: 16px; font-size: 15px }' ;
149216 var expected = '.rule { margin: 1rem; font-size: 0.9375rem }' ;
@@ -251,3 +318,53 @@ describe('minPixelValue', function () {
251318 expect ( processed ) . toBe ( expected ) ;
252319 } ) ;
253320} ) ;
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