|
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const Attr_1 = __importDefault(require("../../nodes/attr/Attr"));
- const DOMExceptionNameEnum_1 = __importDefault(require("../../exception/DOMExceptionNameEnum"));
- const DOMException_1 = __importDefault(require("../../exception/DOMException"));
- const CSSStyleDeclarationElementStyle_1 = __importDefault(require("./utilities/CSSStyleDeclarationElementStyle"));
- const CSSStyleDeclarationPropertyManager_1 = __importDefault(require("./utilities/CSSStyleDeclarationPropertyManager"));
- /**
- * CSS Style Declaration.
- */
- class AbstractCSSStyleDeclaration {
- /**
- * Constructor.
- *
- * @param [ownerElement] Computed style element.
- * @param [computed] Computed.
- */
- constructor(ownerElement = null, computed = false) {
- this.parentRule = null;
- this._style = null;
- this._elementStyle = null;
- this._style = !ownerElement ? new CSSStyleDeclarationPropertyManager_1.default() : null;
- this._ownerElement = ownerElement;
- this._computed = ownerElement ? computed : false;
- this._elementStyle = ownerElement
- ? new CSSStyleDeclarationElementStyle_1.default(ownerElement, this._computed)
- : null;
- }
- /**
- * Returns length.
- *
- * @returns Length.
- */
- get length() {
- if (this._ownerElement) {
- const style = this._elementStyle.getElementStyle();
- return style.size();
- }
- return this._style.size();
- }
- /**
- * Returns the style decleration as a CSS text.
- *
- * @returns CSS text.
- */
- get cssText() {
- if (this._ownerElement) {
- if (this._computed) {
- return '';
- }
- return this._elementStyle.getElementStyle().toString();
- }
- return this._style.toString();
- }
- /**
- * Sets CSS text.
- *
- * @param cssText CSS text.
- */
- set cssText(cssText) {
- if (this._computed) {
- 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);
- }
- if (this._ownerElement) {
- const style = new CSSStyleDeclarationPropertyManager_1.default({ cssText });
- if (!this._ownerElement['_attributes']['style']) {
- Attr_1.default._ownerDocument = this._ownerElement.ownerDocument;
- this._ownerElement['_attributes']['style'] = new Attr_1.default();
- this._ownerElement['_attributes']['style'].name = 'style';
- }
- if (this._ownerElement.isConnected) {
- this._ownerElement.ownerDocument['_cacheID']++;
- }
- this._ownerElement['_attributes']['style'].value = style.toString();
- }
- else {
- this._style = new CSSStyleDeclarationPropertyManager_1.default({ cssText });
- }
- }
- /**
- * Returns item.
- *
- * @param index Index.
- * @returns Item.
- */
- item(index) {
- if (this._ownerElement) {
- return this._elementStyle.getElementStyle().item(index);
- }
- return this._style.item(index);
- }
- /**
- * Set a property.
- *
- * @param name Property name.
- * @param value Value. Must not contain "!important" as that should be set using the priority parameter.
- * @param [priority] Can be "important", or an empty string.
- */
- setProperty(name, value, priority) {
- if (this._computed) {
- 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);
- }
- if (priority !== '' && priority !== undefined && priority !== 'important') {
- return;
- }
- const stringValue = String(value);
- if (!stringValue) {
- this.removeProperty(name);
- }
- else if (this._ownerElement) {
- if (!this._ownerElement['_attributes']['style']) {
- Attr_1.default._ownerDocument = this._ownerElement.ownerDocument;
- this._ownerElement['_attributes']['style'] = new Attr_1.default();
- this._ownerElement['_attributes']['style'].name = 'style';
- }
- const style = this._elementStyle.getElementStyle();
- style.set(name, stringValue, !!priority);
- if (this._ownerElement.isConnected) {
- this._ownerElement.ownerDocument['_cacheID']++;
- }
- this._ownerElement['_attributes']['style'].value = style.toString();
- }
- else {
- this._style.set(name, stringValue, !!priority);
- }
- }
- /**
- * Removes a property.
- *
- * @param name Property name in kebab case.
- * @param value Value. Must not contain "!important" as that should be set using the priority parameter.
- * @param [priority] Can be "important", or an empty string.
- */
- removeProperty(name) {
- if (this._computed) {
- 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);
- }
- if (this._ownerElement) {
- const style = this._elementStyle.getElementStyle();
- style.remove(name);
- const newCSSText = style.toString();
- if (newCSSText) {
- if (this._ownerElement.isConnected) {
- this._ownerElement.ownerDocument['_cacheID']++;
- }
- this._ownerElement['_attributes']['style'].value = newCSSText;
- }
- else {
- delete this._ownerElement['_attributes']['style'];
- }
- }
- else {
- this._style.remove(name);
- }
- }
- /**
- * Returns a property.
- *
- * @param name Property name in kebab case.
- * @returns Property value.
- */
- getPropertyValue(name) {
- if (this._ownerElement) {
- const style = this._elementStyle.getElementStyle();
- return style.get(name)?.value || '';
- }
- return this._style.get(name)?.value || '';
- }
- /**
- * Returns a property.
- *
- * @param name Property name in kebab case.
- * @returns "important" if set to be important.
- */
- getPropertyPriority(name) {
- if (this._ownerElement) {
- const style = this._elementStyle.getElementStyle();
- return style.get(name)?.important ? 'important' : '';
- }
- return this._style.get(name)?.important ? 'important' : '';
- }
- }
- exports.default = AbstractCSSStyleDeclaration;
- //# sourceMappingURL=AbstractCSSStyleDeclaration.js.map
|