Skip to content

Commit f294c2e

Browse files
committed
better parsing
fixes webpack-contrib#15 fixes webpack-contrib#5
1 parent 73c0494 commit f294c2e

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ module.exports = function(content) {
3838
var css = JSON.stringify(tree ? csso.translate(tree) : "");
3939
var uriRegExp = /%CSSURL\[%(.*?)%\]CSSURL%/g;
4040
css = css.replace(uriRegExp, function(str) {
41-
var match = /^%CSSURL\[%(.*?)%\]CSSURL%$/.exec(str);
42-
if(!loaderUtils.isUrlRequest(match[1], root)) return match[1];
43-
var idx = match[1].indexOf("?");
44-
if(idx < 0) idx = match[1].indexOf("#");
41+
var match = /^%CSSURL\[%(["']?(.*?)["']?)%\]CSSURL%$/.exec(JSON.parse('"' + str + '"'));
42+
var url = loaderUtils.parseString(match[2]);
43+
if(!loaderUtils.isUrlRequest(match[2], root)) return JSON.stringify(match[1]).replace(/^"|"$/g, "");
44+
var idx = url.indexOf("?");
45+
if(idx < 0) idx = url.indexOf("#");
4546
if(idx > 0) {
4647
// in cases like url('webfont.eot?#iefix')
47-
var url = JSON.parse("\"" + match[1].substr(0, idx) + "\"");
48-
return "\"+require(" + JSON.stringify(loaderUtils.urlToRequest(url, root)) + ")+\"" + match[1].substr(idx);
48+
var request = url.substr(0, idx);
49+
return "\"+require(" + JSON.stringify(loaderUtils.urlToRequest(request, root)) + ")+\"" + url.substr(idx);
4950
} else if(idx === 0) {
5051
// only hash
51-
return match[1];
52+
return JSON.stringify(match[1]).replace(/^"|"$/g, "");
5253
}
53-
var url = JSON.parse("\"" + match[1] + "\"");
5454
return "\"+require(" + JSON.stringify(loaderUtils.urlToRequest(url, root)) + ")+\"";
5555
});
5656
result.push(css);
@@ -81,9 +81,9 @@ function extractImports(tree) {
8181
for(var j = 2; j < rule.length; j++) {
8282
var item = rule[j];
8383
if(item[0] === "string") {
84-
imp.url = JSON.parse(item[1]);
84+
imp.url = loaderUtils.parseString(item[1]);
8585
} else if(item[0] === "uri") {
86-
imp.url = item[1][0] === "string" ? JSON.parse(item[1][1]) : item[1][1];
86+
imp.url = item[1][0] === "string" ? loaderUtils.parseString(item[1][1]) : item[1][1];
8787
} else if(item[0] === "ident" && item[1] !== "url") {
8888
imp.media.push(csso.translate(item));
8989
} else if(item[0] !== "s" || imp.media.length > 0) {
@@ -128,7 +128,7 @@ function annotateUrls(tree) {
128128
item[1] = "%CSSURL[%" + item[1] + "%]CSSURL%";
129129
return;
130130
case "string":
131-
item[1] = "%CSSURL[%" + item[1].substring(1, item[1].length-1) + "%]CSSURL%";
131+
item[1] = "%CSSURL[%" + item[1] + "%]CSSURL%";
132132
return;
133133
}
134134
}

test/urlTest.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ describe("url", function() {
4646
test("import", "@import url(test.css);\n.class { a: b c d; }",
4747
["require("+JSON.stringify("!"+path.join(__dirname, "..", "index.js")+"!./test.css")+")",
4848
"\"\\n.class { a: b c d; }\""]);
49+
test("import 2", "@import url('test.css');",
50+
["require("+JSON.stringify("!"+path.join(__dirname, "..", "index.js")+"!./test.css")+")",
51+
"\"\""]);
4952
test("import with media", "@import url(~test/css) screen and print;\n.class { a: b c d; }",
5053
["\"@media screen and print{\"",
5154
"require("+JSON.stringify("!"+path.join(__dirname, "..", "index.js")+"!test/css")+")",
@@ -59,13 +62,18 @@ describe("url", function() {
5962
["\".class { background: green url( \"+require(\"./img.png\")+\" ) xyz }\""]);
6063
test("background img 2", ".class { background: green url(~img/png ) url(aaa) xyz }",
6164
["\".class { background: green url(\"+require(\"img/png\")+\" ) url(\"+require(\"./aaa\")+\") xyz }\""]);
65+
test("background img 3", ".class { background: green url( 'img.png' ) xyz }",
66+
["\".class { background: green url( \"+require(\"./img.png\")+\" ) xyz }\""]);
6267
test("background img absolute", ".class { background: green url(/img.png) xyz }",
6368
["\".class { background: green url(/img.png) xyz }\""]);
6469
test("background img absolute with root", ".class { background: green url(/img.png) xyz }",
6570
["\".class { background: green url(\"+require(\"./img.png\")+\") xyz }\""], "?root=.");
6671
test("background img external",
6772
".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }",
6873
["\".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }\""]);
74+
test("background img external data",
75+
".class { background-image: url(\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\") }",
76+
["\".class { background-image: url(\\\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\\\") }\""]);
6977
test("filter hash",
7078
".highlight { filter: url(#highlight); }",
7179
["\".highlight { filter: url(#highlight); }\""]);

0 commit comments

Comments
 (0)