From e066220fd428faab807ca46d85e345119a30fe78 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 27 Jul 2014 10:51:42 +0100 Subject: [PATCH] Improves performance of advanced mode validators. --- History.md | 1 + lib/properties/validator.js | 46 ++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/History.md b/History.md index ca962938..b1f818d3 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ [2.2.10 / 2014-xx-xx](https://github.com/GoalSmashers/clean-css/compare/v2.2.9...v2.2.10) ================== +* Improved performance of advanced mode validators. * Fixed issue [#323](https://github.com/GoalSmashers/clean-css/issues/323) - stripping variable references. * Fixed issue [#325](https://github.com/GoalSmashers/clean-css/issues/325) - removing invalid @charset declarations. diff --git a/lib/properties/validator.js b/lib/properties/validator.js index 88a1ff59..71149e02 100644 --- a/lib/properties/validator.js +++ b/lib/properties/validator.js @@ -11,6 +11,13 @@ module.exports = (function () { var cssFunctionAnyRegexStr = '(' + cssVariableRegexStr + '|' + cssFunctionNoVendorRegexStr + '|' + cssFunctionVendorRegexStr + ')'; var cssUnitAnyRegexStr = '(none|' + widthKeywords.join('|') + '|' + cssUnitRegexStr + '|' + cssVariableRegexStr + '|' + cssFunctionNoVendorRegexStr + '|' + cssFunctionVendorRegexStr + ')'; + var cssFunctionNoVendorRegex = new RegExp('^' + cssFunctionNoVendorRegexStr + '$', 'i'); + var cssFunctionVendorRegex = new RegExp('^' + cssFunctionVendorRegexStr + '$', 'i'); + var cssVariableRegex = new RegExp('^' + cssVariableRegexStr + '$', 'i'); + var cssFunctionAnyRegex = new RegExp('^' + cssFunctionAnyRegexStr + '$', 'i'); + var cssUnitRegex = new RegExp('^' + cssUnitRegexStr + '$', 'i'); + var cssUnitAnyRegex = new RegExp('^' + cssUnitAnyRegexStr + '$', 'i'); + var backgroundRepeatKeywords = ['repeat', 'no-repeat', 'repeat-x', 'repeat-y', 'inherit']; var backgroundAttachmentKeywords = ['inherit', 'scroll', 'fixed', 'local']; var backgroundPositionKeywords = ['center', 'top', 'bottom', 'left', 'right']; @@ -35,7 +42,7 @@ module.exports = (function () { return s !== 'auto' && (s === 'transparent' || s === 'inherit' || /^[a-zA-Z]+$/.test(s)); }, isValidVariable: function(s) { - return new RegExp('^' + cssVariableRegexStr + '$', 'gi').test(s); + return cssVariableRegex.test(s); }, isValidColor: function (s) { return validator.isValidNamedColor(s) || validator.isValidHexColor(s) || validator.isValidRgbaColor(s) || validator.isValidHslaColor(s) || validator.isValidVariable(s); @@ -45,19 +52,19 @@ module.exports = (function () { return s.indexOf('__ESCAPED_URL_CLEAN_CSS') === 0; }, isValidUnit: function (s) { - return new RegExp('^' + cssUnitAnyRegexStr + '$', 'gi').test(s); + return cssUnitAnyRegex.test(s); }, isValidUnitWithoutFunction: function (s) { - return new RegExp('^' + cssUnitRegexStr + '$', 'gi').test(s); + return cssUnitRegex.test(s); }, isValidFunctionWithoutVendorPrefix: function (s) { - return new RegExp('^' + cssFunctionNoVendorRegexStr + '$', 'gi').test(s); + return cssFunctionNoVendorRegex.test(s); }, isValidFunctionWithVendorPrefix: function (s) { - return new RegExp('^' + cssFunctionVendorRegexStr + '$', 'gi').test(s); + return cssFunctionVendorRegex.test(s); }, isValidFunction: function (s) { - return new RegExp('^' + cssFunctionAnyRegexStr + '$', 'gi').test(s); + return cssFunctionAnyRegex.test(s); }, isValidBackgroundRepeat: function (s) { return backgroundRepeatKeywords.indexOf(s) >= 0 || validator.isValidVariable(s); @@ -69,17 +76,23 @@ module.exports = (function () { if (backgroundPositionKeywords.indexOf(s) >= 0) return true; - return new RegExp('^' + cssUnitRegexStr + '$', 'gi').test(s) || validator.isValidVariable(s); + return cssUnitRegex.test(s) || validator.isValidVariable(s); }, isValidBackgroundPosition: function (s) { if (s === 'inherit') return true; - return s.split(' ').filter(function (p) { - return p !== ''; - }).every(function(p) { - return validator.isValidBackgroundPositionPart(p) || validator.isValidVariable(s); - }); + var parts = s.split(' '); + for (var i = 0, l = parts.length; i < l; i++) { + if (parts[i] === '') + continue; + if (validator.isValidBackgroundPositionPart(parts[i]) || validator.isValidVariable(parts[i])) + continue; + + return false; + } + + return true; }, isValidListStyleType: function (s) { return listStyleTypeKeywords.indexOf(s) >= 0 || validator.isValidVariable(s); @@ -100,9 +113,9 @@ module.exports = (function () { return /^-([A-Za-z0-9]|-)*$/gi.test(s); }, areSameFunction: function (a, b) { - if (!validator.isValidFunction(a) || !validator.isValidFunction(b)) { + if (!validator.isValidFunction(a) || !validator.isValidFunction(b)) return false; - } + var f1name = a.substring(0, a.indexOf('(')); var f2name = b.substring(0, b.indexOf('(')); @@ -110,10 +123,5 @@ module.exports = (function () { } }; - validator.cssUnitRegexStr = cssUnitRegexStr; - validator.cssFunctionNoVendorRegexStr = cssFunctionNoVendorRegexStr; - validator.cssFunctionVendorRegexStr = cssFunctionVendorRegexStr; - validator.cssFunctionAnyRegexStr = cssFunctionAnyRegexStr; - return validator; })(); -- 2.34.1