From: Jakub Pawlowicz Date: Sun, 12 Apr 2015 18:33:55 +0000 (+0100) Subject: Fixes #446 - adds fuzzy matching to `list-style`. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=8a4edfd392ffd2d7c99c1f6a971a6cf4786e4df4;p=clean-css.git Fixes #446 - adds fuzzy matching to `list-style`. The order is not important so we'll try to match components searching for compatible values. --- diff --git a/History.md b/History.md index 02fd0b10..69b496f6 100644 --- a/History.md +++ b/History.md @@ -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. diff --git a/lib/properties/break-up.js b/lib/properties/break-up.js index 9a280290..3d8abcbb 100644 --- a/lib/properties/break-up.js +++ b/lib/properties/break-up.js @@ -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; } diff --git a/test/properties/break-up-test.js b/test/properties/break-up-test.js index 09144702..710df1df 100644 --- a/test/properties/break-up-test.js +++ b/test/properties/break-up-test.js @@ -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': { diff --git a/test/properties/optimizer-test.js b/test/properties/optimizer-test.js index 4fca81ea..bc102161 100644 --- a/test/properties/optimizer-test.js +++ b/test/properties/optimizer-test.js @@ -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}', diff --git a/test/properties/restore-test.js b/test/properties/restore-test.js index 951b9f7c..087534d5 100644 --- a/test/properties/restore-test.js +++ b/test/properties/restore-test.js @@ -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': {