Skip to content

Commit

Permalink
chore: [#1678] Fixes problem with query selector
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jan 8, 2025
1 parent 55110a1 commit 3f545e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/query-selector/SelectorItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ISelectorAttribute from './ISelectorAttribute.js';
import ISelectorMatch from './ISelectorMatch.js';
import ISelectorPseudo from './ISelectorPseudo.js';

const SPACE_REGEXP = /\s+/;

/**
* Selector item.
*/
Expand Down Expand Up @@ -417,7 +419,7 @@ export default class SelectorItem {
return null;
}

const classList = element.className.split(' ');
const classList = element.className.split(SPACE_REGEXP);
let priorityWeight = 0;

for (const className of this.classNames) {
Expand Down
4 changes: 4 additions & 0 deletions packages/happy-dom/test/html-parser/HTMLParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,10 @@ describe('HTMLParser', () => {
visually-hidden" type="button">{{message_gui_replies}}</button>
</div>`
);

const element = result.querySelector('div > .comment_collapse');

expect(element).toBe(result.children[0].children[1]);
});
});
});
14 changes: 12 additions & 2 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('QuerySelector', () => {
document = window.document;
});

describe('querySelectorAll', () => {
describe('querySelectorAll()', () => {
it('Throws an error for invalid selectors.', () => {
const container = document.createElement('div');
expect(() => container.querySelectorAll(<string>(<unknown>12))).toThrow(
Expand Down Expand Up @@ -1214,7 +1214,7 @@ describe('QuerySelector', () => {
});
});

describe('querySelector', () => {
describe('querySelector()', () => {
it('Throws an error for invalid selectors.', () => {
const container = document.createElement('div');
expect(() => container.querySelector(<string>(<unknown>12))).toThrow(
Expand Down Expand Up @@ -1628,6 +1628,16 @@ describe('QuerySelector', () => {
expect(document.querySelector(':focus')).toBe(div);
expect(document.querySelector(':focus-visible')).toBe(div);
});

it('Handles class names with line breaks', () => {
const div = document.createElement('div');
div.innerHTML = `
<div class="class1
class2"></div>
`;

expect(div.querySelector('.class1.class2')).toBe(div.children[0]);
});
});

describe('matches()', () => {
Expand Down

0 comments on commit 3f545e8

Please sign in to comment.