Fixes #446 - adds fuzzy matching to `list-style`.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 12 Apr 2015 18:33:55 +0000 (19:33 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 12 Apr 2015 18:33:55 +0000 (19:33 +0100)
The order is not important so we'll try to match components
searching for compatible values.

History.md
lib/properties/break-up.js
test/properties/break-up-test.js
test/properties/optimizer-test.js
test/properties/restore-test.js

index 02fd0b1..69b496f 100644 (file)
@@ -9,6 +9,7 @@
 * Fixed issue [#397](https://github.com/jakubpawlowicz/clean-css/issues/397) - support for source map sources.
 * Fixed issue [#399](https://github.com/jakubpawlowicz/clean-css/issues/399) - support compacting with source maps.
 * Fixed issue [#429](https://github.com/jakubpawlowicz/clean-css/issues/429) - unifies data tokenization.
+* Fixed issue [#446](https://github.com/jakubpawlowicz/clean-css/issues/446) - `list-style` fuzzy matching.
 * Fixed issue [#468](https://github.com/jakubpawlowicz/clean-css/issues/468) - bumps `source-map` to 0.4.x.
 * Fixed issue [#480](https://github.com/jakubpawlowicz/clean-css/issues/480) - extracting uppercase property names.
 * Fixed issue [#500](https://github.com/jakubpawlowicz/clean-css/issues/500) - merging duplicate adjacent properties.
index 9a28029..3d8abcb 100644 (file)
@@ -229,15 +229,31 @@ function listStyle(property, compactable, validator) {
     return components;
   }
 
-  var values = property.value;
+  var values = property.value.slice(0);
+  var total = values.length;
   var index = 0;
 
-  if (index < values.length && validator.isValidListStyleType(values[index][0]))
-    type.value = [values[index++]];
-  if (index < values.length && validator.isValidListStylePosition(values[index][0]))
-    position.value = [values[index++]];
-  if (index < values.length)
-    image.value = [values[index]];
+  // `image` first...
+  for (index = 0, total = values.length; index < total; index++) {
+    if (validator.isValidUrl(values[index][0]) || values[index][0] == '0') {
+      image.value = [values[index]];
+      values.splice(index, 1);
+      break;
+    }
+  }
+
+  // ... then `type`...
+  for (index = 0, total = values.length; index < total; index++) {
+    if (validator.isValidListStyleType(values[index][0])) {
+      type.value = [values[index]];
+      values.splice(index, 1);
+      break;
+    }
+  }
+
+  // ... and what's left is a `position`
+  if (values.length > 0 && validator.isValidListStylePosition(values[0][0]))
+    position.value = [values[0]];
 
   return components;
 }
index 0914470..710df1d 100644 (file)
@@ -522,6 +522,26 @@ vows.describe(breakUp)
           assert.equal(components[2].name, 'list-style-image');
           assert.deepEqual(components[2].value, [['none']]);
         }
+      },
+      'fuzzy matching': {
+        'topic': function () {
+          return _breakUp([[['list-style'], ['__ESCAPED_URL_CLEAN_CSS0__'], ['outside'], ['none']]]);
+        },
+        'has 3 components': function (components) {
+          assert.lengthOf(components, 3);
+        },
+        'has list-style-type': function (components) {
+          assert.equal(components[0].name, 'list-style-type');
+          assert.deepEqual(components[0].value, [['none']]);
+        },
+        'has list-style-position': function (components) {
+          assert.equal(components[1].name, 'list-style-position');
+          assert.deepEqual(components[1].value, [['outside']]);
+        },
+        'has list-style-image': function (components) {
+          assert.equal(components[2].name, 'list-style-image');
+          assert.deepEqual(components[2].value, [['__ESCAPED_URL_CLEAN_CSS0__']]);
+        }
       }
     },
     'multiple values': {
index 4fca81e..bc10216 100644 (file)
@@ -163,6 +163,16 @@ vows.describe(optimize)
       }
     }
   })
+  .addBatch({
+    'list-style fuzzy matching': {
+      'topic': 'p{list-style:inside none}',
+      'into': function (topic) {
+        assert.deepEqual(_optimize(topic, false, true), [
+          [['list-style', false , false], ['none'], ['inside']]
+        ]);
+      }
+    }
+  })
   .addBatch({
     'ie hacks - normal before hack': {
       'topic': 'p{color:red;display:none;color:#fff\\9}',
index 951b9f7..087534d 100644 (file)
@@ -259,10 +259,10 @@ vows.describe(restore)
       },
       'list with all values': {
         'topic': function () {
-          return _restore(_breakUp([['list-style'], ['circle'], ['inside'], ['url()']]));
+          return _restore(_breakUp([['list-style'], ['circle'], ['inside'], ['__ESCAPED_URL_CLEAN_CSS1__']]));
         },
         'gives right value back': function (restoredValue) {
-          assert.deepEqual(restoredValue, [['circle'], ['inside'], ['url()']]);
+          assert.deepEqual(restoredValue, [['circle'], ['inside'], ['__ESCAPED_URL_CLEAN_CSS1__']]);
         }
       },
       'list with some defaults': {