From: Jakub Pawlowicz Date: Tue, 20 Dec 2016 16:10:47 +0000 (+0100) Subject: Fixes #657 - adds property name validation. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=27a1097c3378a4158500ee9c0f4d966f0faa0b31;p=clean-css.git Fixes #657 - adds property name validation. It's a bit relaxed matching but too strict one would do too much harm with many false-positives. Why: * To prevent processing errors further down the optimization pipeline. --- diff --git a/lib/optimizer/basic.js b/lib/optimizer/basic.js index 17754918..bae8b651 100644 --- a/lib/optimizer/basic.js +++ b/lib/optimizer/basic.js @@ -28,6 +28,7 @@ var FONT_NAME_WEIGHTS_WITHOUT_NORMAL = ['bold', 'bolder', 'lighter']; var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/; var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/; +var PROPERTY_NAME_PATTERN = /^(?:\-chrome\-|\-[\w\-]+\w|\w[\w\-]+\w|\-\-\S+)$/; var IMPORT_PREFIX_PATTERN = /^@import/i; var QUOTED_PATTERN = /^('.*'|".*")$/; var QUOTED_BUT_SAFE_PATTERN = /^['"][a-zA-Z][a-zA-Z\d\-_]+['"]$/; @@ -345,6 +346,12 @@ function optimizeBody(properties, context) { property = _properties[i]; name = property.name; + if (!PROPERTY_NAME_PATTERN.test(name)) { + propertyToken = property.all[property.position]; + context.warnings.push('Invalid property name \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.'); + property.unused = true; + } + if (property.value.length === 0) { propertyToken = property.all[property.position]; context.warnings.push('Empty property \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.'); diff --git a/test/module-test.js b/test/module-test.js index 5f256b19..4460c2b7 100644 --- a/test/module-test.js +++ b/test/module-test.js @@ -131,6 +131,21 @@ vows.describe('module tests').addBatch({ assert.equal(minified.warnings[1], 'Invalid selector \'color:#535353}p\' at 1:16. Ignoring.'); } }, + 'warning on invalid property': { + 'topic': function () { + return new CleanCSS().minify('a{-webkit-:0px}'); + }, + 'should minify correctly': function (error, minified) { + assert.isEmpty(minified.styles); + }, + 'should raise no errors': function (error, minified) { + assert.isEmpty(minified.errors); + }, + 'should raise one warning': function (error, minified) { + assert.lengthOf(minified.warnings, 1); + assert.equal(minified.warnings[0], 'Invalid property name \'-webkit-\' at 1:2. Ignoring.'); + } + }, 'warnings on empty properties': { 'topic': function () { return new CleanCSS().minify('a{color:}'); diff --git a/test/optimizer/basic-test.js b/test/optimizer/basic-test.js index 2fb61eb3..3907cd60 100644 --- a/test/optimizer/basic-test.js +++ b/test/optimizer/basic-test.js @@ -911,4 +911,24 @@ vows.describe('simple optimizations') ] }, { advanced: false, compatibility: { properties: { shorterLengthUnits: true }, units: { in: false, pt: false } } }) ) + .addBatch( + optimizerContext('property name validation', { + 'trimmed': [ + 'a{-webkit-:0 0 2px red}', + '' + ], + 'with incorrect characters': [ + 'a{color+other:red}', + '' + ], + 'for chrome only': [ + 'a{-chrome-:only(;color:red;)}', + 'a{-chrome-:only(;color:red;)}' + ], + 'custom vendor prefix': [ + 'a{-custom-color:red}', + 'a{-custom-color:red}' + ] + }, { advanced: false }) + ) .export(module);