Skip to content

Commit

Permalink
fix(index): escape double quotes correctly (options.interpolate) (#154
Browse files Browse the repository at this point in the history
)
  • Loading branch information
benurb authored and michael-ciniawsky committed Jan 4, 2018
1 parent 5d4342a commit 1ef5de4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -53,7 +53,7 @@ module.exports = function(content) {
content = [content];
links.forEach(function(link) {
if(!loaderUtils.isUrlRequest(link.value, root)) return;

if (link.value.indexOf('mailto:') > -1 ) return;

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

if(config.interpolate && config.interpolate !== 'require') {
// Double escape quotes so that they are not unescaped completely in the template string
content = content.replace(/\\"/g, "\\\\\"");
content = content.replace(/\\'/g, "\\\\\'");
content = compile('`' + content + '`').code;
} else {
content = JSON.stringify(content);
Expand Down
12 changes: 12 additions & 0 deletions test/loaderTest.js
Expand Up @@ -71,6 +71,11 @@ describe("loader", function() {
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\" + require("./image.png") + \" />";'
);
});
it("should preserve escaped quotes", function() {
loader.call({}, '<script>{"json": "with \\"quotes\\" in value"}</script>').should.be.eql(
'module.exports = "<script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";'
);
})

it("should preserve comments and white spaces when minimizing (via webpack config property)", function() {
loader.call({
Expand Down Expand Up @@ -167,6 +172,13 @@ describe("loader", function() {
'module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\">";'
);
});
it("should not change handling of quotes when interpolation is enabled", function() {
loader.call({
query: "?interpolate"
}, '<script>{"json": "with \\"quotes\\" in value"}</script>').should.be.eql(
'module.exports = "<script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";'
);
})
it("should enable interpolations when using interpolate=require flag and only require function to be translate", function() {
loader.call({
query: "?interpolate=require"
Expand Down

0 comments on commit 1ef5de4

Please sign in to comment.