From e9a1d985c1eb64e5c4139b42bf3edd9a34996856 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Fri, 30 Dec 2016 12:49:02 +0100 Subject: [PATCH] Fixes #739 - error when a closing brace is missing. Why: * When such content gets parsed into a block we should ignore the whole declaration as browsers do. --- History.md | 1 + lib/optimizer/basic.js | 9 ++++- test/optimizer/basic-test.js | 4 +++ test/tokenizer/tokenize-test.js | 59 +++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index b882244f..2ddfe31f 100644 --- a/History.md +++ b/History.md @@ -15,6 +15,7 @@ * Fixed issue [#685](https://github.com/jakubpawlowicz/clean-css/issues/685) - adds lowercasing hex colors optimization. * Fixed issue [#686](https://github.com/jakubpawlowicz/clean-css/issues/686) - adds rounding precision for all units. * Fixed issue [#703](https://github.com/jakubpawlowicz/clean-css/issues/703) - changes default IE compatibility to 10+. +* Fixed issue [#739](https://github.com/jakubpawlowicz/clean-css/issues/739) - error when a closing brace is missing. * Fixed issue [#756](https://github.com/jakubpawlowicz/clean-css/issues/756) - adds disabling font-weight optimizations. * Fixed issue [#758](https://github.com/jakubpawlowicz/clean-css/issues/758) - ignores rules with empty selector. * Fixed issue [#767](https://github.com/jakubpawlowicz/clean-css/issues/767) - disables remote `@import` inlining by default. diff --git a/lib/optimizer/basic.js b/lib/optimizer/basic.js index 96ec441d..f06bbec8 100644 --- a/lib/optimizer/basic.js +++ b/lib/optimizer/basic.js @@ -353,7 +353,7 @@ function removeQuotes(name, value) { function optimizeBody(properties, context) { var options = context.options; - var property, name, value; + var property, name, type, value; var valueIsUrl; var propertyToken; var _properties = wrapForOptimizing(properties); @@ -393,9 +393,16 @@ function optimizeBody(properties, context) { } for (var j = 0, m = property.value.length; j < m; j++) { + type = property.value[j][0]; value = property.value[j][1]; valueIsUrl = isUrl(value); + if (type == Token.PROPERTY_BLOCK) { + property.unused = true; + context.warnings.push('Invalid value token at ' + formatPosition(value[0][1][2][0]) + '. Ignoring.'); + break; + } + if (valueIsUrl && !context.validator.isValidUrl(value)) { property.unused = true; context.warnings.push('Broken URL \'' + value + '\' at ' + formatPosition(property.value[j][2][0]) + '. Ignoring.'); diff --git a/test/optimizer/basic-test.js b/test/optimizer/basic-test.js index f4318e25..6c95da4b 100644 --- a/test/optimizer/basic-test.js +++ b/test/optimizer/basic-test.js @@ -116,6 +116,10 @@ vows.describe('simple optimizations') 'invalid characters #3 - relation at the beginning': [ '>.funky{background:red}', '' + ], + 'missing semicolon and brace in the middle': [ + 'body{color:red a{color:blue;}', + '' ] }, { advanced: false }) ) diff --git a/test/tokenizer/tokenize-test.js b/test/tokenizer/tokenize-test.js index 292a246d..152926a4 100644 --- a/test/tokenizer/tokenize-test.js +++ b/test/tokenizer/tokenize-test.js @@ -3412,6 +3412,65 @@ vows.describe(tokenize) ] ] ], + 'missing end brace and semicolon in the middle': [ + 'body{color:red a{color:blue;}', + [ + [ + 'rule', + [ + [ + 'rule-scope', + 'body', + [ + [1, 0, undefined] + ] + ] + ], + [ + [ + 'property', + [ + 'property-name', + 'color', + [ + [1, 5, undefined] + ] + ], + [ + 'property-value', + 'red', + [ + [1, 11, undefined] + ] + ], + [ + 'property-block', + [ + [ + 'property', + [ + 'property-name', + 'acolor', + [ + [1, 15, undefined] + ] + ], + [ + 'property-value', + 'blue', + [ + [1, 23, undefined] + ] + + ] + ] + ] + ] + ] + ] + ] + ] + ], 'extra end brace in the middle': [ 'body{color:red}}a{color:blue;}', [ -- 2.34.1