-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
CSSStyleDeclarationElement.ts
165 lines (147 loc) · 5.65 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
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 ICSSStyleDeclarationPropertyValue from './ICSSStyleDeclarationPropertyValue';
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 CSSStyleDeclarationElementDefaultProperties from './CSSStyleDeclarationElementDefaultProperties';
import CSSStyleDeclarationElementInheritedProperties from './CSSStyleDeclarationElementInheritedProperties';
/**
* 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(element['_attributes']['style']?.value);
}
/**
* Returns style sheets.
*
* @param element Element.
* @returns Style sheets.
*/
private static getComputedElementStyle(element: IElement): CSSStyleDeclarationPropertyManager {
const targetElement: { element: IElement; cssText: string } = { element, cssText: '' };
const parentElements: Array<{ element: IElement; cssText: string }> = [];
const inheritedProperties: { [k: string]: ICSSStyleDeclarationPropertyValue } = {};
let shadowRootElements: Array<{ element: IElement; cssText: string }> = [targetElement];
let currentNode: IElement | IShadowRoot | IDocument = <IElement | IShadowRoot | IDocument>(
element.parentNode
);
if (!element.isConnected) {
return new CSSStyleDeclarationPropertyManager();
}
while (currentNode) {
const styleAndElement = { element: <IElement>currentNode, cssText: '' };
if (currentNode.nodeType === NodeTypeEnum.elementNode) {
parentElements.unshift(styleAndElement);
shadowRootElements.unshift(styleAndElement);
}
currentNode = <IElement | IShadowRoot | IDocument>currentNode.parentNode;
if (currentNode) {
if (currentNode === element.ownerDocument) {
const styleSheets = <INodeList<IHTMLStyleElement>>(
element.ownerDocument.querySelectorAll('style')
);
for (const styleSheet of styleSheets) {
this.applyCSSTextToElements(shadowRootElements, styleSheet.sheet.cssRules);
}
currentNode = null;
} else if ((<IShadowRoot>currentNode).host) {
const styleSheets = <INodeList<IHTMLStyleElement>>(
(<IShadowRoot>currentNode).querySelectorAll('style')
);
for (const styleSheet of styleSheets) {
this.applyCSSTextToElements(shadowRootElements, styleSheet.sheet.cssRules);
}
currentNode = (<IShadowRoot>currentNode).host;
shadowRootElements = [];
}
}
}
for (const parentElement of parentElements) {
const propertyManager = new CSSStyleDeclarationPropertyManager(
parentElement.cssText + (parentElement.element['_attributes']['style']?.value || '')
);
const properties = Object.assign(
{},
CSSStyleDeclarationElementDefaultProperties.default,
CSSStyleDeclarationElementDefaultProperties[parentElement.element.tagName],
propertyManager.properties
);
for (const name of Object.keys(properties)) {
if (CSSStyleDeclarationElementInheritedProperties.includes(name)) {
if (!inheritedProperties[name]?.important || properties[name].important) {
inheritedProperties[name] = properties[name];
}
}
}
}
const targetPropertyManager = new CSSStyleDeclarationPropertyManager(
targetElement.cssText + (targetElement.element['_attributes']['style']?.value || '')
);
const targetProperties = Object.assign(
{},
CSSStyleDeclarationElementDefaultProperties.default,
CSSStyleDeclarationElementDefaultProperties[targetElement.element.tagName],
inheritedProperties
);
for (const name of Object.keys(targetPropertyManager.properties)) {
if (!targetProperties[name]?.important || targetPropertyManager.properties[name].important) {
targetProperties[name] = targetPropertyManager.properties[name];
}
}
targetPropertyManager.properties = targetProperties;
return targetPropertyManager;
}
/**
* Applies CSS text to elements.
*
* @param elements Elements.
* @param cssRules CSS rules.
*/
private static applyCSSTextToElements(
elements: Array<{ element: IElement; cssText: string }>,
cssRules: CSSRule[]
): void {
if (!elements.length) {
return;
}
const defaultView = elements[0].element.ownerDocument.defaultView;
for (const rule of cssRules) {
if (rule.type === CSSRuleTypeEnum.styleRule) {
for (const element of elements) {
const selectorText = (<CSSStyleRule>rule).selectorText;
if (selectorText && element.element.matches(selectorText)) {
const firstBracket = rule.cssText.indexOf('{');
const lastBracket = rule.cssText.lastIndexOf('}');
element.cssText += rule.cssText.substring(firstBracket + 1, lastBracket);
}
}
} else if (
rule.type === CSSRuleTypeEnum.mediaRule &&
defaultView.matchMedia((<CSSMediaRule>rule).conditionalText).matches
) {
this.applyCSSTextToElements(elements, (<CSSMediaRule>rule).cssRules);
}
}
}
}