Fixes #308 - parsing comments in quoted urls.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 5 Jul 2014 11:08:07 +0000 (12:08 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 5 Jul 2014 13:59:57 +0000 (14:59 +0100)
History.md
lib/images/url-rewriter.js
lib/text/comments.js
test/data/issue-308-min.css [new file with mode: 0644]
test/data/issue-308.css [new file with mode: 0644]
test/unit-test.js

index a2b8626..a9c309a 100644 (file)
@@ -2,6 +2,7 @@
 ==================
 
 * Improves QuoteScanner to handle comments correctly.
+* Fixed issue [#308](https://github.com/GoalSmashers/clean-css/issues/308) - parsing comments in quoted urls.
 * Fixed issue [#311](https://github.com/GoalSmashers/clean-css/issues/311) - leading/trailing decimal points.
 
 [2.2.5 / 2014-06-29](https://github.com/GoalSmashers/clean-css/compare/v2.2.4...v2.2.5)
index af17d5b..1788b88 100644 (file)
@@ -18,7 +18,10 @@ module.exports = {
         break;
 
       tempData.push(data.substring(cursor, nextStart));
-      var url = data.substring(nextStart + 4, nextEnd).replace(/['"]/g, '');
+      var url = data.substring(nextStart + 4, nextEnd);
+      if (!/\/\*|\*\//.test(url))
+        url = url.replace(/['"]/g, '');
+
       tempData.push('url(' + this._rebased(url, options) + ')');
       cursor = nextEnd + 1;
     }
index f6913c8..c715100 100644 (file)
@@ -1,4 +1,5 @@
 var EscapeStore = require('./escape-store');
+var QuoteScanner = require('./quote-scanner');
 
 module.exports = function Comments(keepSpecialComments, keepBreaks, lineBreak) {
   var comments = new EscapeStore('COMMENT');
@@ -12,11 +13,34 @@ module.exports = function Comments(keepSpecialComments, keepBreaks, lineBreak) {
       var nextStart = 0;
       var nextEnd = 0;
       var cursor = 0;
+      var isQuotedAt = (function () {
+        var quoteMap = [];
+        new QuoteScanner(data).each(function (quotedString, _, startsAt) {
+          quoteMap.push([startsAt, startsAt + quotedString.length]);
+        });
+
+        return function (position) {
+          for (var i = 0, l = quoteMap.length; i < l; i++) {
+            if (quoteMap[i][0] < position && quoteMap[i][1] > position)
+              return true;
+          }
+
+          return false;
+        };
+      })();
 
       for (; nextEnd < data.length;) {
         nextStart = data.indexOf('/*', cursor);
+        if (nextStart == -1)
+          break;
+        if (isQuotedAt(nextStart)) {
+          tempData.push(data.substring(cursor, nextStart + 2));
+          cursor = nextStart + 2;
+          continue;
+        }
+
         nextEnd = data.indexOf('*/', nextStart + 2);
-        if (nextStart == -1 || nextEnd == -1)
+        if (nextEnd == -1)
           break;
 
         tempData.push(data.substring(cursor, nextStart));
diff --git a/test/data/issue-308-min.css b/test/data/issue-308-min.css
new file mode 100644 (file)
index 0000000..db80a90
--- /dev/null
@@ -0,0 +1 @@
+p{background-image:url(/*)}
diff --git a/test/data/issue-308.css b/test/data/issue-308.css
new file mode 100644 (file)
index 0000000..5c2ad4a
--- /dev/null
@@ -0,0 +1,5 @@
+p {
+  background-image: url('/*');
+}
+
+/* */
index 2dd4a23..6d01379 100644 (file)
@@ -293,6 +293,22 @@ vows.describe('clean-units').addBatch({
     'selector between comments': [
       '/*comment*/*/*comment*/{color:red}',
       '*{color:red}'
+    ],
+    'inside url': [
+      "p{background-image:url('/*')}/* */",
+      "p{background-image:url(/*)}"
+    ],
+    'inside url twice': [
+      "p{background-image:url('/* */\" /*')}/* */",
+      "p{background-image:url('/* */\" /*')}"
+    ],
+    'inside url with more quotation': [
+      "p{background-image:url('/*');content:\"\"/* */}",
+      "p{background-image:url(/*);content:\"\"}"
+    ],
+    'with quote marks': [
+      '/*"*//* */',
+      ''
     ]
   }),
   'escaping': cssContext({