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

187 строки
7.0 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 Attr_1 = __importDefault(require("../../nodes/attr/Attr"));
  7. const DOMExceptionNameEnum_1 = __importDefault(require("../../exception/DOMExceptionNameEnum"));
  8. const DOMException_1 = __importDefault(require("../../exception/DOMException"));
  9. const CSSStyleDeclarationElementStyle_1 = __importDefault(require("./utilities/CSSStyleDeclarationElementStyle"));
  10. const CSSStyleDeclarationPropertyManager_1 = __importDefault(require("./utilities/CSSStyleDeclarationPropertyManager"));
  11. /**
  12. * CSS Style Declaration.
  13. */
  14. class AbstractCSSStyleDeclaration {
  15. /**
  16. * Constructor.
  17. *
  18. * @param [ownerElement] Computed style element.
  19. * @param [computed] Computed.
  20. */
  21. constructor(ownerElement = null, computed = false) {
  22. this.parentRule = null;
  23. this._style = null;
  24. this._elementStyle = null;
  25. this._style = !ownerElement ? new CSSStyleDeclarationPropertyManager_1.default() : null;
  26. this._ownerElement = ownerElement;
  27. this._computed = ownerElement ? computed : false;
  28. this._elementStyle = ownerElement
  29. ? new CSSStyleDeclarationElementStyle_1.default(ownerElement, this._computed)
  30. : null;
  31. }
  32. /**
  33. * Returns length.
  34. *
  35. * @returns Length.
  36. */
  37. get length() {
  38. if (this._ownerElement) {
  39. const style = this._elementStyle.getElementStyle();
  40. return style.size();
  41. }
  42. return this._style.size();
  43. }
  44. /**
  45. * Returns the style decleration as a CSS text.
  46. *
  47. * @returns CSS text.
  48. */
  49. get cssText() {
  50. if (this._ownerElement) {
  51. if (this._computed) {
  52. return '';
  53. }
  54. return this._elementStyle.getElementStyle().toString();
  55. }
  56. return this._style.toString();
  57. }
  58. /**
  59. * Sets CSS text.
  60. *
  61. * @param cssText CSS text.
  62. */
  63. set cssText(cssText) {
  64. if (this._computed) {
  65. throw new DOMException_1.default(`Failed to execute 'cssText' on 'CSSStyleDeclaration': These styles are computed, and the properties are therefore read-only.`, DOMExceptionNameEnum_1.default.domException);
  66. }
  67. if (this._ownerElement) {
  68. const style = new CSSStyleDeclarationPropertyManager_1.default({ cssText });
  69. if (!this._ownerElement['_attributes']['style']) {
  70. Attr_1.default._ownerDocument = this._ownerElement.ownerDocument;
  71. this._ownerElement['_attributes']['style'] = new Attr_1.default();
  72. this._ownerElement['_attributes']['style'].name = 'style';
  73. }
  74. if (this._ownerElement.isConnected) {
  75. this._ownerElement.ownerDocument['_cacheID']++;
  76. }
  77. this._ownerElement['_attributes']['style'].value = style.toString();
  78. }
  79. else {
  80. this._style = new CSSStyleDeclarationPropertyManager_1.default({ cssText });
  81. }
  82. }
  83. /**
  84. * Returns item.
  85. *
  86. * @param index Index.
  87. * @returns Item.
  88. */
  89. item(index) {
  90. if (this._ownerElement) {
  91. return this._elementStyle.getElementStyle().item(index);
  92. }
  93. return this._style.item(index);
  94. }
  95. /**
  96. * Set a property.
  97. *
  98. * @param name Property name.
  99. * @param value Value. Must not contain "!important" as that should be set using the priority parameter.
  100. * @param [priority] Can be "important", or an empty string.
  101. */
  102. setProperty(name, value, priority) {
  103. if (this._computed) {
  104. throw new DOMException_1.default(`Failed to execute 'setProperty' on 'CSSStyleDeclaration': These styles are computed, and therefore the '${name}' property is read-only.`, DOMExceptionNameEnum_1.default.domException);
  105. }
  106. if (priority !== '' && priority !== undefined && priority !== 'important') {
  107. return;
  108. }
  109. const stringValue = String(value);
  110. if (!stringValue) {
  111. this.removeProperty(name);
  112. }
  113. else if (this._ownerElement) {
  114. if (!this._ownerElement['_attributes']['style']) {
  115. Attr_1.default._ownerDocument = this._ownerElement.ownerDocument;
  116. this._ownerElement['_attributes']['style'] = new Attr_1.default();
  117. this._ownerElement['_attributes']['style'].name = 'style';
  118. }
  119. const style = this._elementStyle.getElementStyle();
  120. style.set(name, stringValue, !!priority);
  121. if (this._ownerElement.isConnected) {
  122. this._ownerElement.ownerDocument['_cacheID']++;
  123. }
  124. this._ownerElement['_attributes']['style'].value = style.toString();
  125. }
  126. else {
  127. this._style.set(name, stringValue, !!priority);
  128. }
  129. }
  130. /**
  131. * Removes a property.
  132. *
  133. * @param name Property name in kebab case.
  134. * @param value Value. Must not contain "!important" as that should be set using the priority parameter.
  135. * @param [priority] Can be "important", or an empty string.
  136. */
  137. removeProperty(name) {
  138. if (this._computed) {
  139. throw new DOMException_1.default(`Failed to execute 'removeProperty' on 'CSSStyleDeclaration': These styles are computed, and therefore the '${name}' property is read-only.`, DOMExceptionNameEnum_1.default.domException);
  140. }
  141. if (this._ownerElement) {
  142. const style = this._elementStyle.getElementStyle();
  143. style.remove(name);
  144. const newCSSText = style.toString();
  145. if (newCSSText) {
  146. if (this._ownerElement.isConnected) {
  147. this._ownerElement.ownerDocument['_cacheID']++;
  148. }
  149. this._ownerElement['_attributes']['style'].value = newCSSText;
  150. }
  151. else {
  152. delete this._ownerElement['_attributes']['style'];
  153. }
  154. }
  155. else {
  156. this._style.remove(name);
  157. }
  158. }
  159. /**
  160. * Returns a property.
  161. *
  162. * @param name Property name in kebab case.
  163. * @returns Property value.
  164. */
  165. getPropertyValue(name) {
  166. if (this._ownerElement) {
  167. const style = this._elementStyle.getElementStyle();
  168. return style.get(name)?.value || '';
  169. }
  170. return this._style.get(name)?.value || '';
  171. }
  172. /**
  173. * Returns a property.
  174. *
  175. * @param name Property name in kebab case.
  176. * @returns "important" if set to be important.
  177. */
  178. getPropertyPriority(name) {
  179. if (this._ownerElement) {
  180. const style = this._elementStyle.getElementStyle();
  181. return style.get(name)?.important ? 'important' : '';
  182. }
  183. return this._style.get(name)?.important ? 'important' : '';
  184. }
  185. }
  186. exports.default = AbstractCSSStyleDeclaration;
  187. //# sourceMappingURL=AbstractCSSStyleDeclaration.js.map