-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
AbstractCSSStyleDeclaration.ts
200 lines (180 loc) · 5.75 KB
/
AbstractCSSStyleDeclaration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import IElement from '../../nodes/element/IElement';
import Attr from '../../nodes/attr/Attr';
import CSSRule from '../CSSRule';
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum';
import DOMException from '../../exception/DOMException';
import CSSStyleDeclarationElement from './utilities/CSSStyleDeclarationElement';
import CSSStyleDeclarationPropertyManager from './utilities/CSSStyleDeclarationPropertyManager';
/**
* CSS Style Declaration.
*/
export default abstract class AbstractCSSStyleDeclaration {
public readonly parentRule: CSSRule = null;
protected _style: CSSStyleDeclarationPropertyManager = null;
protected _ownerElement: IElement;
protected _computed: boolean;
/**
* Constructor.
*
* @param [ownerElement] Computed style element.
* @param [computed] Computed.
*/
constructor(ownerElement: IElement = null, computed = false) {
this._style = !ownerElement ? new CSSStyleDeclarationPropertyManager() : null;
this._ownerElement = ownerElement;
this._computed = ownerElement ? computed : false;
}
/**
* Returns length.
*
* @returns Length.
*/
public get length(): number {
if (this._ownerElement) {
const style = CSSStyleDeclarationElement.getElementStyle(this._ownerElement, this._computed);
return style.size();
}
return this._style.size();
}
/**
* Returns the style decleration as a CSS text.
*
* @returns CSS text.
*/
public get cssText(): string {
if (this._ownerElement) {
if (this._computed) {
return '';
}
return CSSStyleDeclarationElement.getElementStyle(this._ownerElement, false).toString();
}
return this._style.toString();
}
/**
* Sets CSS text.
*
* @param cssText CSS text.
*/
public set cssText(cssText: string) {
if (this._computed) {
throw new DOMException(
`Failed to execute 'cssText' on 'CSSStyleDeclaration': These styles are computed, and the properties are therefore read-only.`,
DOMExceptionNameEnum.domException
);
}
if (this._ownerElement) {
const style = new CSSStyleDeclarationPropertyManager({ cssText });
if (!style.size()) {
delete this._ownerElement['_attributes']['style'];
} else {
if (!this._ownerElement['_attributes']['style']) {
Attr._ownerDocument = this._ownerElement.ownerDocument;
this._ownerElement['_attributes']['style'] = new Attr();
this._ownerElement['_attributes']['style'].name = 'style';
}
this._ownerElement['_attributes']['style'].value = style.toString();
}
} else {
this._style = new CSSStyleDeclarationPropertyManager({ cssText });
}
}
/**
* Returns item.
*
* @param index Index.
* @returns Item.
*/
public item(index: number): string {
if (this._ownerElement) {
return CSSStyleDeclarationElement.getElementStyle(this._ownerElement, this._computed).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.
*/
public setProperty(name: string, value: string, priority?: 'important' | '' | undefined): void {
if (this._computed) {
throw new 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;
}
if (!value) {
this.removeProperty(name);
} else if (this._ownerElement) {
if (!this._ownerElement['_attributes']['style']) {
Attr._ownerDocument = this._ownerElement.ownerDocument;
this._ownerElement['_attributes']['style'] = new Attr();
this._ownerElement['_attributes']['style'].name = 'style';
}
const style = CSSStyleDeclarationElement.getElementStyle(this._ownerElement, false);
style.set(name, value, !!priority);
this._ownerElement['_attributes']['style'].value = style.toString();
} else {
this._style.set(name, value, !!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.
*/
public removeProperty(name: string): void {
if (this._computed) {
throw new 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 = CSSStyleDeclarationElement.getElementStyle(this._ownerElement, false);
style.remove(name);
const newCSSText = style.toString();
if (newCSSText) {
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.
*/
public getPropertyValue(name: string): string {
if (this._ownerElement) {
const style = CSSStyleDeclarationElement.getElementStyle(this._ownerElement, this._computed);
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.
*/
public getPropertyPriority(name: string): string {
if (this._ownerElement) {
const style = CSSStyleDeclarationElement.getElementStyle(this._ownerElement, this._computed);
return style.get(name)?.important ? 'important' : '';
}
return this._style.get(name)?.important ? 'important' : '';
}
}