happy-dom icon indicating copy to clipboard operation
happy-dom copied to clipboard

document.querySelector misbehaving on 'tagName#id'

Open Tanimodori opened this issue 2 years ago • 2 comments

It ignores the #id part and search for the tagName only.

const window = new Window();
const document = window.document;
const content = document.createElement('div');
content.innerHTML = `<div id="foo">bar</div>`;
document.body.appendChild(content);

console.log(document.querySelector('div#foo').outerHTML);
// <div><div id="foo">bar</div></div>

console.log(document.querySelector('#foo').outerHTML);
// <div id="foo">bar</div>

On my Chrome, both console.log print <div id="foo">bar</div>.

Environment

  • happy-dom 6.0.4
  • Chrome 105.0.5195.102

Tanimodori avatar Sep 02 '22 21:09 Tanimodori

Of course you can search #foo in this example, but some libraries may rely on this feature (e.g. document.querySelector should return null when tagName does not match)

Tanimodori avatar Sep 02 '22 21:09 Tanimodori

According to this W3C selector document, E#myid is a valid selector and considered as "an E element with ID equal to myid."

Tanimodori avatar Sep 03 '22 13:09 Tanimodori

Thank you for reporting and contributing @Tanimodori! 🙂

Your PR has now been merged and released.

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

capricorn86 avatar Oct 06 '22 15:10 capricorn86