版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

96 строки
3.3 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const DOMException_1 = __importDefault(require("../exception/DOMException"));
  7. const DOMExceptionNameEnum_1 = __importDefault(require("../exception/DOMExceptionNameEnum"));
  8. const CSSParser_1 = __importDefault(require("./CSSParser"));
  9. /**
  10. * CSS StyleSheet.
  11. *
  12. * Reference:
  13. * https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet.
  14. */
  15. class CSSStyleSheet {
  16. /**
  17. * Constructor.
  18. *
  19. * @param [options] Options.
  20. * @param [options.media] Media.
  21. * @param [options.title] Title.
  22. * @param [options.alternate] Alternate.
  23. * @param [options.disabled] Disabled.
  24. */
  25. constructor(options) {
  26. this.value = null;
  27. this.name = null;
  28. this.namespaceURI = null;
  29. this.cssRules = [];
  30. this._currentText = null;
  31. this.media = options && options.media ? options.media : '';
  32. this.title = options && options.title ? options.title : '';
  33. this.alternate = options && options.alternate ? options.alternate : false;
  34. this.disabled = options && options.disabled ? options.disabled : false;
  35. }
  36. /**
  37. * Inserts a rule.
  38. *
  39. * @see https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
  40. * @param rule Rule.
  41. * @param [index] Index.
  42. * @returns The newly inserterted rule's index.
  43. */
  44. insertRule(rule, index) {
  45. const rules = CSSParser_1.default.parseFromString(this, rule);
  46. if (rules.length === 0) {
  47. throw new DOMException_1.default('Invalid CSS rule.', DOMExceptionNameEnum_1.default.hierarchyRequestError);
  48. }
  49. if (rules.length > 1) {
  50. throw new DOMException_1.default('Only one rule is allowed to be added.', DOMExceptionNameEnum_1.default.syntaxError);
  51. }
  52. if (index !== undefined) {
  53. if (index > this.cssRules.length) {
  54. throw new DOMException_1.default('Index is more than the length of CSSRuleList.', DOMExceptionNameEnum_1.default.indexSizeError);
  55. }
  56. this.cssRules.splice(index, 0, rules[0]);
  57. return index;
  58. }
  59. const newIndex = this.cssRules.length;
  60. this.cssRules.push(rules[0]);
  61. return newIndex;
  62. }
  63. /**
  64. * Removes a rule.
  65. *
  66. * @see https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/deleteRule
  67. * @param index Index.
  68. */
  69. deleteRule(index) {
  70. delete this.cssRules[index];
  71. }
  72. /**
  73. * Replaces all CSS rules.
  74. *
  75. * @see https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/replace
  76. * @param text CSS text.
  77. * @returns Promise.
  78. */
  79. async replace(text) {
  80. this.replaceSync(text);
  81. }
  82. /**
  83. * Replaces all CSS rules.
  84. *
  85. * @see https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/replaceSync
  86. * @param text CSS text.
  87. */
  88. replaceSync(text) {
  89. if (this._currentText !== text) {
  90. this._currentText = text;
  91. this.cssRules = CSSParser_1.default.parseFromString(this, text);
  92. }
  93. }
  94. }
  95. exports.default = CSSStyleSheet;
  96. //# sourceMappingURL=CSSStyleSheet.js.map