Fixes #640 - URI processing regression.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 9 Aug 2015 09:55:15 +0000 (10:55 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 9 Aug 2015 10:07:55 +0000 (11:07 +0100)
The fix to #633 included a regression in URI processing as the
algorithm introduced should only be applied to data URIs.

History.md
lib/urls/reduce.js
test/text/urls-processor-test.js

index 2c282d1..9bcfc6b 100644 (file)
@@ -4,6 +4,11 @@
 * Unifies wrappers for simple & advanced optimizations.
 * Fixed issue [#599](https://github.com/jakubpawlowicz/clean-css/issues/599) - support for inlined source maps.
 
+[3.3.9 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.3.8...3.3)
+==================
+
+* Fixed issue [#640](https://github.com/jakubpawlowicz/clean-css/issues/640) - URI processing regression.
+
 [3.3.8 / 2015-08-06](https://github.com/jakubpawlowicz/clean-css/compare/v3.3.7...v3.3.8)
 ==================
 
index 1f7d7fd..daf4ee4 100644 (file)
@@ -1,6 +1,7 @@
 var URL_PREFIX = 'url(';
 var UPPERCASE_URL_PREFIX = 'URL(';
 var URL_SUFFIX = ')';
+var DATA_URI_PREFIX = 'data:';
 
 var IMPORT_URL_PREFIX = '@import';
 var UPPERCASE_IMPORT_URL_PREFIX = '@IMPORT';
@@ -10,6 +11,7 @@ function byUrl(data, context, callback) {
   var nextStartUpperCase = 0;
   var nextEnd = 0;
   var nextEndAhead = 0;
+  var isDataURI = false;
   var cursor = 0;
   var tempData = [];
   var hasUppercaseUrl = data.indexOf(UPPERCASE_URL_PREFIX) > -1;
@@ -23,22 +25,27 @@ function byUrl(data, context, callback) {
     if (nextStart == -1 && nextStartUpperCase > -1)
       nextStart = nextStartUpperCase;
 
+
     if (data[nextStart + URL_PREFIX.length] == '"') {
       nextEnd = data.indexOf('"', nextStart + URL_PREFIX.length + 1);
     } else if (data[nextStart + URL_PREFIX.length] == '\'') {
       nextEnd = data.indexOf('\'', nextStart + URL_PREFIX.length + 1);
     } else {
+      isDataURI = data.substring(nextStart + URL_PREFIX.length).trim().indexOf(DATA_URI_PREFIX) === 0;
       nextEnd = data.indexOf(URL_SUFFIX, nextStart);
 
-      while (true) {
-        nextEndAhead = data.indexOf(URL_SUFFIX, nextEnd + 1);
-        // if it has whitespace then we should be out of URL, otherwise keep iterating
-        // if it has not but content is not escaped, it has to be quoted so it will be captured
-        // by either of two clauses above
-        if (nextEndAhead == -1 || /\s/.test(data.substring(nextEnd, nextEndAhead)))
-          break;
-
-        nextEnd = nextEndAhead;
+      if (isDataURI) {
+        // this is a fuzzy matching logic for unqoted data URIs
+        while (true) {
+          nextEndAhead = data.indexOf(URL_SUFFIX, nextEnd + 1);
+          // if it has whitespace then we should be out of URL, otherwise keep iterating
+          // if it has not but content is not escaped, it has to be quoted so it will be captured
+          // by either of two clauses above
+          if (nextEndAhead == -1 || /\s/.test(data.substring(nextEnd, nextEndAhead)))
+            break;
+
+          nextEnd = nextEndAhead;
+        }
       }
     }
 
index f2a75aa..ff66f76 100644 (file)
@@ -80,6 +80,11 @@ vows.describe(UrlsProcessor)
         'div{background:url("some/).png") repeat}',
         'div{background:__ESCAPED_URL_CLEAN_CSS0__ repeat}',
         'div{background:url("some/).png") repeat}'
+      ],
+      'followed by attribute matcher selector': [
+        'a{background:url(url)}div:not([test]){color:red}',
+        'a{background:__ESCAPED_URL_CLEAN_CSS0__}div:not([test]){color:red}',
+        'a{background:url(url)}div:not([test]){color:red}'
       ]
     })
   )