Skip to content

Commit

Permalink
capricorn86#661@patch: Take class selector escaping into account.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejodo committed Nov 21, 2022
1 parent e91b864 commit 62076da
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/happy-dom/src/query-selector/SelectorItem.ts
@@ -1,11 +1,13 @@
import DOMException from '../exception/DOMException';
import IElement from '../nodes/element/IElement';
import Element from '../nodes/element/Element';
import CSSEscape from 'css.escape';

const ATTRIBUTE_REGEXP =
/\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)([~|^$*]{0,1})[ ]*=[ ]*["']{0,1}([^"']+)["']{0,1}\]/g;
const ATTRIBUTE_NAME_REGEXP = /[^a-zA-Z0-9-_$]/;
const PSUEDO_REGEXP = /:([a-zA-Z-]+)\(([0-9n+-]+|odd|even)\)|:not\(([^)]+)\)|:([a-zA-Z-]+)/g;
const PSUEDO_REGEXP =
/(?<!\\):([a-zA-Z-]+)\(([0-9n+-]+|odd|even)\)|(?<!\\):not\(([^)]+)\)|(?<!\\):([a-zA-Z-]+)/g;
const CLASS_REGEXP = /\.([a-zA-Z0-9-_$]+)/g;
const TAG_NAME_REGEXP = /^[a-zA-Z0-9-]+/;
const ID_REGEXP = /#[A-Za-z][-A-Za-z0-9_]*/g;
Expand Down Expand Up @@ -294,19 +296,16 @@ export default class SelectorItem {
element: IElement,
selector: string
): { priorityWeight: number; matches: boolean } {
const regexp = new RegExp(CLASS_REGEXP, 'g');
const classList = element.className.split(' ');
const classSelector = selector.split(':')[0];
const classList = element.className.split(' ').map(CSSEscape);
const classSelector = selector.split(/(?<!\\):/g)[0];
let priorityWeight = 0;
let match: RegExpMatchArray;

while ((match = regexp.exec(classSelector))) {
// Still figuring the right regex for this
if (classSelector.startsWith('.')) {
priorityWeight += 10;
if (!classList.includes(match[1])) {
if (!classList.includes(classSelector.slice(1))) {
return { priorityWeight: 0, matches: false };
}
}

return { priorityWeight, matches: true };
}

Expand Down

0 comments on commit 62076da

Please sign in to comment.