Adds unit compatibility switches to disable length optimizations.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 13 Aug 2015 12:26:30 +0000 (13:26 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 13 Aug 2015 12:26:30 +0000 (13:26 +0100)
You can disable px -> (in|pc|pt) transforations with compatibility unit
switches. Tied to #625.

History.md
lib/selectors/simple.js
lib/utils/compatibility.js
test/selectors/simple-test.js
test/utils/compatibility-test.js

index 2d19044..7488a09 100644 (file)
@@ -1,6 +1,7 @@
 [3.4.0 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.3.7...master)
 ==================
 
+* Adds unit compatibility switches to disable length optimizations.
 * Unifies wrappers for simple & advanced optimizations.
 * Fixed issue [#599](https://github.com/jakubpawlowicz/clean-css/issues/599) - support for inlined source maps.
 * Fixed issue [#625](https://github.com/jakubpawlowicz/clean-css/issues/625) - adds length unit optimizations.
index 116f651..0737d6a 100644 (file)
@@ -170,7 +170,7 @@ function colorMininifier(_, value, compatibility) {
   return HexNameShortener.shorten(value);
 }
 
-function pixelLengthMinifier(_, value) {
+function pixelLengthMinifier(_, value, compatibility) {
   if (!WHOLE_PIXEL_VALUE.test(value))
     return value;
 
@@ -181,13 +181,13 @@ function pixelLengthMinifier(_, value) {
     if (intVal === 0)
       return match;
 
-    if (intVal * 3 % 4 === 0)
+    if (compatibility.units.pt && intVal * 3 % 4 === 0)
       newValue = intVal * 3 / 4 + 'pt';
 
-    if (intVal % 16 === 0)
+    if (compatibility.units.pc && intVal % 16 === 0)
       newValue = intVal / 16 + 'pc';
 
-    if (intVal % 96 === 0)
+    if (compatibility.units.in && intVal % 96 === 0)
       newValue = intVal / 96 + 'in';
 
     if (newValue)
@@ -313,7 +313,7 @@ function optimizeBody(properties, options) {
 
       value = whitespaceMinifier(name, value);
       value = precisionMinifier(name, value, options.precision);
-      value = pixelLengthMinifier(name, value);
+      value = pixelLengthMinifier(name, value, options.compatibility);
       value = timeUnitMinifier(name, value);
       value = zeroMinifier(name, value);
       if (options.compatibility.properties.zeroUnits) {
index 66892a9..a48e51b 100644 (file)
@@ -24,6 +24,9 @@ var DEFAULTS = {
     },
     units: {
       ch: true,
+      in: true,
+      pc: true,
+      pt: true,
       rem: true,
       vh: true,
       vm: true, // vm is vmin on IE9+ see https://developer.mozilla.org/en-US/docs/Web/CSS/length
@@ -55,6 +58,9 @@ var DEFAULTS = {
     },
     units: {
       ch: false,
+      in: true,
+      pc: true,
+      pt: true,
       rem: false,
       vh: false,
       vm: false,
@@ -86,6 +92,9 @@ var DEFAULTS = {
     },
     units: {
       ch: false,
+      in: true,
+      pc: true,
+      pt: true,
       rem: false,
       vh: false,
       vm: false,
index 75f63fc..d228e00 100644 (file)
@@ -772,4 +772,20 @@ vows.describe('simple optimizations')
       ]
     }, { compatibility: 'ie8' })
   )
+  .addBatch(
+    propertyContext('length units when turned off', {
+      'px to in': [
+        'div{left:480px}',
+        [['left', '480px']]
+      ],
+      'px to pc': [
+        'div{left:32px}',
+        [['left', '32px']]
+      ],
+      'px to pt': [
+        'div{left:120px}',
+        [['left', '120px']]
+      ]
+    }, { compatibility: { units: { in: false, pc: false, pt: false } } })
+  )
   .export(module);
index 2b98d59..a00c32a 100644 (file)
@@ -23,6 +23,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isTrue(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isTrue(options.units.rem);
         assert.isTrue(options.units.vh);
         assert.isTrue(options.units.vm);
@@ -57,6 +60,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isTrue(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isFalse(options.units.rem);
         assert.isTrue(options.units.vh);
         assert.isTrue(options.units.vm);
@@ -86,6 +92,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isFalse(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isFalse(options.units.rem);
         assert.isFalse(options.units.vh);
         assert.isFalse(options.units.vm);
@@ -113,6 +122,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isTrue(options.selectors.ie7Hack);
         assert.isFalse(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isFalse(options.units.rem);
         assert.isFalse(options.units.vh);
         assert.isFalse(options.units.vm);
@@ -150,6 +162,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isFalse(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isFalse(options.units.rem);
         assert.isFalse(options.units.vh);
         assert.isFalse(options.units.vm);
@@ -177,6 +192,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isTrue(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isTrue(options.units.rem);
         assert.isTrue(options.units.vh);
         assert.isTrue(options.units.vm);
@@ -204,6 +222,9 @@ vows.describe(Compatibility)
         assert.isFalse(options.selectors.adjacentSpace);
         assert.isFalse(options.selectors.ie7Hack);
         assert.isTrue(options.units.ch);
+        assert.isTrue(options.units.in);
+        assert.isTrue(options.units.pc);
+        assert.isTrue(options.units.pt);
         assert.isFalse(options.units.rem);
         assert.isTrue(options.units.vh);
         assert.isTrue(options.units.vm);