Fixes #787 - regression in data URI processing.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 15 Jun 2016 06:23:29 +0000 (08:23 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 15 Jun 2016 06:35:13 +0000 (08:35 +0200)
Yet another regression so we revert to the original order of
processing `url()` values.

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

index 9d55feb..f21e0e0 100644 (file)
@@ -3,6 +3,11 @@
 
 * Requires Node.js 4.0+ to run.
 
+[3.4.18 / 2016-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.17...3.4)
+==================
+
+* Fixed issue [#787](https://github.com/jakubpawlowicz/clean-css/issues/787) - regression in processing data URIs.
+
 [3.4.17 / 2016-06-04](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.16...v3.4.17)
 ==================
 
index b66e5cd..35ad555 100644 (file)
@@ -3,6 +3,8 @@ var split = require('../utils/split');
 var URL_PREFIX = 'url(';
 var UPPERCASE_URL_PREFIX = 'URL(';
 var URL_SUFFIX = ')';
+var SINGLE_QUOTE_URL_SUFFIX = '\')';
+var DOUBLE_QUOTE_URL_SUFFIX = '")';
 
 var DATA_URI_PREFIX_PATTERN = /^\s*['"]?\s*data:/;
 var DATA_URI_TRAILER_PATTERN = /[\s\};,\/!]/;
@@ -31,21 +33,21 @@ function byUrl(data, context, callback) {
     if (nextStart == -1 && nextStartUpperCase > -1)
       nextStart = nextStartUpperCase;
 
-    isDataURI = DATA_URI_PREFIX_PATTERN.test(data.substring(nextStart + URL_PREFIX.length));
+    if (data[nextStart + URL_PREFIX.length] == '"') {
+      nextEnd = data.indexOf(DOUBLE_QUOTE_URL_SUFFIX, nextStart);
+    } else if (data[nextStart + URL_PREFIX.length] == '\'') {
+      nextEnd = data.indexOf(SINGLE_QUOTE_URL_SUFFIX, nextStart);
+    } else {
+      isDataURI = DATA_URI_PREFIX_PATTERN.test(data.substring(nextStart + URL_PREFIX.length));
 
-    if (isDataURI) {
-      firstMatch = split(data.substring(nextStart), DATA_URI_TRAILER_PATTERN, false, '(', ')', true).pop();
+      if (isDataURI) {
+        firstMatch = split(data.substring(nextStart), DATA_URI_TRAILER_PATTERN, false, '(', ')', true).pop();
 
-      if (firstMatch && firstMatch[firstMatch.length - 1] == URL_SUFFIX) {
-        nextEnd = nextStart + firstMatch.length - URL_SUFFIX.length;
-      } else {
-        nextEnd = -1;
-      }
-    } else {
-      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);
+        if (firstMatch && firstMatch[firstMatch.length - 1] == URL_SUFFIX) {
+          nextEnd = nextStart + firstMatch.length - URL_SUFFIX.length;
+        } else {
+          nextEnd = -1;
+        }
       } else {
         nextEnd = data.indexOf(URL_SUFFIX, nextStart);
       }
index 54111df..daa74e3 100644 (file)
@@ -100,6 +100,11 @@ vows.describe(UrlsProcessor)
         '.a{cursor:url("data:application/octet-stream;base64,A...rotate(30 60,60)...="),move!important}.b{cursor:url("data:application/octet-stream;base64,A...rotate(30 60,60)...=")}',
         '.a{cursor:__ESCAPED_URL_CLEAN_CSS0__,move!important}.b{cursor:__ESCAPED_URL_CLEAN_CSS0__}',
         '.a{cursor:url("data:application/octet-stream;base64,A...rotate(30 60,60)...="),move!important}.b{cursor:url("data:application/octet-stream;base64,A...rotate(30 60,60)...=")}',
+      ],
+      'quoted data URI with unbalanced closing brackets': [
+        '.a{cursor:url("data:application/octet-stream;base64,A...%3C!--)--%3E...=");color:red;}',
+        '.a{cursor:__ESCAPED_URL_CLEAN_CSS0__;color:red;}',
+        '.a{cursor:url("data:application/octet-stream;base64,A...%3C!--)--%3E...=");color:red;}'
       ]
     })
   )