From e9a603ffc7901f9ec4cb77c52b427542fa2edf09 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Wed, 15 Jun 2016 08:23:29 +0200 Subject: [PATCH] Fixes #787 - regression in data URI processing. Yet another regression so we revert to the original order of processing `url()` values. --- History.md | 5 +++++ lib/urls/reduce.js | 28 +++++++++++++++------------- test/text/urls-processor-test.js | 5 +++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/History.md b/History.md index 9d55febf..f21e0e06 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/lib/urls/reduce.js b/lib/urls/reduce.js index b66e5cdb..35ad5556 100644 --- a/lib/urls/reduce.js +++ b/lib/urls/reduce.js @@ -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); } diff --git a/test/text/urls-processor-test.js b/test/text/urls-processor-test.js index 54111dfb..daa74e35 100644 --- a/test/text/urls-processor-test.js +++ b/test/text/urls-processor-test.js @@ -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;}' ] }) ) -- 2.34.1