From 8b3d306be9f199882cc582c83709e2f7da94e718 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Fri, 19 Dec 2014 20:33:51 +0000 Subject: [PATCH] Fixes #411 - stringifying properties with important comments. --- History.md | 5 +++++ lib/selectors/source-map-stringifier.js | 11 ++++++----- lib/selectors/stringifier.js | 10 ++++++---- test/integration-test.js | 12 ++++++++---- test/source-map-test.js | 6 ++++++ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/History.md b/History.md index 6cc98b9e..5d309eb1 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +[3.0.1 / 2014-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.0...v3.0.1) +================== + +* Fixed issue [#411](https://github.com/GoalSmashers/clean-css/issues/411) - properties and important comments. + [3.0.0 / 2014-12-18](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.22...v3.0.0) ================== diff --git a/lib/selectors/source-map-stringifier.js b/lib/selectors/source-map-stringifier.js index f9a7d3ed..16ca2d1a 100644 --- a/lib/selectors/source-map-stringifier.js +++ b/lib/selectors/source-map-stringifier.js @@ -38,20 +38,21 @@ Rebuilder.prototype.relativePathResolver = function (sourcePath, sourceRelativeT }; Rebuilder.prototype.rebuildValue = function (list, separator) { - var lastEscaped = false; + var escaped = 0; for (var i = 0, l = list.length; i < l; i++) { var el = list[i]; if (el.value.indexOf('__ESCAPED_') === 0) { - if (!lastEscaped) - this.output.pop(); this.store(el); - lastEscaped = true; + escaped++; + + if (i === l - 1 && escaped > 0) + this.output.splice(this.output.length - escaped - 1, 1); } else { this.store(el); this.store(i < l - 1 ? separator : ''); - lastEscaped = false; + escaped = 0; } } }; diff --git a/lib/selectors/stringifier.js b/lib/selectors/stringifier.js index baa0010d..1d9bb461 100644 --- a/lib/selectors/stringifier.js +++ b/lib/selectors/stringifier.js @@ -7,17 +7,19 @@ function Stringifier(options, restoreCallback) { function valueRebuilder(list, separator) { var merged = ''; - var lastEscaped = false; for (var i = 0, l = list.length; i < l; i++) { var el = list[i]; if (el.value.indexOf('__ESCAPED_') === 0) { - merged = (lastEscaped ? merged : merged.substring(0, merged.length - 1)) + el.value; - lastEscaped = true; + merged += el.value; + + if (i === l - 1) { + var lastSemicolonAt = merged.lastIndexOf(';'); + merged = merged.substring(0, lastSemicolonAt) + merged.substring(lastSemicolonAt + 1); + } } else { merged += list[i].value + (i < l - 1 ? separator : ''); - lastEscaped = false; } } diff --git a/test/integration-test.js b/test/integration-test.js index d7fcbae8..19c9c459 100644 --- a/test/integration-test.js +++ b/test/integration-test.js @@ -297,12 +297,16 @@ vows.describe('integration tests').addBatch({ '' ], 'important after value': [ - 'div{color:red!important;/*!comment*/}', - 'div{color:red!important/*!comment*/}' + 'div{color:red;/*!comment*/}', + 'div{color:red/*!comment*/}' + ], + 'important between values': [ + 'div{color:red;/*!comment*/display:block}', + 'div{color:red;/*!comment*/display:block}' ], 'two important after value': [ - 'div{color:red!important;/*!1*//*!2*/}', - 'div{color:red!important/*!1*//*!2*/}' + 'div{color:red;/*!1*//*!2*/}', + 'div{color:red/*!1*//*!2*/}' ] }), 'escaping': cssContext({ diff --git a/test/source-map-test.js b/test/source-map-test.js index 5359d54f..9b84cb8d 100644 --- a/test/source-map-test.js +++ b/test/source-map-test.js @@ -670,6 +670,12 @@ vows.describe('source-map') assert.equal(minified.styles, 'div{color:red!important/*!comment*/}'); } }, + 'important comment between properties': { + 'topic': new CleanCSS({ sourceMap: true }).minify('div { color: #f00 !important; /*!comment*/; display: block }'), + 'has right output': function (errors, minified) { + assert.equal(minified.styles, 'div{color:red!important;/*!comment*/display:block}'); + } + }, 'important comments after a property': { 'topic': new CleanCSS({ sourceMap: true }).minify('div { color: #f00 !important; /*!1*//*!2*/ }'), 'has right output': function (errors, minified) { -- 2.34.1