A Node JS implementation of the CSS Object Model CSSStyleDeclaration interface.
This package is an extension of the CSSStyleDeclaration class in Nikita Vasilyev's CSSOM with added support for CSS 2 & 3 properties. The primary use case is for testing browser code in a Node environment.
It was originally created by Chad Walker, it is now maintained by the jsdom community.
Bug reports and pull requests are welcome.
This package exposes two flavors of the CSSStyleDeclaration
interface depending on the imported module.
This module default-exports the CSSStyleDeclaration
interface constructor, with the change that it can be constructed with an optional onChangeCallback
parameter. Whenever any CSS property is modified through an instance of this class, the callback (if provided) will be called with a string that represents all CSS properties of this element, serialized. This allows the embedding environment to properly reflect the style changes to an element's style
attribute.
Here is a crude example of using the onChangeCallback
to implement the style
property of HTMLElement
:
const CSSStyleDeclaration = require('cssstyle');
class HTMLElement extends Element {
constructor() {
this._style = new CSSStyleDeclaration(newCSSText => {
this.setAttributeNS(null, "style", newCSSText);
});
}
get style() {
return this._style;
}
set style(text) {
this._style.cssText = text;
}
}
This module exports the CSSStyleDeclaration
interface wrapper API generated by webidl2js. Unlike the default export, CSSStyleDeclaration
constructors installed by the webidl2js wrapper do not support construction, just like how they actually are in browsers. Creating new CSSStyleDeclaration
objects can be done with the create
method of the wrapper.
The privateData
parameter of create
and createImpl
provides a way to specify the onChangeCallback
that is a constructor parameter in the default export. Only the onChangeCallback
property is supported on privateData
currently, with the same semantics as the constructor parameter documented above.