Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Escape Newline/Paragraph separators
Browse files Browse the repository at this point in the history
I think that the Webpack JSON loader seems like the right place to handle 
this issue.

For whatever reason, the JSON and Javascript specifications disagree on
whether or not strings can contain the unicode Newline or Paragraph 
characters.

In the case that a JSON object containing one of these characters is being
printed to a script tag, we should escape them.

See also, this discussion:
expressjs/express#1132 (comment)
  • Loading branch information
ajhyndman authored and laughinghan committed Dec 7, 2016
1 parent 4cf55b7 commit 76bd270
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ module.exports = function(source) {
this.cacheable && this.cacheable();
var value = typeof source === "string" ? JSON.parse(source) : source;
this.value = [value];
return "module.exports = " + JSON.stringify(value) + ";";
return "module.exports = " +
JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') +
";";
}

0 comments on commit 76bd270

Please sign in to comment.