Skip to content

Commit

Permalink
fix: [#1558] Fixes bug where psuedo selectors without an ending prant…
Browse files Browse the repository at this point in the history
…hese result in invalid selector error (#1602)
  • Loading branch information
capricorn86 authored Nov 12, 2024
1 parent e038cf9 commit dc06801
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/query-selector/SelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import ISelectorPseudo from './ISelectorPseudo.js';
* Group 17: Combinator.
*/
const SELECTOR_REGEXP =
/(\*)|([a-zA-Z0-9-]+)|#((?:[a-zA-Z0-9-_]|\\.)+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)\s*([~|^$*]{0,1})\s*=\s*["']{1}([^"']*)["']{1}\s*(s|i){0,1}\]|\[([a-zA-Z0-9-_]+)\s*([~|^$*]{0,1})\s*=\s*([^\]]*)\]|:([a-zA-Z-]+)\s*\(([^)]+\)?)\)|:([a-zA-Z-]+)|::([a-zA-Z-]+)|([\s,+>]*)/gm;
/(\*)|([a-zA-Z0-9-]+)|#((?:[a-zA-Z0-9-_]|\\.)+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_\\:]+)\]|\[([a-zA-Z0-9-_\\:]+)\s*([~|^$*]{0,1})\s*=\s*["']{1}([^"']*)["']{1}\s*(s|i){0,1}\]|\[([a-zA-Z0-9-_]+)\s*([~|^$*]{0,1})\s*=\s*([^\]]*)\]|:([a-zA-Z-]+)\s*\(([^)]+)\){0,1}|:([a-zA-Z-]+)|::([a-zA-Z-]+)|([\s,+>]*)/gm;

/**
* Escaped Character RegExp.
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/xml-parser/XMLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const MARKUP_REGEXP =
* Group 9: Attribute name when the attribute has no value (e.g. "disabled" in "<div disabled>").
*/
const ATTRIBUTE_REGEXP =
/\s*([a-zA-Z0-9-_:.$@?]+) *= *([a-zA-Z0-9-_:.$@?{}/]+)|\s*([a-zA-Z0-9-_:.$@?]+) *= *"([^"]*)("{0,1})|\s*([a-zA-Z0-9-_:.$@?]+) *= *'([^']*)('{0,1})|\s*([a-zA-Z0-9-_:.$@?]+)/gm;
/\s*([a-zA-Z0-9-_:.$@?\\]+) *= *([a-zA-Z0-9-_:.$@?{}/]+)|\s*([a-zA-Z0-9-_:.$@?\\]+) *= *"([^"]*)("{0,1})|\s*([a-zA-Z0-9-_:.$@?\\]+) *= *'([^']*)('{0,1})|\s*([a-zA-Z0-9-_:.$@?\\]+)/gm;

enum MarkupReadStateEnum {
startOrEndTag = 'startOrEndTag',
Expand Down
25 changes: 25 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,31 @@ describe('QuerySelector', () => {
expect(elements3[1] === container.children[0].children[1].children[2]).toBe(true);
});

it('Returns all elements matching the selector without ending parenthese "button:not([type]"', () => {
const container = document.createElement('div');
container.innerHTML = `
<button></button>
<button type="submit"></button>
<button></button>
`;
const elements = container.querySelectorAll('button:not([type]');
expect(elements.length).toBe(2);
expect(elements[0]).toBe(container.children[0]);
expect(elements[1]).toBe(container.children[2]);
});

it('Returns all elements matching the invalid selector "[q\\:shadowroot]"', () => {
const container = document.createElement('div');
container.innerHTML = `
<span q\\:shadowroot></span>
<button></button>
<article></article>
`;
const elements = container.querySelectorAll('[q\\:shadowroot]');
expect(elements.length).toBe(1);
expect(elements[0]).toBe(container.children[0]);
});

it('Returns all elements matching "input:not([type]):not([list])" to verify that "screen.getByRole(\'checkbox\')" works in Testing Library.', () => {
const container = document.createElement('div');

Expand Down

0 comments on commit dc06801

Please sign in to comment.