happy-dom 7.4.0 → 7.5.1

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 (34) hide show
  1. package/lib/css/declaration/AbstractCSSStyleDeclaration.js +9 -0
  2. package/lib/css/declaration/AbstractCSSStyleDeclaration.js.map +1 -1
  3. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.d.ts +0 -2
  4. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.js +54 -25
  5. package/lib/css/declaration/utilities/CSSStyleDeclarationElementStyle.js.map +1 -1
  6. package/lib/nodes/character-data/CharacterData.js +3 -0
  7. package/lib/nodes/character-data/CharacterData.js.map +1 -1
  8. package/lib/nodes/document/Document.d.ts +1 -0
  9. package/lib/nodes/document/Document.js +2 -0
  10. package/lib/nodes/document/Document.js.map +1 -1
  11. package/lib/nodes/element/Element.js +7 -7
  12. package/lib/nodes/element/Element.js.map +1 -1
  13. package/lib/nodes/node/Node.d.ts +6 -0
  14. package/lib/nodes/node/Node.js +17 -0
  15. package/lib/nodes/node/Node.js.map +1 -1
  16. package/lib/nodes/node/NodeList.d.ts +6 -0
  17. package/lib/nodes/node/NodeList.js +8 -0
  18. package/lib/nodes/node/NodeList.js.map +1 -1
  19. package/lib/query-selector/QuerySelector.d.ts +21 -0
  20. package/lib/query-selector/QuerySelector.js +45 -4
  21. package/lib/query-selector/QuerySelector.js.map +1 -1
  22. package/lib/query-selector/SelectorItem.d.ts +14 -11
  23. package/lib/query-selector/SelectorItem.js +35 -19
  24. package/lib/query-selector/SelectorItem.js.map +1 -1
  25. package/package.json +2 -2
  26. package/src/css/declaration/AbstractCSSStyleDeclaration.ts +12 -0
  27. package/src/css/declaration/utilities/CSSStyleDeclarationElementStyle.ts +86 -36
  28. package/src/nodes/character-data/CharacterData.ts +4 -0
  29. package/src/nodes/document/Document.ts +4 -0
  30. package/src/nodes/element/Element.ts +9 -7
  31. package/src/nodes/node/Node.ts +21 -0
  32. package/src/nodes/node/NodeList.ts +9 -0
  33. package/src/query-selector/QuerySelector.ts +66 -5
  34. package/src/query-selector/SelectorItem.ts +66 -37
@@ -1,4 +1,5 @@
1
1
  import DOMException from '../exception/DOMException';
2
+ import IElement from '../nodes/element/IElement';
2
3
  import Element from '../nodes/element/Element';
3
4
 
4
5
  const ATTRIBUTE_REGEXP =
@@ -13,12 +14,12 @@ const ID_REGEXP = /#[A-Za-z][-A-Za-z0-9_]*/g;
13
14
  * Selector item.
14
15
  */
