From: Jakub Pawlowicz Date: Sat, 5 Jul 2014 11:08:07 +0000 (+0100) Subject: Fixes #308 - parsing comments in quoted urls. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=1a186bdb09b2c6e925196b16c575b60ce780bc77;p=clean-css.git Fixes #308 - parsing comments in quoted urls. --- diff --git a/History.md b/History.md index a2b86268..a9c309a5 100644 --- a/History.md +++ b/History.md @@ -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) diff --git a/lib/images/url-rewriter.js b/lib/images/url-rewriter.js index af17d5ba..1788b881 100644 --- a/lib/images/url-rewriter.js +++ b/lib/images/url-rewriter.js @@ -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; } diff --git a/lib/text/comments.js b/lib/text/comments.js index f6913c80..c7151002 100644 --- a/lib/text/comments.js +++ b/lib/text/comments.js @@ -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 index 00000000..db80a901 --- /dev/null +++ b/test/data/issue-308-min.css @@ -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 index 00000000..5c2ad4a6 --- /dev/null +++ b/test/data/issue-308.css @@ -0,0 +1,5 @@ +p { + background-image: url('/*'); +} + +/* */ diff --git a/test/unit-test.js b/test/unit-test.js index 2dd4a23a..6d013790 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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({