UNPKG

happy-dom

Version:

Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.

162 lines 5.63 kB
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum.js'; import CSSStyleDeclarationElementStyle from './element-style/CSSStyleDeclarationElementStyle.js'; import CSSStyleDeclarationPropertyManager from './property-manager/CSSStyleDeclarationPropertyManager.js'; import * as PropertySymbol from '../../PropertySymbol.js'; /** * CSS Style Declaration. */ export default class AbstractCSSStyleDeclaration { parentRule = null; #style = null; #ownerElement; #computed; #elementStyle = null; /** * Constructor. * * @param [ownerElement] Computed style element. * @param [computed] Computed. */ constructor(ownerElement = null, computed = false) { this.#style = !ownerElement ? new CSSStyleDeclarationPropertyManager() : null; this.#ownerElement = ownerElement; this.#computed = ownerElement ? computed : false; this.#elementStyle = ownerElement ? new CSSStyleDeclarationElementStyle(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 this[PropertySymbol.window].DOMException(`Failed to execute 'cssText' on 'CSSStyleDeclaration': These styles are computed, and the properties are therefore read-only.`, DOMExceptionNameEnum.domException); } if (this.#ownerElement) { this.#ownerElement.setAttribute('style', new CSSStyleDeclarationPropertyManager({ cssText }).toString()); } else { this.#style = new CSSStyleDeclarationPropertyManager({ 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 this[PropertySymbol.window].DOMException(`Failed to execute 'setProperty' on 'CSSStyleDeclaration': These styles are computed, and therefore the '${name}' property is read-only.`, DOMExceptionNameEnum.domException); } if (priority !== '' && priority !== undefined && priority !== 'important') { return; } const stringValue = String(value); if (!stringValue) { this.removeProperty(name); } else if (this.#ownerElement) { const style = this.#elementStyle.getElementStyle(); style.set(name, stringValue, !!priority); this.#ownerElement.setAttribute('style', 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 this[PropertySymbol.window].DOMException(`Failed to execute 'removeProperty' on 'CSSStyleDeclaration': These styles are computed, and therefore the '${name}' property is read-only.`, DOMExceptionNameEnum.domException); } if (this.#ownerElement) { const style = this.#elementStyle.getElementStyle(); style.remove(name); const newCSSText = style.toString(); if (newCSSText) { this.#ownerElement.setAttribute('style', newCSSText); } else { this.#ownerElement.removeAttribute('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' : ''; } } //# sourceMappingURL=AbstractCSSStyleDeclaration.js.map