15
16
  export default class SelectorItem {
16
- public isAll: boolean;
17
- public isID: boolean;
18
- public isAttribute: boolean;
19
- public isPseudo: boolean;
20
- public isClass: boolean;
21
- public isTagName: boolean;
17
+ private isAll: boolean;
18
+ private isID: boolean;
19
+ private isAttribute: boolean;
20
+ private isPseudo: boolean;
21
+ private isClass: boolean;
22
+ private isTagName: boolean;
22
23
  private tagName = null;
23
24
  private selector: string;
24
25
  private id: string;
@@ -42,6 +43,7 @@ export default class SelectorItem {
42
43
  this.isTagName = this.tagName !== null;
43
44
  this.selector = selector;
44
45
  this.id = null;
46
+
45
47
  if (!this.isAll && this.isID) {
46
48
  const idMatches = baseSelector.match(ID_REGEXP);
47
49
  if (idMatches) {
@@ -54,46 +56,60 @@ export default class SelectorItem {
54
56
  * Matches a selector against an element.
55
57
  *
56
58
  * @param element HTML element.
57
- * @returns TRUE if matching.
59
+ * @returns Result.
58
60
  */
59
- public match(element: Element): boolean {
61
+ public match(element: IElement): { priorityWeight: number; matches: boolean } {
60
62
  const selector = this.selector;
61
63
 
64
+ let priorityWeight = 0;
65
+
62
66
  // Is all (*)
63
67
  if (this.isAll) {
64
- return true;
68
+ return { priorityWeight: 0, matches: true };
65
69
  }
66
70
 
67
71
  // ID Match
68
72
  if (this.isID) {
73
+ priorityWeight += 100;
74
+
69
75
  if (this.id !== element.id) {
70
- return false;
76
+ return { priorityWeight: 0, matches: false };
71
77
  }
72
78
  }
73
79
 
74
80
  // Tag name match
75
81
  if (this.isTagName) {
82
+ priorityWeight += 1;
83
+
76
84
  if (this.tagName !== element.tagName) {
77
- return false;
85
+ return { priorityWeight: 0, matches: false };
78
86
  }
79
87
  }
80
88
 
81
89
  // Class match
82
- if (this.isClass && !this.matchesClass(element, selector)) {
83
- return false;
90
+ if (this.isClass) {
91
+ const result = this.matchesClass(element, selector);
92
+ priorityWeight += result.priorityWeight;
93
+ if (!result.matches) {
94
+ return { priorityWeight: 0, matches: false };
95
+ }
84
96
  }
85
97
 
86
98
  // Pseudo match
87
99
  if (this.isPseudo && !this.matchesPsuedo(element, selector)) {
88
- return false;
100
+ return { priorityWeight: 0, matches: false };
89
101
  }
90
102
 
91
103
  // Attribute match
92
- if (this.isAttribute && !this.matchesAttribute(element, selector)) {
93
- return false;
104
+ if (this.isAttribute) {
105
+ const result = this.matchesAttribute(element, selector);
106
+ priorityWeight += result.priorityWeight;
107
+ if (!result.matches) {
108
+ return { priorityWeight: 0, matches: false };
109
+ }
94
110
  }
95
111
 
96
- return true;
112
+ return { priorityWeight, matches: true };
97
113
  }
98
114
 
99
115
  /**
@@ -103,7 +119,7 @@ export default class SelectorItem {
103
119
  * @param selector Selector.
104
120
  * @returns True if it is a match.
105
121
  */
106
- private matchesPsuedo(element: Element, selector: string): boolean {
122
+ private matchesPsuedo(element: IElement, selector: string): boolean {
107
123
  const regexp = new RegExp(PSUEDO_REGEXP, 'g');
108
124
  let match: RegExpMatchArray;
109
125
 
@@ -113,8 +129,8 @@ export default class SelectorItem {
113
129
  return false;
114
130
  } else if (
115
131
  match[3] &&
116
- ((isNotClass && this.matchesClass(element, match[3])) ||
117
- (!isNotClass && this.matchesAttribute(element, match[3])))
132
+ ((isNotClass && this.matchesClass(element, match[3]).matches) ||
133
+ (!isNotClass && this.matchesAttribute(element, match[3])).matches)
118
134
  ) {
119
135
  return false;
120
136
  } else if (match[4] && !this.matchesPsuedoExpression(element, match[4])) {
@@ -133,8 +149,8 @@ export default class SelectorItem {
133
149
  * @param place Place.
134
150
  * @returns True if it is a match.
135
151
  */
136
- private matchesNthChild(element: Element, psuedo: string, place: string): boolean {
137
- let children = element.parentNode ? (<Element>element.parentNode).children : [];
152
+ private matchesNthChild(element: IElement, psuedo: string, place: string): boolean {
153
+ let children = element.parentNode ? (<IElement>element.parentNode).children : [];
138
154
 
139
155
  switch (psuedo.toLowerCase()) {
140
156
  case 'nth-of-type':
@@ -188,8 +204,8 @@ export default class SelectorItem {
188
204
  * @param psuedo Psuedo name.
189
205
  * @returns True if it is a match.
190
206
  */
191
- private matchesPsuedoExpression(element: Element, psuedo: string): boolean {
192
- const parent = <Element>element.parentNode;
207
+ private matchesPsuedoExpression(element: IElement, psuedo: string): boolean {
208
+ const parent = <IElement>element.parentNode;
193
209
 
194
210
  if (!parent) {
195
211
  return false;
@@ -240,24 +256,31 @@ export default class SelectorItem {
240
256
  *
241
257
  * @param element Element.
242
258
  * @param selector Selector.
243
- * @returns True if it is a match.
259
+ * @returns Result.
244
260
  */
245
- private matchesAttribute(element: Element, selector: string): boolean {
261
+ private matchesAttribute(
262
+ element: IElement,
263
+ selector: string
264
+ ): { priorityWeight: number; matches: boolean } {
246
265
  const regexp = new RegExp(ATTRIBUTE_REGEXP, 'g');
247
266
  let match: RegExpMatchArray;
267
+ let priorityWeight = 0;
248
268
 
249
269
  while ((match = regexp.exec(selector))) {
250
270
  const isPsuedo = match.index > 0 && selector[match.index - 1] === '(';
271
+
272
+ priorityWeight += 10;
273
+
251
274
  if (
252
275
  !isPsuedo &&
253
276
  ((match[1] && !this.matchesAttributeName(element, match[1])) ||
254
277
  (match[2] && !this.matchesAttributeNameAndValue(element, match[2], match[4], match[3])))
255
278
  ) {
256
- return false;
279
+ return { priorityWeight: 0, matches: false };
257
280
  }
258
281
  }
259
282
 
260
- return true;
283
+ return { priorityWeight, matches: true };
261
284
  }
262
285
 
263
286
  /**
@@ -265,20 +288,26 @@ export default class SelectorItem {
265
288
  *
266
289
  * @param element Element.
267
290
  * @param selector Selector.
268
- * @returns True if it is a match.
291
+ * @returns Result.
269
292
  */
270
- private matchesClass(element: Element, selector: string): boolean {
293
+ private matchesClass(
294
+ element: IElement,
295
+ selector: string
296
+ ): { priorityWeight: number; matches: boolean } {
271
297
  const regexp = new RegExp(CLASS_REGEXP, 'g');
272
298
  const classList = element.className.split(' ');
299
+ const classSelector = selector.split(':')[0];
300
+ let priorityWeight = 0;
273
301
  let match: RegExpMatchArray;
274
302
 
275
- while ((match = regexp.exec(selector.split(':')[0]))) {
303
+ while ((match = regexp.exec(classSelector))) {
304
+ priorityWeight += 10;
276
305
  if (!classList.includes(match[1])) {
277
- return false;
306
+ return { priorityWeight: 0, matches: false };
278
307
  }
279
308
  }
280
309
 
281
- return true;
310
+ return { priorityWeight, matches: true };
282
311
  }
283
312
 
284
313
  /**
@@ -288,12 +317,12 @@ export default class SelectorItem {
288
317
  * @param attributeName Attribute name.
289
318
  * @returns True if it is a match.
290
319
  */
291
- private matchesAttributeName(element: Element, attributeName: string): boolean {
320
+ private matchesAttributeName(element: IElement, attributeName: string): boolean {
292
321
  if (ATTRIBUTE_NAME_REGEXP.test(attributeName)) {
293
322
  throw new DOMException(`The selector "${this.selector}" is not valid.`);
294
323
  }
295
324
 
296
- return !!element._attributes[attributeName.toLowerCase()];
325
+ return !!(<Element>element)._attributes[attributeName.toLowerCase()];
297
326
  }
298
327
 
299
328
  /** .
@@ -314,12 +343,12 @@ export default class SelectorItem {
314
343
  * @param matchType
315
344
  */
316
345
  private matchesAttributeNameAndValue(
317
- element: Element,
346
+ element: IElement,
318
347
  attributeName: string,
319
348
  attributeValue: string,
320
349
  matchType: string = null
321
350
  ): boolean {
322
- const attribute = element._attributes[attributeName.toLowerCase()];
351
+ const attribute = (<Element>element)._attributes[attributeName.toLowerCase()];
323
352
  const value = attributeValue;
324
353
 
325
354
  if (ATTRIBUTE_NAME_REGEXP.test(attributeName)) {