script_builders.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.safeScriptWithArgs = exports.scriptFromJson = exports.concatScripts = exports.safeScript = void 0;
  8. require("../environment/dev");
  9. var script_impl_1 = require("../internals/script_impl");
  10. var string_literal_1 = require("../internals/string_literal");
  11. /**
  12. * Creates a SafeScript object from a template literal (without any embedded
  13. * expressions).
  14. *
  15. * This function is a template literal tag function. It should be called with
  16. * a template literal that does not contain any expressions. For example,
  17. * safeScript`foo`;
  18. *
  19. * @param templateObj This contains the literal part of the template literal.
  20. */
  21. function safeScript(templateObj) {
  22. if (process.env.NODE_ENV !== 'production') {
  23. (0, string_literal_1.assertIsTemplateObject)(templateObj, false, 'safeScript is a template literal tag function ' +
  24. 'that only accepts template literals without expressions. ' +
  25. 'For example, safeScript`foo`;');
  26. }
  27. return (0, script_impl_1.createScript)(templateObj[0]);
  28. }
  29. exports.safeScript = safeScript;
  30. /** Creates a `SafeScript` value by concatenating multiple `SafeScript`s. */
  31. function concatScripts(scripts) {
  32. return (0, script_impl_1.createScript)(scripts.map(script_impl_1.unwrapScript).join(''));
  33. }
  34. exports.concatScripts = concatScripts;
  35. /**
  36. * Converts a serializable value into JSON that is safe to interpolate into a
  37. * script context. In particular it escapes < characters so that a value of
  38. * "</script>" doesn't break out of the context.
  39. * @param value The value to serialize.
  40. */
  41. function scriptFromJson(value) {
  42. return (0, script_impl_1.createScript)(JSON.stringify(value).replace(/</g, '\\x3c'));
  43. }
  44. exports.scriptFromJson = scriptFromJson;
  45. /**
  46. * Creates a `SafeScript` object from a template literal (without any embedded
  47. * expressions) along with additional arguments that the script should have
  48. * access to. These arguments will be JSON-encoded and passed to the script as
  49. * a function call.
  50. * @example
  51. * ```ts
  52. * safeScriptWithArgs`function (name, props) {
  53. * console.log(name + ' is ' + props.age);
  54. * }`('Bob', { 'age': 42 })
  55. * ```
  56. * would return a `SafeScript` that represents the following code:
  57. * ```js
  58. * (function (name, props) {
  59. * console.log(name + ' is ' + props.age);
  60. * })("Bob",{"age":42})
  61. * ```
  62. * @note Be careful when passing objects as arguments, as unquoted property
  63. * names may be changed during compilation.
  64. * @param templateObj This contains the literal part of the template literal.
  65. * @param emptyArgs Expressions that evaluate to the empty string to enable
  66. * inline comments.
  67. */
  68. function safeScriptWithArgs(templateObj) {
  69. var emptyArgs = [];
  70. for (var _i = 1; _i < arguments.length; _i++) {
  71. emptyArgs[_i - 1] = arguments[_i];
  72. }
  73. if (process.env.NODE_ENV !== 'production') {
  74. if (emptyArgs.some(function (a) { return a !== ''; })) {
  75. throw new Error('safeScriptWithArgs only allows empty string expressions ' +
  76. 'to enable inline comments.');
  77. }
  78. (0, string_literal_1.assertIsTemplateObject)(templateObj, true, 'safeScriptWithArgs is a template literal tag function ' +
  79. 'that only accepts template literals. ' +
  80. 'For example, safeScriptWithArgs`foo`;');
  81. }
  82. return function () {
  83. var argValues = [];
  84. for (var _i = 0; _i < arguments.length; _i++) {
  85. argValues[_i] = arguments[_i];
  86. }
  87. var values = argValues.map(function (v) { return scriptFromJson(v).toString(); });
  88. return (0, script_impl_1.createScript)("(".concat(templateObj.join(''), ")(").concat(values.join(','), ")"));
  89. };
  90. }
  91. exports.safeScriptWithArgs = safeScriptWithArgs;