happy-dom 14.3.3 → 14.3.5
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.
- package/cjs/css/declaration/element-style/CSSStyleDeclarationElementStyle.cjs +5 -3
- package/cjs/css/declaration/element-style/CSSStyleDeclarationElementStyle.cjs.map +1 -1
- package/cjs/css/declaration/element-style/CSSStyleDeclarationElementStyle.d.ts.map +1 -1
- package/cjs/custom-element/CustomElementRegistry.cjs +3 -18
- package/cjs/custom-element/CustomElementRegistry.cjs.map +1 -1
- package/cjs/custom-element/CustomElementRegistry.d.ts.map +1 -1
- package/cjs/nodes/element/Element.cjs +19 -16
- package/cjs/nodes/element/Element.cjs.map +1 -1
- package/cjs/nodes/element/Element.d.ts +3 -0
- package/cjs/nodes/element/Element.d.ts.map +1 -1
- package/cjs/query-selector/QuerySelector.cjs +10 -4
- package/cjs/query-selector/QuerySelector.cjs.map +1 -1
- package/cjs/query-selector/QuerySelector.d.ts +5 -1
- package/cjs/query-selector/QuerySelector.d.ts.map +1 -1
- package/cjs/query-selector/SelectorItem.cjs +9 -4
- package/cjs/query-selector/SelectorItem.cjs.map +1 -1
- package/cjs/query-selector/SelectorItem.d.ts +4 -1
- package/cjs/query-selector/SelectorItem.d.ts.map +1 -1
- package/cjs/query-selector/SelectorParser.cjs +39 -18
- package/cjs/query-selector/SelectorParser.cjs.map +1 -1
- package/cjs/query-selector/SelectorParser.d.ts +12 -2
- package/cjs/query-selector/SelectorParser.d.ts.map +1 -1
- package/lib/css/declaration/element-style/CSSStyleDeclarationElementStyle.d.ts.map +1 -1
- package/lib/css/declaration/element-style/CSSStyleDeclarationElementStyle.js +5 -3
- package/lib/css/declaration/element-style/CSSStyleDeclarationElementStyle.js.map +1 -1
- package/lib/custom-element/CustomElementRegistry.d.ts.map +1 -1
- package/lib/custom-element/CustomElementRegistry.js +3 -18
- package/lib/custom-element/CustomElementRegistry.js.map +1 -1
- package/lib/nodes/element/Element.d.ts +3 -0
- package/lib/nodes/element/Element.d.ts.map +1 -1
- package/lib/nodes/element/Element.js +19 -16
- package/lib/nodes/element/Element.js.map +1 -1
- package/lib/query-selector/QuerySelector.d.ts +5 -1
- package/lib/query-selector/QuerySelector.d.ts.map +1 -1
- package/lib/query-selector/QuerySelector.js +10 -4
- package/lib/query-selector/QuerySelector.js.map +1 -1
- package/lib/query-selector/SelectorItem.d.ts +4 -1
- package/lib/query-selector/SelectorItem.d.ts.map +1 -1
- package/lib/query-selector/SelectorItem.js +9 -4
- package/lib/query-selector/SelectorItem.js.map +1 -1
- package/lib/query-selector/SelectorParser.d.ts +12 -2
- package/lib/query-selector/SelectorParser.d.ts.map +1 -1
- package/lib/query-selector/SelectorParser.js +39 -18
- package/lib/query-selector/SelectorParser.js.map +1 -1
- package/package.json +1 -1
- package/src/css/declaration/element-style/CSSStyleDeclarationElementStyle.ts +5 -3
- package/src/custom-element/CustomElementRegistry.ts +3 -21
- package/src/nodes/element/Element.ts +10 -5
- package/src/query-selector/QuerySelector.ts +15 -4
- package/src/query-selector/SelectorItem.ts +15 -4
- package/src/query-selector/SelectorParser.ts +49 -18
@@ -18,6 +18,7 @@ export default class SelectorItem {
|
|
18
18
|
public pseudos: ISelectorPseudo[] | null;
|
19
19
|
public isPseudoElement: boolean;
|
20
20
|
public combinator: SelectorCombinatorEnum;
|
21
|
+
public ignoreErrors: boolean;
|
21
22
|
|
22
23
|
/**
|
23
24
|
* Constructor.
|
@@ -30,6 +31,7 @@ export default class SelectorItem {
|
|
30
31
|
* @param [options.attributes] Attributes.
|
31
32
|
* @param [options.pseudos] Pseudos.
|
32
33
|
* @param [options.isPseudoElement] Is pseudo element.
|
34
|
+
* @param [options.ignoreErrors] Ignore errors.
|
33
35
|
*/
|
34
36
|
constructor(options?: {
|
35
37
|
tagName?: string;
|
@@ -39,6 +41,7 @@ export default class SelectorItem {
|
|
39
41
|
pseudos?: ISelectorPseudo[];
|
40
42
|
isPseudoElement?: boolean;
|
41
43
|
combinator?: SelectorCombinatorEnum;
|
44
|
+
ignoreErrors?: boolean;
|
42
45
|
}) {
|
43
46
|
this.tagName = options?.tagName || null;
|
44
47
|
this.id = options?.id || null;
|
@@ -47,6 +50,7 @@ export default class SelectorItem {
|
|
47
50
|
this.pseudos = options?.pseudos || null;
|
48
51
|
this.isPseudoElement = options?.isPseudoElement || false;
|
49
52
|
this.combinator = options?.combinator || SelectorCombinatorEnum.descendant;
|
53
|
+
this.ignoreErrors = options?.ignoreErrors || false;
|
50
54
|
}
|
51
55
|
|
52
56
|
/**
|
@@ -98,7 +102,7 @@ export default class SelectorItem {
|
|
98
102
|
|
99
103
|
// Pseudo match
|
100
104
|
if (this.pseudos) {
|
101
|
-
const result = this.
|
105
|
+
const result = this.matchPseudo(element);
|
102
106
|
if (!result) {
|
103
107
|
return null;
|
104
108
|
}
|
@@ -114,7 +118,7 @@ export default class SelectorItem {
|
|
114
118
|
* @param element Element.
|
115
119
|
* @returns Result.
|
116
120
|
*/
|
117
|
-
private
|
121
|
+
private matchPseudo(element: Element): ISelectorMatch | null {
|
118
122
|
const parent = <Element>element[PropertySymbol.parentNode];
|
119
123
|
const parentChildren = element[PropertySymbol.parentNode]
|
120
124
|
? (<Element>element[PropertySymbol.parentNode])[PropertySymbol.children]
|
@@ -135,7 +139,14 @@ export default class SelectorItem {
|
|
135
139
|
case 'nth-last-child':
|
136
140
|
case 'nth-last-of-type':
|
137
141
|
if (!pseudo.arguments) {
|
138
|
-
|
142
|
+
if (this.ignoreErrors) {
|
143
|
+
return null;
|
144
|
+
}
|
145
|
+
throw new DOMException(
|
146
|
+
`Failed to execute 'matches' on '${
|
147
|
+
element.constructor.name
|
148
|
+
}': '${this.getSelectorString()}' is not a valid selector.`
|
149
|
+
);
|
139
150
|
}
|
140
151
|
break;
|
141
152
|
}
|
@@ -357,7 +368,7 @@ export default class SelectorItem {
|
|
357
368
|
* @returns Selector string.
|
358
369
|
*/
|
359
370
|
private getSelectorString(): string {
|
360
|
-
return `${this.tagName
|
371
|
+
return `${this.tagName ? this.tagName.toLowerCase() : ''}${this.id ? `#${this.id}` : ''}${
|
361
372
|
this.classNames ? `.${this.classNames.join('.')}` : ''
|
362
373
|
}${
|
363
374
|
this.attributes
|
@@ -63,38 +63,52 @@ export default class SelectorParser {
|
|
63
63
|
* Parses a selector string and returns an instance of SelectorItem.
|
64
64
|
*
|
65
65
|
* @param selector Selector.
|
66
|
+
* @param [options] Options.
|
67
|
+
* @param [options.ignoreErrors] Ignores errors.
|
66
68
|
* @returns Selector item.
|
67
69
|
*/
|
68
|
-
public static getSelectorItem(
|
69
|
-
|
70
|
+
public static getSelectorItem(
|
71
|
+
selector: string,
|
72
|
+
options?: { ignoreErrors?: boolean }
|
73
|
+
): SelectorItem {
|
74
|
+
return this.getSelectorGroups(selector, options)[0][0];
|
70
75
|
}
|
71
76
|
|
72
77
|
/**
|
73
78
|
* Parses a selector string and returns groups with SelectorItem instances.
|
74
79
|
*
|
75
80
|
* @param selector Selector.
|
81
|
+
* @param [options] Options.
|
82
|
+
* @param [options.ignoreErrors] Ignores errors.
|
76
83
|
* @returns Selector groups.
|
77
84
|
*/
|
78
|
-
public static getSelectorGroups(
|
85
|
+
public static getSelectorGroups(
|
86
|
+
selector: string,
|
87
|
+
options?: { ignoreErrors?: boolean }
|
88
|
+
): Array<Array<SelectorItem>> {
|
89
|
+
const ignoreErrors = options?.ignoreErrors;
|
79
90
|
if (selector === '*') {
|
80
|
-
return [[new SelectorItem({ tagName: '*' })]];
|
91
|
+
return [[new SelectorItem({ tagName: '*', ignoreErrors })]];
|
81
92
|
}
|
82
93
|
|
83
94
|
const simpleMatch = selector.match(SIMPLE_SELECTOR_REGEXP);
|
84
95
|
|
85
96
|
if (simpleMatch) {
|
86
97
|
if (simpleMatch[1]) {
|
87
|
-
return [[new SelectorItem({ tagName: selector.toUpperCase() })]];
|
98
|
+
return [[new SelectorItem({ tagName: selector.toUpperCase(), ignoreErrors })]];
|
88
99
|
} else if (simpleMatch[2]) {
|
89
|
-
return [
|
100
|
+
return [
|
101
|
+
[new SelectorItem({ classNames: selector.replace('.', '').split('.'), ignoreErrors })]
|
102
|
+
];
|
90
103
|
} else if (simpleMatch[3]) {
|
91
|
-
return [[new SelectorItem({ id: selector.replace('#', '') })]];
|
104
|
+
return [[new SelectorItem({ id: selector.replace('#', ''), ignoreErrors })]];
|
92
105
|
}
|
93
106
|
}
|
94
107
|
|
95
108
|
const regexp = new RegExp(SELECTOR_REGEXP);
|
96
109
|
let currentSelectorItem: SelectorItem = new SelectorItem({
|
97
|
-
combinator: SelectorCombinatorEnum.descendant
|
110
|
+
combinator: SelectorCombinatorEnum.descendant,
|
111
|
+
ignoreErrors
|
98
112
|
});
|
99
113
|
let currentGroup: SelectorItem[] = [currentSelectorItem];
|
100
114
|
const groups: Array<Array<SelectorItem>> = [currentGroup];
|
@@ -147,34 +161,40 @@ export default class SelectorParser {
|
|
147
161
|
});
|
148
162
|
} else if (match[13] && match[14]) {
|
149
163
|
currentSelectorItem.pseudos = currentSelectorItem.pseudos || [];
|
150
|
-
currentSelectorItem.pseudos.push(this.getPseudo(match[13], match[14]));
|
164
|
+
currentSelectorItem.pseudos.push(this.getPseudo(match[13], match[14], options));
|
151
165
|
} else if (match[15]) {
|
152
166
|
currentSelectorItem.pseudos = currentSelectorItem.pseudos || [];
|
153
|
-
currentSelectorItem.pseudos.push(this.getPseudo(match[15]));
|
167
|
+
currentSelectorItem.pseudos.push(this.getPseudo(match[15], null, options));
|
154
168
|
} else if (match[16]) {
|
155
169
|
currentSelectorItem.isPseudoElement = true;
|
156
170
|
} else if (match[17]) {
|
157
171
|
switch (match[17].trim()) {
|
158
172
|
case ',':
|
159
173
|
currentSelectorItem = new SelectorItem({
|
160
|
-
combinator: SelectorCombinatorEnum.descendant
|
174
|
+
combinator: SelectorCombinatorEnum.descendant,
|
175
|
+
ignoreErrors
|
161
176
|
});
|
162
177
|
currentGroup = [currentSelectorItem];
|
163
178
|
groups.push(currentGroup);
|
164
179
|
break;
|
165
180
|
case '>':
|
166
|
-
currentSelectorItem = new SelectorItem({
|
181
|
+
currentSelectorItem = new SelectorItem({
|
182
|
+
combinator: SelectorCombinatorEnum.child,
|
183
|
+
ignoreErrors
|
184
|
+
});
|
167
185
|
currentGroup.push(currentSelectorItem);
|
168
186
|
break;
|
169
187
|
case '+':
|
170
188
|
currentSelectorItem = new SelectorItem({
|
171
|
-
combinator: SelectorCombinatorEnum.adjacentSibling
|
189
|
+
combinator: SelectorCombinatorEnum.adjacentSibling,
|
190
|
+
ignoreErrors
|
172
191
|
});
|
173
192
|
currentGroup.push(currentSelectorItem);
|
174
193
|
break;
|
175
194
|
case '':
|
176
195
|
currentSelectorItem = new SelectorItem({
|
177
|
-
combinator: SelectorCombinatorEnum.descendant
|
196
|
+
combinator: SelectorCombinatorEnum.descendant,
|
197
|
+
ignoreErrors
|
178
198
|
});
|
179
199
|
currentGroup.push(currentSelectorItem);
|
180
200
|
break;
|
@@ -186,6 +206,9 @@ export default class SelectorParser {
|
|
186
206
|
}
|
187
207
|
|
188
208
|
if (!isValid) {
|
209
|
+
if (options?.ignoreErrors) {
|
210
|
+
return [];
|
211
|
+
}
|
189
212
|
throw new DOMException(`Invalid selector: "${selector}"`);
|
190
213
|
}
|
191
214
|
|
@@ -241,9 +264,15 @@ export default class SelectorParser {
|
|
241
264
|
*
|
242
265
|
* @param name Pseudo name.
|
243
266
|
* @param args Pseudo arguments.
|
267
|
+
* @param [options] Options.
|
268
|
+
* @param [options.ignoreErrors] Ignores errors.
|
244
269
|
* @returns Pseudo.
|
245
270
|
*/
|
246
|
-
private static getPseudo(
|
271
|
+
private static getPseudo(
|
272
|
+
name: string,
|
273
|
+
args?: string,
|
274
|
+
options?: { ignoreErrors?: boolean }
|
275
|
+
): ISelectorPseudo {
|
247
276
|
const lowerName = name.toLowerCase();
|
248
277
|
|
249
278
|
if (!args) {
|
@@ -256,7 +285,9 @@ export default class SelectorParser {
|
|
256
285
|
const nthOfIndex = args.indexOf(' of ');
|
257
286
|
const nthFunction = nthOfIndex !== -1 ? args.substring(0, nthOfIndex) : args;
|
258
287
|
const selectorItem =
|
259
|
-
nthOfIndex !== -1
|
288
|
+
nthOfIndex !== -1
|
289
|
+
? this.getSelectorItem(args.substring(nthOfIndex + 4).trim(), options)
|
290
|
+
: null;
|
260
291
|
return {
|
261
292
|
name: lowerName,
|
262
293
|
arguments: args,
|
@@ -275,12 +306,12 @@ export default class SelectorParser {
|
|
275
306
|
return {
|
276
307
|
name: lowerName,
|
277
308
|
arguments: args,
|
278
|
-
selectorItems: [this.getSelectorItem(args)],
|
309
|
+
selectorItems: [this.getSelectorItem(args, options)],
|
279
310
|
nthFunction: null
|
280
311
|
};
|
281
312
|
case 'is':
|
282
313
|
case 'where':
|
283
|
-
const selectorGroups = this.getSelectorGroups(args);
|
314
|
+
const selectorGroups = this.getSelectorGroups(args, options);
|
284
315
|
const selectorItems = [];
|
285
316
|
for (const group of selectorGroups) {
|
286
317
|
selectorItems.push(group[0]);
|