question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Element.matches fails when using non-matching descendant selector on element attached to document

See original GitHub issue
const element = document.createElement('div');
document.appendChild(element);
expect(element.matches('.nonexistent-class div')).toBe(false);
TypeError: Cannot read properties of undefined (reading 'split')

  296 |     ): { priorityWeight: number; matches: boolean } {
  297 |             const regexp = new RegExp(CLASS_REGEXP, 'g');
> 298 |             const classList = element.className.split(' ');
	  |                                                 ^
  299 |             const classSelector = selector.split(':')[0];
  300 |             let priorityWeight = 0;
  301 |             let match: RegExpMatchArray;

  at SelectorItem.matchesClass (src/query-selector/SelectorItem.ts:298:39)
  at SelectorItem.match (src/query-selector/SelectorItem.ts:91:24)
  at Function.matchesSelector (src/query-selector/QuerySelector.ts:107:27)
  at Function.matchesSelector (src/query-selector/QuerySelector.ts:113:15)
  at Function.matchesSelector (src/query-selector/QuerySelector.ts:113:15)
  at Function.matchesSelector (src/query-selector/QuerySelector.ts:113:15)
  at Function.match (src/query-selector/QuerySelector.ts:66:24)
  at HTMLElement.matches (src/nodes/element/Element.ts:734:24)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
cristianstucommented, Oct 18, 2022

@cristianstu give v7.5.14 a try and let us know!

It’s fixed! Everything looks nice, thanks!

1reaction
capricorn86commented, Oct 18, 2022

Thanks for your contribution @IGx89! 🙂

Your fix is in now.

You can read more about the release here: https://github.com/capricorn86/happy-dom/releases/tag/v7.5.14

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element.matches() - Web APIs - MDN Web Docs
The matches() method of the Element interface tests whether the element would be selected by the specified CSS selector.
Read more >
Why does jQuery or a DOM method such as getElementById ...
For efficiency, select the nearest reliable ancestor of the target element rather than attaching it to the document .
Read more >
Selectors Level 3 - W3C
Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to...
Read more >
Searching: getElement*, querySelector*
If an element has the id attribute, we can get the element using the method document.getElementById(id) , no matter where it is.
Read more >
Select Page Elements | Basic Guides - TestCafe
Finds a descendant node that matches the specified CSS selector or predicate. Selects parent elements. Selects child elements.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found