Improves performance of advanced mode validators.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 27 Jul 2014 09:51:42 +0000 (10:51 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 27 Jul 2014 13:31:20 +0000 (14:31 +0100)
History.md
lib/properties/validator.js

index ca96293..b1f818d 100644 (file)
@@ -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.
 
index 88a1ff5..71149e0 100644 (file)
@@ -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;
 })();