style_impl.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.unwrapStyle = exports.isStyle = exports.createStyle = exports.SafeStyle = void 0;
  23. require("../environment/dev");
  24. var secrets_1 = require("./secrets");
  25. /**
  26. * Sequence of CSS declarations safe to use in style contexts in an HTML
  27. * document or in DOM APIs.
  28. */
  29. var SafeStyle = /** @class */ (function () {
  30. function SafeStyle() {
  31. }
  32. return SafeStyle;
  33. }());
  34. exports.SafeStyle = SafeStyle;
  35. /** Implementation for `SafeStyle` */
  36. var StyleImpl = /** @class */ (function (_super) {
  37. __extends(StyleImpl, _super);
  38. function StyleImpl(style, token) {
  39. var _this = _super.call(this) || this;
  40. if (process.env.NODE_ENV !== 'production') {
  41. (0, secrets_1.ensureTokenIsValid)(token);
  42. }
  43. _this.privateDoNotAccessOrElseWrappedStyle = style;
  44. return _this;
  45. }
  46. StyleImpl.prototype.toString = function () {
  47. return this.privateDoNotAccessOrElseWrappedStyle;
  48. };
  49. return StyleImpl;
  50. }(SafeStyle));
  51. /**
  52. * Builds a new `SafeStyle` from the given string, without enforcing
  53. * safety guarantees. This shouldn't be exposed to application developers, and
  54. * must only be used as a step towards safe builders or safe constants.
  55. */
  56. function createStyle(style) {
  57. return new StyleImpl(style, secrets_1.secretToken);
  58. }
  59. exports.createStyle = createStyle;
  60. /**
  61. * Checks if the given value is a `SafeStyle` instance.
  62. */
  63. function isStyle(value) {
  64. return value instanceof StyleImpl;
  65. }
  66. exports.isStyle = isStyle;
  67. /**
  68. * Returns the string value of the passed `SafeStyle` object while ensuring it
  69. * has the correct type.
  70. */
  71. function unwrapStyle(value) {
  72. if (value instanceof StyleImpl) {
  73. return value.privateDoNotAccessOrElseWrappedStyle;
  74. }
  75. else {
  76. var message = '';
  77. if (process.env.NODE_ENV !== 'production') {
  78. message = 'Unexpected type when unwrapping SafeStyle';
  79. }
  80. throw new Error(message);
  81. }
  82. }
  83. exports.unwrapStyle = unwrapStyle;