From 55165afa40de92f6cdf9f4d8b3ad33909e3b82d1 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 30 May 2016 07:21:53 +0200 Subject: [PATCH] Fixes #763 - data URI SVG and quoting. Quotes inside data URI SVG can be unescaped and we should handle such cases correctly by leaving quoting in place. --- History.md | 1 + lib/text/urls-processor.js | 3 +++ test/integration-test.js | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/History.md b/History.md index 0cd92486..ed841708 100644 --- a/History.md +++ b/History.md @@ -7,6 +7,7 @@ ================== * Fixed issue [#751](https://github.com/jakubpawlowicz/clean-css/issues/751) - stringifying CSS variables. +* Fixed issue [#763](https://github.com/jakubpawlowicz/clean-css/issues/763) - data URI SVG and quoting. [3.4.13 / 2016-05-23](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.12...v3.4.13) ================== diff --git a/lib/text/urls-processor.js b/lib/text/urls-processor.js index 7e4a5301..4dbb55ad 100644 --- a/lib/text/urls-processor.js +++ b/lib/text/urls-processor.js @@ -42,6 +42,9 @@ function normalize(url, keepUrlQuotes) { .replace(/^url\((['"])? /, 'url($1') .replace(/ (['"])?\)$/, '$1)'); + if (/url\(".*'.*"\)/.test(url) || /url\('.*".*'\)/.test(url)) + return url; + if (!keepUrlQuotes && !/^['"].+['"]$/.test(url) && !/url\(.*[\s\(\)].*\)/.test(url) && !/url\(['"]data:[^;]+;charset/.test(url)) url = url.replace(/["']/g, ''); diff --git a/test/integration-test.js b/test/integration-test.js index cb3c9af2..efc240ed 100644 --- a/test/integration-test.js +++ b/test/integration-test.js @@ -1381,6 +1381,14 @@ vows.describe('integration tests') 'quotes SVG data URI if with parentheses': [ 'div{background:url(data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2018%2018%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2214%22%20height%3D%2214%22%20transform%3D%22translate(2%202)%22%2F%3E%3C%2Fsvg%3E) bottom left}', 'div{background:url(\'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2018%2018%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2214%22%20height%3D%2214%22%20transform%3D%22translate(2%202)%22%2F%3E%3C%2Fsvg%3E\') bottom left}' + ], + 'do not unquote double quoted URI with unescaped single quotes inside': [ + 'a{background:url("data:image/svg+xml,%3csvg%20xmlns%3d\'http://www.w3.org/2000/svg\'/%3e")}', + 'a{background:url("data:image/svg+xml,%3csvg%20xmlns%3d\'http://www.w3.org/2000/svg\'/%3e")}' + ], + 'do not unquote single quoted URI with unescaped double quotes inside': [ + 'a{background:url(\'data:image/svg+xml,%3csvg%20xmlns%3d"http://www.w3.org/2000/svg"/%3e\')}', + 'a{background:url(\'data:image/svg+xml,%3csvg%20xmlns%3d"http://www.w3.org/2000/svg"/%3e\')}' ] }, { root: process.cwd(), relativeTo: process.cwd() }) ) -- 2.34.1