Skip to content

Commit 1ef5de4

Browse files
benurbmichael-ciniawsky
authored andcommitted
fix(index): escape double quotes correctly (options.interpolate) (#154)
1 parent 5d4342a commit 1ef5de4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = function(content) {
5353
content = [content];
5454
links.forEach(function(link) {
5555
if(!loaderUtils.isUrlRequest(link.value, root)) return;
56-
56+
5757
if (link.value.indexOf('mailto:') > -1 ) return;
5858

5959
var uri = url.parse(link.value);
@@ -129,6 +129,9 @@ module.exports = function(content) {
129129
}
130130

131131
if(config.interpolate && config.interpolate !== 'require') {
132+
// Double escape quotes so that they are not unescaped completely in the template string
133+
content = content.replace(/\\"/g, "\\\\\"");
134+
content = content.replace(/\\'/g, "\\\\\'");
132135
content = compile('`' + content + '`').code;
133136
} else {
134137
content = JSON.stringify(content);

test/loaderTest.js

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe("loader", function() {
7171
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\" + require("./image.png") + \" />";'
7272
);
7373
});
74+
it("should preserve escaped quotes", function() {
75+
loader.call({}, '<script>{"json": "with \\"quotes\\" in value"}</script>').should.be.eql(
76+
'module.exports = "<script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";'
77+
);
78+
})
7479

7580
it("should preserve comments and white spaces when minimizing (via webpack config property)", function() {
7681
loader.call({
@@ -167,6 +172,13 @@ describe("loader", function() {
167172
'module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\">";'
168173
);
169174
});
175+
it("should not change handling of quotes when interpolation is enabled", function() {
176+
loader.call({
177+
query: "?interpolate"
178+
}, '<script>{"json": "with \\"quotes\\" in value"}</script>').should.be.eql(
179+
'module.exports = "<script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";'
180+
);
181+
})
170182
it("should enable interpolations when using interpolate=require flag and only require function to be translate", function() {
171183
loader.call({
172184
query: "?interpolate=require"

0 commit comments

Comments
 (0)