Skip to content

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 committed Apr 2, 2016
1 parent 1409807 commit be7abf1
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, undefined, "\t") + ";";
return "module.exports = " +
JSON.stringify(value, undefined, "\t")
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') +
";";
}

0 comments on commit be7abf1

Please sign in to comment.