happy-dom 6.0.4 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of happy-dom might be problematic. Click here for more details.

Files changed (179) hide show
  1. package/lib/css/CSSParser.js +12 -6
  2. package/lib/css/CSSParser.js.map +1 -1
  3. package/lib/css/CSSRule.d.ts +15 -13
  4. package/lib/css/CSSRule.js +18 -13
  5. package/lib/css/CSSRule.js.map +1 -1
  6. package/lib/css/CSSRuleTypeEnum.d.ts +17 -0
  7. package/lib/css/CSSRuleTypeEnum.js +21 -0
  8. package/lib/css/CSSRuleTypeEnum.js.map +1 -0
  9. package/lib/css/CSSStyleSheet.d.ts +6 -16
  10. package/lib/css/CSSStyleSheet.js +11 -19
  11. package/lib/css/CSSStyleSheet.js.map +1 -1
  12. package/lib/css/declaration/AbstractCSSStyleDeclaration.d.ts +76 -0
  13. package/lib/css/declaration/AbstractCSSStyleDeclaration.js +182 -0
  14. package/lib/css/declaration/AbstractCSSStyleDeclaration.js.map +1 -0
  15. package/lib/css/declaration/CSSStyleDeclaration.d.ts +1194 -0
  16. package/lib/css/declaration/CSSStyleDeclaration.js +3566 -0
  17. package/lib/css/declaration/CSSStyleDeclaration.js.map +1 -0
  18. package/lib/css/declaration/utilities/CSSStyleDeclarationCSSParser.d.ts +12 -0
  19. package/lib/css/declaration/utilities/CSSStyleDeclarationCSSParser.js +34 -0
  20. package/lib/css/declaration/utilities/CSSStyleDeclarationCSSParser.js.map +1 -0
  21. package/lib/css/declaration/utilities/CSSStyleDeclarationElementDefaultCSS.d.ts +131 -0
  22. package/lib/css/declaration/utilities/CSSStyleDeclarationElementDefaultCSS.js +133 -0
  23. package/lib/css/declaration/utilities/CSSStyleDeclarationElementDefaultCSS.js.map +1 -0
  24. package/lib/css/declaration/utilities/CSSStyleDeclarationElementInheritedProperties.d.ts +40 -0
  25. package/lib/css/declaration/utilities/CSSStyleDeclarationElementInheritedProperties.js +42 -0
  26. package/lib/css/declaration/utilities/CSSStyleDeclarationElementInheritedProperties.js.map +1 -0
  27. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.d.ts +49 -0
  28. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.js +244 -0
  29. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.js.map +1 -0
  30. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyGetParser.d.ts +167 -0
  31. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyGetParser.js +537 -0
  32. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyGetParser.js.map +1 -0
  33. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyManager.d.ts +65 -0
  34. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyManager.js +565 -0
  35. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertyManager.js.map +1 -0
  36. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertySetParser.d.ts +809 -0
  37. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertySetParser.js +2303 -0
  38. package/lib/css/declaration/utilities/CSSStyleDeclarationPropertySetParser.js.map +1 -0
  39. package/lib/css/declaration/utilities/CSSStyleDeclarationValueParser.d.ts +127 -0
  40. package/lib/css/declaration/utilities/CSSStyleDeclarationValueParser.js +411 -0
  41. package/lib/css/declaration/utilities/CSSStyleDeclarationValueParser.js.map +1 -0
  42. package/lib/css/declaration/utilities/ICSSStyleDeclarationPropertyValue.d.ts +4 -0
  43. package/lib/css/declaration/utilities/ICSSStyleDeclarationPropertyValue.js +3 -0
  44. package/lib/css/declaration/utilities/ICSSStyleDeclarationPropertyValue.js.map +1 -0
  45. package/lib/css/rules/CSSContainerRule.d.ts +15 -0
  46. package/lib/css/rules/CSSContainerRule.js +31 -0
  47. package/lib/css/rules/CSSContainerRule.js.map +1 -0
  48. package/lib/css/rules/CSSFontFaceRule.d.ts +10 -3
  49. package/lib/css/rules/CSSFontFaceRule.js +16 -0
  50. package/lib/css/rules/CSSFontFaceRule.js.map +1 -1
  51. package/lib/css/rules/CSSKeyframeRule.d.ts +10 -3
  52. package/lib/css/rules/CSSKeyframeRule.js +16 -0
  53. package/lib/css/rules/CSSKeyframeRule.js.map +1 -1
  54. package/lib/css/rules/CSSKeyframesRule.d.ts +1 -1
  55. package/lib/css/rules/CSSKeyframesRule.js +1 -1
  56. package/lib/css/rules/CSSKeyframesRule.js.map +1 -1
  57. package/lib/css/rules/CSSMediaRule.d.ts +1 -1
  58. package/lib/css/rules/CSSStyleRule.d.ts +10 -3
  59. package/lib/css/rules/CSSStyleRule.js +16 -0
  60. package/lib/css/rules/CSSStyleRule.js.map +1 -1
  61. package/lib/event/Event.d.ts +2 -3
  62. package/lib/event/Event.js.map +1 -1
  63. package/lib/event/events/IMediaQueryListInit.d.ts +5 -0
  64. package/lib/event/events/IMediaQueryListInit.js +3 -0
  65. package/lib/event/events/IMediaQueryListInit.js.map +1 -0
  66. package/lib/event/events/MediaQueryListEvent.d.ts +16 -0
  67. package/lib/event/events/MediaQueryListEvent.js +28 -0
  68. package/lib/event/events/MediaQueryListEvent.js.map +1 -0
  69. package/lib/exception/DOMException.js +5 -3
  70. package/lib/exception/DOMException.js.map +1 -1
  71. package/lib/exception/DOMExceptionNameEnum.d.ts +2 -1
  72. package/lib/exception/DOMExceptionNameEnum.js +1 -0
  73. package/lib/exception/DOMExceptionNameEnum.js.map +1 -1
  74. package/lib/file/Blob.d.ts +9 -0
  75. package/lib/file/Blob.js +14 -0
  76. package/lib/file/Blob.js.map +1 -1
  77. package/lib/index.d.ts +9 -2
  78. package/lib/index.js +16 -2
  79. package/lib/index.js.map +1 -1
  80. package/lib/match-media/MediaQueryList.d.ts +19 -8
  81. package/lib/match-media/MediaQueryList.js +56 -13
  82. package/lib/match-media/MediaQueryList.js.map +1 -1
  83. package/lib/{attribute → nodes/attr}/Attr.d.ts +8 -7
  84. package/lib/{attribute → nodes/attr}/Attr.js +12 -5
  85. package/lib/nodes/attr/Attr.js.map +1 -0
  86. package/lib/nodes/attr/IAttr.d.ts +14 -0
  87. package/lib/nodes/attr/IAttr.js +3 -0
  88. package/lib/nodes/attr/IAttr.js.map +1 -0
  89. package/lib/nodes/child-node/ChildNodeUtility.js +2 -1
  90. package/lib/nodes/child-node/ChildNodeUtility.js.map +1 -1
  91. package/lib/nodes/document/Document.d.ts +4 -4
  92. package/lib/nodes/document/Document.js +5 -8
  93. package/lib/nodes/document/Document.js.map +1 -1
  94. package/lib/nodes/document/IDocument.d.ts +3 -3
  95. package/lib/nodes/element/Element.d.ts +11 -9
  96. package/lib/nodes/element/Element.js +2 -1
  97. package/lib/nodes/element/Element.js.map +1 -1
  98. package/lib/nodes/element/IElement.d.ts +10 -8
  99. package/lib/nodes/html-element/HTMLElement.d.ts +21 -5
  100. package/lib/nodes/html-element/HTMLElement.js +74 -11
  101. package/lib/nodes/html-element/HTMLElement.js.map +1 -1
  102. package/lib/nodes/html-element/IHTMLElement.d.ts +5 -1
  103. package/lib/nodes/html-link-element/HTMLLinkElement.d.ts +2 -2
  104. package/lib/nodes/html-link-element/HTMLLinkElement.js.map +1 -1
  105. package/lib/nodes/html-script-element/HTMLScriptElement.d.ts +2 -2
  106. package/lib/nodes/html-script-element/HTMLScriptElement.js.map +1 -1
  107. package/lib/nodes/html-select-element/HTMLSelectElement.js +1 -1
  108. package/lib/nodes/html-select-element/HTMLSelectElement.js.map +1 -1
  109. package/lib/nodes/html-style-element/HTMLStyleElement.js +1 -1
  110. package/lib/nodes/html-style-element/HTMLStyleElement.js.map +1 -1
  111. package/lib/nodes/node/INode.d.ts +2 -0
  112. package/lib/nodes/node/Node.d.ts +5 -1
  113. package/lib/nodes/node/Node.js +7 -1
  114. package/lib/nodes/node/Node.js.map +1 -1
  115. package/lib/nodes/node/NodeTypeEnum.d.ts +2 -0
  116. package/lib/nodes/node/NodeTypeEnum.js +2 -0
  117. package/lib/nodes/node/NodeTypeEnum.js.map +1 -1
  118. package/lib/nodes/svg-element/ISVGElement.d.ts +1 -1
  119. package/lib/nodes/svg-element/SVGElement.d.ts +4 -4
  120. package/lib/nodes/svg-element/SVGElement.js +2 -2
  121. package/lib/nodes/svg-element/SVGElement.js.map +1 -1
  122. package/lib/window/IWindow.d.ts +23 -3
  123. package/lib/window/Window.d.ts +35 -6
  124. package/lib/window/Window.js +58 -23
  125. package/lib/window/Window.js.map +1 -1
  126. package/package.json +2 -2
  127. package/src/css/CSSParser.ts +14 -6
  128. package/src/css/CSSRule.ts +15 -13
  129. package/src/css/CSSRuleTypeEnum.ts +18 -0
  130. package/src/css/CSSStyleSheet.ts +12 -22
  131. package/src/css/declaration/AbstractCSSStyleDeclaration.ts +202 -0
  132. package/src/css/declaration/CSSStyleDeclaration.ts +4743 -0
  133. package/src/css/declaration/utilities/CSSStyleDeclarationCSSParser.ts +35 -0
  134. package/src/css/declaration/utilities/CSSStyleDeclarationElementDefaultCSS.ts +130 -0
  135. package/src/css/declaration/utilities/CSSStyleDeclarationElementInheritedProperties.ts +39 -0
  136. package/src/css/declaration/utilities/CSSStyleDeclarationElementStyle.ts +282 -0
  137. package/src/css/declaration/utilities/CSSStyleDeclarationPropertyGetParser.ts +743 -0
  138. package/src/css/declaration/utilities/CSSStyleDeclarationPropertyManager.ts +592 -0
  139. package/src/css/declaration/utilities/CSSStyleDeclarationPropertySetParser.ts +3026 -0
  140. package/src/css/declaration/utilities/CSSStyleDeclarationValueParser.ts +437 -0
  141. package/src/css/declaration/utilities/ICSSStyleDeclarationPropertyValue.ts +4 -0
  142. package/src/css/rules/CSSContainerRule.ts +23 -0
  143. package/src/css/rules/CSSFontFaceRule.ts +17 -2
  144. package/src/css/rules/CSSKeyframeRule.ts +17 -2
  145. package/src/css/rules/CSSKeyframesRule.ts +1 -1
  146. package/src/css/rules/CSSStyleRule.ts +17 -2
  147. package/src/event/Event.ts +2 -3
  148. package/src/event/events/IMediaQueryListInit.ts +6 -0
  149. package/src/event/events/MediaQueryListEvent.ts +25 -0
  150. package/src/exception/DOMException.ts +3 -3
  151. package/src/exception/DOMExceptionNameEnum.ts +2 -1
  152. package/src/file/Blob.ts +16 -0
  153. package/src/index.ts +15 -1
  154. package/src/match-media/MediaQueryList.ts +63 -10
  155. package/src/{attribute → nodes/attr}/Attr.ts +11 -8
  156. package/src/nodes/attr/IAttr.ts +15 -0
  157. package/src/nodes/child-node/ChildNodeUtility.ts +2 -1
  158. package/src/nodes/document/Document.ts +8 -10
  159. package/src/nodes/document/IDocument.ts +3 -3
  160. package/src/nodes/element/Element.ts +13 -10
  161. package/src/nodes/element/IElement.ts +8 -8
  162. package/src/nodes/html-element/HTMLElement.ts +94 -16
  163. package/src/nodes/html-element/IHTMLElement.ts +3 -1
  164. package/src/nodes/html-link-element/HTMLLinkElement.ts +2 -2
  165. package/src/nodes/html-script-element/HTMLScriptElement.ts +2 -2
  166. package/src/nodes/html-select-element/HTMLSelectElement.ts +1 -1
  167. package/src/nodes/html-style-element/HTMLStyleElement.ts +1 -1
  168. package/src/nodes/node/INode.ts +2 -0
  169. package/src/nodes/node/Node.ts +7 -1
  170. package/src/nodes/node/NodeTypeEnum.ts +2 -0
  171. package/src/nodes/svg-element/ISVGElement.ts +1 -1
  172. package/src/nodes/svg-element/SVGElement.ts +5 -5
  173. package/src/window/IWindow.ts +23 -3
  174. package/src/window/Window.ts +51 -11
  175. package/lib/attribute/Attr.js.map +0 -1
  176. package/lib/css/CSSStyleDeclaration.d.ts +0 -3017
  177. package/lib/css/CSSStyleDeclaration.js +0 -4643
  178. package/lib/css/CSSStyleDeclaration.js.map +0 -1
  179. package/src/css/CSSStyleDeclaration.ts +0 -5026
