From 1ef5de4f7886cda0828084450e8086c68f2db740 Mon Sep 17 00:00:00 2001 From: Benjamin Urban Date: Thu, 4 Jan 2018 21:42:39 +0100 Subject: [PATCH] fix(index): escape double quotes correctly (`options.interpolate`) (#154) --- index.js | 5 ++++- test/loaderTest.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8465da00..5b8b3618 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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); diff --git a/test/loaderTest.js b/test/loaderTest.js index b5957dc1..7efe7800 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -71,6 +71,11 @@ describe("loader", function() { 'module.exports = "

#{number} {customer}

{title}

";' ); }); + it("should preserve escaped quotes", function() { + loader.call({}, '').should.be.eql( + 'module.exports = "";' + ); + }) it("should preserve comments and white spaces when minimizing (via webpack config property)", function() { loader.call({ @@ -167,6 +172,13 @@ describe("loader", function() { 'module.exports = "";' ); }); + it("should not change handling of quotes when interpolation is enabled", function() { + loader.call({ + query: "?interpolate" + }, '').should.be.eql( + 'module.exports = "";' + ); + }) it("should enable interpolations when using interpolate=require flag and only require function to be translate", function() { loader.call({ query: "?interpolate=require"