StringHelper.coffee 763 B

123456789101112131415161718
  1. JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/g
  2. JSON_ESCAPE =
  3. '&': '\\u0026'
  4. '>': '\\u003e'
  5. '<': '\\u003c'
  6. '\u2028': '\\u2028'
  7. '\u2029': '\\u2029'
  8. module.exports = StringHelper =
  9. # stringifies and escapes a json object for use in a script. This ensures that &, < and > characters are escaped,
  10. # along with quotes. This ensures that the string can be safely rendered into HTML. See rationale at:
  11. # https://api.rubyonrails.org/classes/ERB/Util.html#method-c-json_escape
  12. # and implementation lifted from:
  13. # https://github.com/ember-fastboot/fastboot/blob/cafd96c48564d8384eb83dc908303dba8ece10fd/src/ember-app.js#L496-L510
  14. stringifyJsonForScript: (object) ->
  15. return JSON.stringify(object).replace JSON_ESCAPE_REGEXP, (match) ->
  16. return JSON_ESCAPE[match]