@@ -0,0 +1,35 @@
1
+ /**
2
+ * CSS parser.
3
+ */
4
+ export default class CSSStyleDeclarationCSSParser {
5
+ /**
6
+ * Class construtor.
7
+ *
8
+ * @param cssText CSS string.
9
+ * @param callback Callback.
10
+ */
11
+ public static parse(
12
+ cssText: string,
13
+ callback: (name: string, value: string, important: boolean) => void
14
+ ): void {
15
+ const parts = cssText.split(';');
16
+
17
+ for (const part of parts) {
18
+ if (part) {
19
+ const [name, value]: string[] = part.trim().split(':');
20
+ if (value) {
21
+ const trimmedName = name.trim();
22
+ const trimmedValue = value.trim();
23
+ if (trimmedName && trimmedValue) {
24
+ const important = trimmedValue.endsWith(' !important');
25
+ const valueWithoutImportant = trimmedValue.replace(' !important', '');
26
+
27
+ if (valueWithoutImportant) {
28
+ callback(trimmedName, valueWithoutImportant, important);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,130 @@
1
+ export default {
2
+ default: 'display: inline;',
3
+ A: '',
4
+ ABBR: '',
5
+ ADDRESS: 'display: block;',
6
+ AREA: '',
7
+ ARTICLE: 'display: block;',
8
+ ASIDE: 'display: block;',
9
+ AUDIO: 'display: none;',
10
+ B: '',
11
+ BASE: 'display: none;',
12
+ BDI: '',
13
+ BDO: '',
14
+ BLOCKQUAOTE: '',
15
+ BODY: 'display: block;',
16
+ TEMPLATE: 'display: none;',
17
+ FORM: 'display: block;',
18
+ INPUT: 'display: inline-block;',
19
+ TEXTAREA: 'display: inline-block;',
20
+ SCRIPT: 'display: none;',
21
+ IMG: '',
22
+ LINK: 'display: none;',
23
+ STYLE: 'display: none;',
24
+ LABEL: '',
25
+ SLOT: 'display: contents;',
26
+ SVG: '',
27
+ CIRCLE: '',
28
+ ELLIPSE: '',
29
+ LINE: '',
30
+ PATH: '',
31
+ POLYGON: '',
32
+ POLYLINE: '',
33
+ RECT: '',
34
+ STOP: '',
35
+ USE: '',
36
+ META: 'display: none;',
37
+ BLOCKQUOTE: 'display: block;',
38
+ BR: '',
39
+ BUTTON: 'display: inline-block;',
40
+ CANVAS: '',
41
+ CAPTION: 'display: table-caption;',
42
+ CITE: '',
43
+ CODE: '',
44
+ COL: 'display: table-column;',
45
+ COLGROUP: 'display: table-column-group;',
46
+ DATA: '',
47
+ DATALIST: 'display: none;',
48
+ DD: 'display: block;',
49
+ DEL: '',
50
+ DETAILS: 'display: block;',
51
+ DFN: '',
52
+ DIALOG: 'display: none;',
53
+ DIV: 'display: block;',
54
+ DL: 'display: block;',
55
+ DT: 'display: block;',
56
+ EM: '',
57
+ EMBED: '',
58
+ FIELDSET: 'display: block;',
59
+ FIGCAPTION: 'display: block;',
60
+ FIGURE: 'display: block;',
61
+ FOOTER: 'display: block;',
62
+ H1: 'display: block;',
63
+ H2: 'display: block;',
64
+ H3: 'display: block;',
65
+ H4: 'display: block;',
66
+ H5: 'display: block;',
67
+ H6: 'display: block;',
68
+ HEAD: 'display: none;',
69
+ HEADER: 'display: block;',
70
+ HGROUP: 'display: block;',
71
+ HR: 'display: block;',
72
+ HTML: 'display: block;direction: ltr;',
73
+ I: '',
74
+ IFRAME: '',
75
+ INS: '',
76
+ KBD: '',
77
+ LEGEND: 'display: block;',
78
+ LI: 'display: list-item;',
79
+ MAIN: 'display: block;',
80
+ MAP: '',
81
+ MARK: '',
82
+ MATH: '',
83
+ MENU: 'display: block;',
84
+ MENUITEM: '',
85
+ METER: 'display: inline-block;',
86
+ NAV: 'display: block;',
87
+ NOSCRIPT: '',
88
+ OBJECT: '',
89
+ OL: 'display: block;',
90
+ OPTGROUP: 'display: block;',
91
+ OPTION: 'display: block;',
92
+ OUTPUT: 'unicode-bidi: isolate;',
93
+ P: 'display: block;',
94
+ PARAM: 'display: none;',
95
+ PICTURE: '',
96
+ PRE: 'display: block;',
97
+ PROGRESS: 'display: inline-block;',
98
+ Q: '',
99
+ RB: '',
100
+ RP: 'display: none;',
101
+ RT: '',
102
+ RTC: '',
103
+ RUBY: '',
104
+ S: '',
105
+ SAMP: '',
106
+ SECTION: 'display: block;',
107
+ SELECT: 'display: inline-block;',
108
+ SMALL: '',
109
+ SOURCE: '',
110
+ SPAN: '',
111
+ STRONG: '',
112
+ SUB: '',
113
+ SUMMARY: 'display: block;',
114
+ SUP: '',
115
+ TABLE: 'display: table;',
116
+ TBODY: 'display: table-row-group;',
117
+ TD: 'display: table-cell;',
118
+ TFOOT: 'display: table-footer-group;',
119
+ TH: 'display: table-cell;',
120
+ THEAD: 'display: table-header-group;',
121
+ TIME: '',
122
+ TITLE: 'display: none;',
123
+ TR: 'display: table-row;',
124
+ TRACK: '',
125
+ U: '',
126
+ UL: 'display: block;',
127
+ VAR: '',
128
+ VIDEO: '',
129
+ WBR: ''
130
+ };
@@ -0,0 +1,39 @@
1
+ export default {
2
+ 'border-collapse': true,
3
+ 'border-spacing': true,
4
+ 'caption-side': true,
5
+ color: true,
6
+ cursor: true,
7
+ direction: true,
8
+ 'empty-cells': true,
9
+ 'font-family': true,
10
+ 'font-size': true,
11
+ 'font-style': true,
12
+ 'font-variant': true,
13
+ 'font-weight': true,
14
+ 'font-size-adjust': true,
15
+ 'font-stretch': true,
16
+ font: true,
17
+ 'letter-spacing': true,
18
+ 'line-height': true,
19
+ 'list-style-image': true,
20
+ 'list-style-position': true,
21
+ 'list-style-type': true,
22
+ 'list-style': true,
23
+ orphans: true,
24
+ quotes: true,
25
+ 'tab-size': true,
26
+ 'text-align': true,
27
+ 'text-align-last': true,
28
+ 'text-decoration-color': true,
29
+ 'text-indent': true,
30
+ 'text-justify': true,
31
+ 'text-shadow': true,
32
+ 'text-transform': true,
33
+ visibility: true,
34
+ 'white-space': true,
35
+ widows: true,
36
+ 'word-break': true,
37
+ 'word-spacing': true,
38
+ 'word-wrap': true
39
+ };
@@ -0,0 +1,282 @@
1
+ import IShadowRoot from '../../../nodes/shadow-root/IShadowRoot';
2
+ import IElement from '../../../nodes/element/IElement';
3
+ import IDocument from '../../../nodes/document/IDocument';
4
+ import IHTMLStyleElement from '../../../nodes/html-style-element/IHTMLStyleElement';
5
+ import INodeList from '../../../nodes/node/INodeList';
6
+ import CSSStyleDeclarationPropertyManager from './CSSStyleDeclarationPropertyManager';
7
+ import NodeTypeEnum from '../../../nodes/node/NodeTypeEnum';
8
+ import CSSRuleTypeEnum from '../../CSSRuleTypeEnum';
9
+ import CSSMediaRule from '../../rules/CSSMediaRule';
10
+ import CSSRule from '../../CSSRule';
11
+ import CSSStyleRule from '../../rules/CSSStyleRule';
12
+ import CSSStyleDeclarationElementDefaultCSS from './CSSStyleDeclarationElementDefaultCSS';
13
+ import CSSStyleDeclarationElementInheritedProperties from './CSSStyleDeclarationElementInheritedProperties';
14
+ import CSSStyleDeclarationCSSParser from './CSSStyleDeclarationCSSParser';
15
+
16
+ const CSS_VARIABLE_REGEXP = /var\( *(--[^) ]+)\)/g;
17
+
18
+ /**
19
+ * CSS Style Declaration utility
20
+ */
21
+ export default class CSSStyleDeclarationElementStyle {
22
+ private cache: { [k: string]: CSSStyleDeclarationPropertyManager } = {};
23
+ private element: IElement;
24
+ private computed: boolean;
25
+
26
+ /**
27
+ * Constructor.
28
+ *
29
+ * @param element Element.
30
+ * @param [computed] Computed.
31
+ */
32
+ constructor(element: IElement, computed = false) {
33
+ this.element = element;
34
+ this.computed = computed;
35
+ }
36
+
37
+ /**
38
+ * Returns element style properties.
39
+ *
40
+ * @returns Element style properties.
41
+ */
42
+ public getElementStyle(): CSSStyleDeclarationPropertyManager {
43
+ if (this.computed) {
44
+ return this.getComputedElementStyle();
45
+ }
46
+
47
+ const cssText = this.element['_attributes']['style']?.value;
48
+
49
+ if (cssText) {
50
+ if (this.cache[cssText]) {
51
+ return this.cache[cssText];
52
+ }
53
+ this.cache[cssText] = new CSSStyleDeclarationPropertyManager({ cssText });
54
+ return this.cache[cssText];
55
+ }
56
+
57
+ return new CSSStyleDeclarationPropertyManager();
58
+ }
59
+
60
+ /**
61
+ * Returns style sheets.
62
+ *
63
+ * @param element Element.
64
+ * @returns Style sheets.
65
+ */
66
+ private getComputedElementStyle(): CSSStyleDeclarationPropertyManager {
67
+ const documentElements: Array<{ element: IElement; cssText: string }> = [];
68
+ const parentElements: Array<{ element: IElement; cssText: string }> = [];
69
+ let styleAndElement = {
70
+ element: <IElement | IShadowRoot | IDocument>this.element,
71
+ cssText: ''
72
+ };
73
+ let shadowRootElements: Array<{ element: IElement; cssText: string }> = [];
74
+
75
+ if (!this.element.isConnected) {
76
+ return new CSSStyleDeclarationPropertyManager();
77
+ }
78
+
79
+ // Walks through all parent elements and stores them in an array with element and matching CSS text.
80
+ while (styleAndElement.element) {
81
+ if (styleAndElement.element.nodeType === NodeTypeEnum.elementNode) {
82
+ const rootNode = styleAndElement.element.getRootNode();
83
+ if (rootNode.nodeType === NodeTypeEnum.documentNode) {
84
+ documentElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
85
+ } else {
86
+ shadowRootElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
87
+ }
88
+ parentElements.unshift(<{ element: IElement; cssText: string }>styleAndElement);
89
+ }
90
+
91
+ if (styleAndElement.element === this.element.ownerDocument) {
92
+ const styleSheets = <INodeList<IHTMLStyleElement>>(
93
+ this.element.ownerDocument.querySelectorAll('style,link[rel="stylesheet"]')
94
+ );
95
+
96
+ for (const styleSheet of styleSheets) {
97
+ const sheet = styleSheet.sheet;
98
+ if (sheet) {
99
+ this.parseCSSRules({
100
+ elements: documentElements,
101
+ cssRules: sheet.cssRules
102
+ });
103
+ }
104
+ }
105
+
106
+ styleAndElement = { element: null, cssText: '' };
107
+ } else if ((<IShadowRoot>styleAndElement.element).host) {
108
+ const styleSheets = <INodeList<IHTMLStyleElement>>(
109
+ (<IShadowRoot>styleAndElement.element).querySelectorAll('style,link[rel="stylesheet"]')
110
+ );
111
+
112
+ styleAndElement = {
113
+ element: <IElement>(<IShadowRoot>styleAndElement.element).host,
114
+ cssText: ''
115
+ };
116
+
117
+ for (const styleSheet of styleSheets) {
118
+ const sheet = styleSheet.sheet;
119
+ if (sheet) {
120
+ this.parseCSSRules({
121
+ elements: shadowRootElements,
122
+ cssRules: sheet.cssRules,
123
+ hostElement: <{ element: IElement; cssText: string }>styleAndElement
124
+ });
125
+ }
126
+ }
127
+ shadowRootElements = [];
128
+ } else {
129
+ styleAndElement = { element: <IElement>styleAndElement.element.parentNode, cssText: '' };
130
+ }
131
+ }
132
+
133
+ // Concatenates all parent element CSS to one string.
134
+ const targetElement = parentElements[parentElements.length - 1];
135
+ let inheritedCSSText = CSSStyleDeclarationElementDefaultCSS.default;
136
+
137
+ for (const parentElement of parentElements) {
138
+ if (parentElement !== targetElement) {
139
+ inheritedCSSText +=
140
+ (CSSStyleDeclarationElementDefaultCSS[parentElement.element.tagName] || '') +
141
+ parentElement.cssText +
142
+ (parentElement.element['_attributes']['style']?.value || '');
143
+ }
144
+ }
145
+
146
+ const cssVariables: { [k: string]: string } = {};
147
+ const properties = {};
148
+ const targetCSSText =
149
+ (CSSStyleDeclarationElementDefaultCSS[targetElement.element.tagName] || '') +
150
+ targetElement.cssText +
151
+ (targetElement.element['_attributes']['style']?.value || '');
152
+ const combinedCSSText = inheritedCSSText + targetCSSText;
153
+
154
+ if (this.cache[combinedCSSText]) {
155
+ return this.cache[combinedCSSText];
156
+ }
157
+
158
+ // Parses the parent element CSS and stores CSS variables and inherited properties.
159
+ CSSStyleDeclarationCSSParser.parse(inheritedCSSText, (name, value, important) => {
160
+ if (name.startsWith('--')) {
161
+ const cssValue = this.getCSSValue(value, cssVariables);
162
+ if (cssValue) {
163
+ cssVariables[name] = cssValue;
164
+ }
165
+ return;
166
+ }
167
+
168
+ if (CSSStyleDeclarationElementInheritedProperties[name]) {
169
+ const cssValue = this.getCSSValue(value, cssVariables);
170
+ if (cssValue && (!properties[name]?.important || important)) {
171
+ properties[name] = {
172
+ value: cssValue,
173
+ important
174
+ };
175
+ }
176
+ }
177
+ });
178
+
179
+ // Parses the target element CSS.
180
+ CSSStyleDeclarationCSSParser.parse(targetCSSText, (name, value, important) => {
181
+ if (name.startsWith('--')) {
182
+ const cssValue = this.getCSSValue(value, cssVariables);
183
+ if (cssValue && (!properties[name]?.important || important)) {
184
+ cssVariables[name] = cssValue;
185
+ properties[name] = {
186
+ value,
187
+ important
188
+ };
189
+ }
190
+ } else {
191
+ const cssValue = this.getCSSValue(value, cssVariables);
192
+ if (cssValue && (!properties[name]?.important || important)) {
193
+ properties[name] = {
194
+ value: cssValue,
195
+ important
196
+ };
197
+ }
198
+ }
199
+ });
200
+
201
+ const propertyManager = new CSSStyleDeclarationPropertyManager();
202
+
203
+ for (const name of Object.keys(properties)) {
204
+ propertyManager.set(name, properties[name].value, properties[name].important);
205
+ }
206
+
207
+ this.cache[combinedCSSText] = propertyManager;
208
+
209
+ return propertyManager;
210
+ }
211
+
212
+ /**
213
+ * Applies CSS text to elements.
214
+ *
215
+ * @param options Options.
216
+ * @param options.elements Elements.
217
+ * @param options.cssRules CSS rules.
218
+ * @param [options.hostElement] Host element.
219
+ * @param [options.hostElement.element] Element.
220
+ * @param [options.hostElement.cssText] CSS text.
221
+ */
222
+ private parseCSSRules(options: {
223
+ cssRules: CSSRule[];
224
+ elements: Array<{ element: IElement; cssText: string }>;
225
+ hostElement?: { element: IElement; cssText: string };
226
+ }): void {
227
+ if (!options.elements.length) {
228
+ return;
229
+ }
230
+
231
+ const defaultView = options.elements[0].element.ownerDocument.defaultView;
232
+
233
+ for (const rule of options.cssRules) {
234
+ if (rule.type === CSSRuleTypeEnum.styleRule) {
235
+ const selectorText: string = (<CSSStyleRule>rule).selectorText;
236
+ if (selectorText) {
237
+ if (selectorText.startsWith(':host')) {
238
+ if (options.hostElement) {
239
+ options.hostElement.cssText += (<CSSStyleRule>rule)._cssText;
240
+ }
241
+ } else {
242
+ for (const element of options.elements) {
243
+ if (element.element.matches(selectorText)) {
244
+ element.cssText += (<CSSStyleRule>rule)._cssText;
245
+ }
246
+ }
247
+ }
248
+ }
249
+ } else if (
250
+ rule.type === CSSRuleTypeEnum.mediaRule &&
251
+ defaultView.matchMedia((<CSSMediaRule>rule).conditionalText).matches
252
+ ) {
253
+ this.parseCSSRules({
254
+ elements: options.elements,
255
+ cssRules: (<CSSMediaRule>rule).cssRules,
256
+ hostElement: options.hostElement
257
+ });
258
+ }
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Returns CSS value.
264
+ *
265
+ * @param value Value.
266
+ * @param cssVariables CSS variables.
267
+ * @returns CSS value.
268
+ */
269
+ private getCSSValue(value: string, cssVariables: { [k: string]: string }): string {
270
+ const regexp = new RegExp(CSS_VARIABLE_REGEXP);
271
+ let newValue = value;
272
+ let match;
273
+ while ((match = regexp.exec(value)) !== null) {
274
+ const cssVariableValue = cssVariables[match[1]];
275
+ if (!cssVariableValue) {
276
+ return null;
277
+ }
278
+ newValue = newValue.replace(match[0], cssVariableValue);
279
+ }
280
+ return newValue;
281
+ }
282
+ }