From: Jakub Pawlowicz Date: Sun, 9 Aug 2015 09:55:15 +0000 (+0100) Subject: Fixes #640 - URI processing regression. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=44d40ddce795f85f78c9c27abae1e80d82cf8b57;p=clean-css.git Fixes #640 - URI processing regression. The fix to #633 included a regression in URI processing as the algorithm introduced should only be applied to data URIs. --- diff --git a/History.md b/History.md index 2c282d1c..9bcfc6ba 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/lib/urls/reduce.js b/lib/urls/reduce.js index 1f7d7fd5..daf4ee48 100644 --- a/lib/urls/reduce.js +++ b/lib/urls/reduce.js @@ -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; + } } } diff --git a/test/text/urls-processor-test.js b/test/text/urls-processor-test.js index f2a75aaa..ff66f76f 100644 --- a/test/text/urls-processor-test.js +++ b/test/text/urls-processor-test.js @@ -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}' ] }) )