attribute_impl.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. var __extends = (this && this.__extends) || (function () {
  7. var extendStatics = function (d, b) {
  8. extendStatics = Object.setPrototypeOf ||
  9. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  10. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  11. return extendStatics(d, b);
  12. };
  13. return function (d, b) {
  14. if (typeof b !== "function" && b !== null)
  15. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  16. extendStatics(d, b);
  17. function __() { this.constructor = d; }
  18. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  19. };
  20. })();
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.unwrapAttributePrefix = exports.createAttributePrefix = exports.SafeAttributePrefix = void 0;
  23. require("../environment/dev");
  24. var secrets_1 = require("./secrets");
  25. /** A prefix with which an attribute is safe to set using plain strings. */
  26. var SafeAttributePrefix = /** @class */ (function () {
  27. function SafeAttributePrefix() {
  28. }
  29. return SafeAttributePrefix;
  30. }());
  31. exports.SafeAttributePrefix = SafeAttributePrefix;
  32. /** Implementation for `SafeAttributePrefix` */
  33. var AttributePrefixImpl = /** @class */ (function (_super) {
  34. __extends(AttributePrefixImpl, _super);
  35. function AttributePrefixImpl(attrPrefix, token) {
  36. var _this = _super.call(this) || this;
  37. if (process.env.NODE_ENV !== 'production') {
  38. (0, secrets_1.ensureTokenIsValid)(token);
  39. }
  40. _this.privateDoNotAccessOrElseWrappedAttrPrefix = attrPrefix;
  41. return _this;
  42. }
  43. AttributePrefixImpl.prototype.toString = function () {
  44. return this.privateDoNotAccessOrElseWrappedAttrPrefix;
  45. };
  46. return AttributePrefixImpl;
  47. }(SafeAttributePrefix));
  48. /**
  49. * Builds a new `SafeAttribute` from the given string, without enforcing
  50. * safety guarantees. This shouldn't be exposed to application developers, and
  51. * must only be used as a step towards safe builders or safe constants.
  52. */
  53. function createAttributePrefix(attrPrefix) {
  54. return new AttributePrefixImpl(attrPrefix, secrets_1.secretToken);
  55. }
  56. exports.createAttributePrefix = createAttributePrefix;
  57. /**
  58. * Returns the string value of the passed `SafeAttributePrefix` object while
  59. * ensuring it has the correct type.
  60. */
  61. function unwrapAttributePrefix(value) {
  62. if (value instanceof AttributePrefixImpl) {
  63. return value.privateDoNotAccessOrElseWrappedAttrPrefix;
  64. }
  65. else {
  66. var message = '';
  67. if (process.env.NODE_ENV !== 'production') {
  68. message = 'Unexpected type when unwrapping SafeAttributePrefix';
  69. }
  70. throw new Error(message);
  71. }
  72. }
  73. exports.unwrapAttributePrefix = unwrapAttributePrefix;