StringHelper.mjs 983 B

123456789101112131415161718192021222324252627282930
  1. /* eslint-disable
  2. max-len,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. let StringHelper
  8. const JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/g
  9. const JSON_ESCAPE = {
  10. '&': '\\u0026',
  11. '>': '\\u003e',
  12. '<': '\\u003c',
  13. '\u2028': '\\u2028',
  14. '\u2029': '\\u2029',
  15. }
  16. module.exports = StringHelper = {
  17. // stringifies and escapes a json object for use in a script. This ensures that &, < and > characters are escaped,
  18. // along with quotes. This ensures that the string can be safely rendered into HTML. See rationale at:
  19. // https://api.rubyonrails.org/classes/ERB/Util.html#method-c-json_escape
  20. // and implementation lifted from:
  21. // https://github.com/ember-fastboot/fastboot/blob/cafd96c48564d8384eb83dc908303dba8ece10fd/src/ember-app.js#L496-L510
  22. stringifyJsonForScript(object) {
  23. return JSON.stringify(object).replace(
  24. JSON_ESCAPE_REGEXP,
  25. match => JSON_ESCAPE[match]
  26. )
  27. },
  28. }