Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(index): escape double quotes correctly (options.interpolate) #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
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, "\\\\\"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\\\\\('|") (5) => \\\\('|") (4) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand.
The two regexes could be combined into one .replace(/\\("|')/g, "\\\\$1") or what do you mean?

content = content.replace(/\\'/g, "\\\\\'");
content = compile('`' + content + '`').code;
} else {
content = JSON.stringify(content);
Expand Down
12 changes: 12 additions & 0 deletions test/loaderTest.js
Original file line number Diff line number Diff line change
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>";'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only occurs for { key: "bla \"blub\" bla" }, but doesn't on { key: "bla 'blub' bla" } right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Michael,
sorry for the late reply, but yes: that is correct. I've updated my minimal test case with two more examples: https://github.com/benurb/html-loader-escape-bug

);
})

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