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

Conversation

benurb
Copy link
Contributor

@benurb benurb commented Nov 10, 2017

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior? (You can also link to an open issue here)
Currently escaped quotes in inline scripts are unescaped when interpolation is enabled. You can find a minimal test case here: https://github.com/benurb/html-loader-escape-bug

What is the new behavior?
interpolate=true behaves like disabled interpolation and quotes are still escaped.

Does this PR introduce a breaking change?

  • Yes
  • No
    The documentation states no difference between interpolate=true and false regarding the handling of escaped quotes.

If this PR contains a breaking change, please describe the following...

Other information:
This solution is very hacky, but I couldn't find a cleaner one 😞
Without these changes the second of the two added tests fails.
This PR might also be a fix for #153 though I don't use angular and haven't tested it.

@jsf-clabot
Copy link

jsf-clabot commented Nov 10, 2017

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Nov 10, 2017

Codecov Report

Merging #154 into master will increase coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #154      +/-   ##
==========================================
+ Coverage   96.96%   97.02%   +0.06%     
==========================================
  Files           2        2              
  Lines          99      101       +2     
  Branches       20       20              
==========================================
+ Hits           96       98       +2     
  Misses          3        3
Impacted Files Coverage Δ
index.js 96.7% <100%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b13d4c...1d2d5ad. Read the comment docs.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Looks good!

@joshwiens joshwiens changed the title Fix escaped quotes when interpolate is enabled fix: Escaped quotes when interpolate is enabled Nov 10, 2017
Copy link
Member

@joshwiens joshwiens left a comment

Choose a reason for hiding this comment

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

@michael-ciniawsky - Ship it if you are good with it

@@ -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?

@@ -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

@michael-ciniawsky michael-ciniawsky changed the title fix: Escaped quotes when interpolate is enabled fix(index): Double escape quotes (options.interpolate) Nov 11, 2017
@benurb
Copy link
Contributor Author

benurb commented Dec 27, 2017

Is there still anything I can do? @michael-ciniawsky

@joshwiens
Copy link
Member

@benurb - Everyone is a bit buried getting ready for Webpack@next

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

@benurb Thx && Sry for the delay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants