html_impl.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.unwrapHtml = exports.isHtml = exports.EMPTY_HTML = exports.createHtml = exports.SafeHtml = void 0;
  8. require("../environment/dev");
  9. /* g3_import_pure from './pure' */
  10. var secrets_1 = require("./secrets");
  11. var trusted_types_1 = require("./trusted_types");
  12. /**
  13. * Runtime implementation of `TrustedHTML` in browsers that don't support it.
  14. */
  15. var HtmlImpl = /** @class */ (function () {
  16. function HtmlImpl(html, token) {
  17. if (process.env.NODE_ENV !== 'production') {
  18. (0, secrets_1.ensureTokenIsValid)(token);
  19. }
  20. this.privateDoNotAccessOrElseWrappedHtml = html;
  21. }
  22. HtmlImpl.prototype.toString = function () {
  23. return this.privateDoNotAccessOrElseWrappedHtml.toString();
  24. };
  25. return HtmlImpl;
  26. }());
  27. function createHtmlInternal(html, trusted) {
  28. return (trusted !== null && trusted !== void 0 ? trusted : new HtmlImpl(html, secrets_1.secretToken));
  29. }
  30. var GlobalTrustedHTML = (typeof window !== undefined) ? window.TrustedHTML : undefined;
  31. /**
  32. * Also exports the constructor so that instanceof checks work.
  33. */
  34. exports.SafeHtml = (GlobalTrustedHTML !== null && GlobalTrustedHTML !== void 0 ? GlobalTrustedHTML : HtmlImpl);
  35. /**
  36. * Builds a new `SafeHtml` from the given string, without enforcing safety
  37. * guarantees. It may cause side effects by creating a Trusted Types policy.
  38. * This shouldn't be exposed to application developers, and must only be used as
  39. * a step towards safe builders or safe constants.
  40. */
  41. function createHtml(html) {
  42. var _a;
  43. /** @noinline */
  44. var noinlineHtml = html;
  45. return createHtmlInternal(noinlineHtml, (_a = (0, trusted_types_1.getTrustedTypesPolicy)()) === null || _a === void 0 ? void 0 : _a.createHTML(noinlineHtml));
  46. }
  47. exports.createHtml = createHtml;
  48. /**
  49. * An empty `SafeHtml` constant.
  50. * Unlike the function above, using this will not create a policy.
  51. */
  52. exports.EMPTY_HTML =
  53. /* #__PURE__ */ (function () { var _a; return createHtmlInternal('', (_a = (0, trusted_types_1.getTrustedTypes)()) === null || _a === void 0 ? void 0 : _a.emptyHTML); })();
  54. /**
  55. * Checks if the given value is a `SafeHtml` instance.
  56. */
  57. function isHtml(value) {
  58. return value instanceof exports.SafeHtml;
  59. }
  60. exports.isHtml = isHtml;
  61. /**
  62. * Returns the value of the passed `SafeHtml` object while ensuring it
  63. * has the correct type.
  64. *
  65. * Returns a native `TrustedHTML` or a string if Trusted Types are disabled.
  66. */
  67. function unwrapHtml(value) {
  68. var _a;
  69. if ((_a = (0, trusted_types_1.getTrustedTypes)()) === null || _a === void 0 ? void 0 : _a.isHTML(value)) {
  70. return value;
  71. }
  72. else if (value instanceof HtmlImpl) {
  73. return value.privateDoNotAccessOrElseWrappedHtml;
  74. }
  75. else {
  76. var message = '';
  77. if (process.env.NODE_ENV !== 'production') {
  78. message = 'Unexpected type when unwrapping SafeHtml';
  79. }
  80. throw new Error(message);
  81. }
  82. }
  83. exports.unwrapHtml = unwrapHtml;