capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.09k stars 185 forks source link

Selector starting with new line throws error #1414

Open rothsandro opened 2 months ago

rothsandro commented 2 months ago

Describe the bug If selector that starts with a new line, Happy DOM throws an error "Invalid selector".

test("selector", () => {
  document.querySelector("\ninput");
});

To Reproduce I forked the happy-dom repro and added a test in QuerySelector.test.ts. Clone the repro, switch to the bug/selector-with-new-line branch and run the tests.

Repro: https://github.com/rothsandro/happy-dom/tree/bug/selector-with-new-line

Test: https://github.com/rothsandro/happy-dom/blob/bug/selector-with-new-line/packages/happy-dom/test/query-selector/QuerySelector.test.ts#L1090-L1098

it('Returns an element when selector starts with a new line', () => {
  const div = document.createElement('div');
  const div2 = document.createElement('div');

  div2.className = 'baz';
  div.appendChild(div2);

  expect(div.querySelector('\n.baz') === div2).toBe(true);
});

Expected behavior The selector should work because it's valid and works in the browser.

Device:

Additional context A third-party component library that we use in our application uses such a selector. Because of that we cannot test parts of our app because it would result in this error.