From c6f4c851ec86618ebd9b549b01589865cc41fdc4 Mon Sep 17 00:00:00 2001 From: syi0808 Date: Sat, 27 Jul 2024 20:36:07 +0900 Subject: [PATCH] fix: [#1414] Normalize selector parameter --- .../happy-dom/src/query-selector/SelectorParser.ts | 2 ++ .../test/query-selector/QuerySelector.test.ts | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/packages/happy-dom/src/query-selector/SelectorParser.ts b/packages/happy-dom/src/query-selector/SelectorParser.ts index 6e62fb46b..aecbf44ad 100644 --- a/packages/happy-dom/src/query-selector/SelectorParser.ts +++ b/packages/happy-dom/src/query-selector/SelectorParser.ts @@ -86,6 +86,8 @@ export default class SelectorParser { selector: string, options?: { ignoreErrors?: boolean } ): Array> { + selector = selector.replace(/[\n\r\f\t]/g, '').trim(); + const ignoreErrors = options?.ignoreErrors; if (selector === '*') { return [[new SelectorItem({ tagName: '*', ignoreErrors })]]; diff --git a/packages/happy-dom/test/query-selector/QuerySelector.test.ts b/packages/happy-dom/test/query-selector/QuerySelector.test.ts index 8aa1e032c..261a245b2 100644 --- a/packages/happy-dom/test/query-selector/QuerySelector.test.ts +++ b/packages/happy-dom/test/query-selector/QuerySelector.test.ts @@ -1297,6 +1297,16 @@ describe('QuerySelector', () => { expect(container.querySelector(':where(div)')).toBe(container.children[0]); expect(container.querySelector(':where(span[attr1="val,ue1"])')).toBe(null); }); + + it('Remove new line from selector and trim selector before parse', () => { + const container = document.createElement('div'); + + container.innerHTML = QuerySelectorHTML; + + expect(container.querySelector('\n \n\r \t \f h1 \n \n\r \t \f')).toBe( + container.children[0].children[0] + ); + }); }); describe('matches()', () => {