-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
CSSStyleDeclarationElement.ts
255 lines (230 loc) · 8.09 KB
/
CSSStyleDeclarationElement.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import IShadowRoot from '../../../nodes/shadow-root/IShadowRoot';
import IElement from '../../../nodes/element/IElement';
import IDocument from '../../../nodes/document/IDocument';
import IHTMLStyleElement from '../../../nodes/html-style-element/IHTMLStyleElement';
import INodeList from '../../../nodes/node/INodeList';
import CSSStyleDeclarationPropertyManager from './CSSStyleDeclarationPropertyManager';
import NodeTypeEnum from '../../../nodes/node/NodeTypeEnum';
import CSSRuleTypeEnum from '../../CSSRuleTypeEnum';
import CSSMediaRule from '../../rules/CSSMediaRule';
import CSSRule from '../../CSSRule';
import CSSStyleRule from '../../rules/CSSStyleRule';
import CSSStyleDeclarationElementDefaultCSS from './CSSStyleDeclarationElementDefaultCSS';
import CSSStyleDeclarationElementInheritedProperties from './CSSStyleDeclarationElementInheritedProperties';
import CSSStyleDeclarationCSSParser from './CSSStyleDeclarationCSSParser';
const CSS_VARIABLE_REGEXP = /var\( *(--[^) ]+)\)/g;
/**
* CSS Style Declaration utility
*/
export default class CSSStyleDeclarationElement {
/**
* Returns element style properties.
*
* @param element Element.
* @param [computed] Computed.
* @returns Element style properties.
*/
public static getElementStyle(
element: IElement,
computed: boolean
): CSSStyleDeclarationPropertyManager {
if (computed) {
return this.getComputedElementStyle(element);
}
return new CSSStyleDeclarationPropertyManager({
cssText: element['_attributes']['style']?.value
});
}
/**
* Returns style sheets.
*
* @param element Element.
* @returns Style sheets.
*/
private static getComputedElementStyle(element: IElement): CSSStyleDeclarationPropertyManager {
const documentElements: Array<{ element: IElement; cssText: string }> = [];
const parentElements: Array<{ element: IElement; cssText: string }> = [];
let styleAndElement = { element: <IElement | IShadowRoot | IDocument>element, cssText: '' };
let shadowRootElements: Array<{ element: IElement; cssText: string }> = [];
if (!element.isConnected) {
return new CSSStyleDeclarationPropertyManager();
}
// Walks through all parent elements and stores them in an array with element and matching CSS text.
while (styleAndElement.element) {
if (styleAndElement.element.nodeType === NodeTypeEnum.elementNode) {
const rootNode = styleAndElement.element.getRootNode();
if (rootNode.nodeType === NodeTypeEnum.documentNode) {
documentElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
} else {
shadowRootElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
}
parentElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
}
if (styleAndElement.element === element.ownerDocument) {
const styleSheets = <INodeList<IHTMLStyleElement>>(
element.ownerDocument.querySelectorAll('style,link[rel="stylesheet"]')
);
for (const styleSheet of styleSheets) {
const sheet = styleSheet.sheet;
if (sheet) {
this.parseCSSRules({
elements: documentElements,
cssRules: sheet.cssRules
});
}
}
styleAndElement = { element: null, cssText: '' };
} else if ((<IShadowRoot>styleAndElement.element).host) {
const styleSheets = <INodeList<IHTMLStyleElement>>(
(<IShadowRoot>styleAndElement.element).querySelectorAll('style,link[rel="stylesheet"]')
);
styleAndElement = {
element: <IElement>(<IShadowRoot>styleAndElement.element).host,
cssText: ''
};
for (const styleSheet of styleSheets) {
const sheet = styleSheet.sheet;
if (sheet) {
this.parseCSSRules({
elements: shadowRootElements,
cssRules: sheet.cssRules,
hostElement: <{ element: IElement; cssText: string }>styleAndElement
});
}
}
shadowRootElements = [];
} else {
styleAndElement = { element: <IElement>styleAndElement.element.parentNode, cssText: '' };
}
}
// Concatenates all parent element CSS to one string.
const targetElement = parentElements[parentElements.length - 1];
let inheritedCSSText = CSSStyleDeclarationElementDefaultCSS.default;
for (const parentElement of parentElements) {
if (parentElement !== targetElement) {
inheritedCSSText +=
(CSSStyleDeclarationElementDefaultCSS[parentElement.element.tagName] || '') +
parentElement.cssText +
(parentElement.element['_attributes']['style']?.value || '');
}
}
const cssVariables: { [k: string]: string } = {};
const properties = {};
// Parses the parent element CSS and stores CSS variables and inherited properties.
CSSStyleDeclarationCSSParser.parse(inheritedCSSText, (name, value, important) => {
if (name.startsWith('--')) {
const cssValue = this.getCSSValue(value, cssVariables);
if (cssValue) {
cssVariables[name] = cssValue;
}
return;
}
if (CSSStyleDeclarationElementInheritedProperties[name]) {
const cssValue = this.getCSSValue(value, cssVariables);
if (cssValue && (!properties[name]?.important || important)) {
properties[name] = {
value: cssValue,
important
};
}
}
});
// Parses the target element CSS.
const targetCSSText =
(CSSStyleDeclarationElementDefaultCSS[targetElement.element.tagName] || '') +
targetElement.cssText +
(targetElement.element['_attributes']['style']?.value || '');
CSSStyleDeclarationCSSParser.parse(targetCSSText, (name, value, important) => {
if (name.startsWith('--')) {
const cssValue = this.getCSSValue(value, cssVariables);
if (cssValue && (!properties[name]?.important || important)) {
cssVariables[name] = cssValue;
properties[name] = {
value,
important
};
}
} else {
const cssValue = this.getCSSValue(value, cssVariables);
if (cssValue && (!properties[name]?.important || important)) {
properties[name] = {
value: cssValue,
important
};
}
}
});
const propertyManager = new CSSStyleDeclarationPropertyManager();
for (const name of Object.keys(properties)) {
propertyManager.set(name, properties[name].value, properties[name].important);
}
return propertyManager;
}
/**
* Applies CSS text to elements.
*
* @param options Options.
* @param options.elements Elements.
* @param options.cssRules CSS rules.
* @param [options.hostElement] Host element.
* @param [options.hostElement.element] Element.
* @param [options.hostElement.cssText] CSS text.
*/
private static parseCSSRules(options: {
cssRules: CSSRule[];
elements: Array<{ element: IElement; cssText: string }>;
hostElement?: { element: IElement; cssText: string };
}): void {
if (!options.elements.length) {
return;
}
const defaultView = options.elements[0].element.ownerDocument.defaultView;
for (const rule of options.cssRules) {
if (rule.type === CSSRuleTypeEnum.styleRule) {
const selectorText: string = (<CSSStyleRule>rule).selectorText;
if (selectorText) {
if (selectorText.startsWith(':host')) {
if (options.hostElement) {
options.hostElement.cssText += (<CSSStyleRule>rule)._cssText;
}
} else {
for (const element of options.elements) {
if (element.element.matches(selectorText)) {
element.cssText += (<CSSStyleRule>rule)._cssText;
}
}
}
}
} else if (
rule.type === CSSRuleTypeEnum.mediaRule &&
defaultView.matchMedia((<CSSMediaRule>rule).conditionalText).matches
) {
this.parseCSSRules({
elements: options.elements,
cssRules: (<CSSMediaRule>rule).cssRules,
hostElement: options.hostElement
});
}
}
}
/**
* Returns CSS value.
*
* @param value Value.
* @param cssVariables CSS variables.
* @returns CSS value.
*/
private static getCSSValue(value: string, cssVariables: { [k: string]: string }): string {
const regexp = new RegExp(CSS_VARIABLE_REGEXP);
let newValue = value;
let match;
while ((match = regexp.exec(value)) !== null) {
const cssVariableValue = cssVariables[match[1]];
if (!cssVariableValue) {
return null;
}
newValue = newValue.replace(match[0], cssVariableValue);
}
return value !== newValue ? newValue : value;
}
}