Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance degradation #26

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/js/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2538,15 +2538,26 @@ export class Matcher {
let find;
let twig;
if (complex) {
const { leaves: [{ type: firstType }] } = firstTwig;
const { combo: firstCombo, leaves: [{ type: firstType }] } = firstTwig;
const lastTwig = branch[branchLen - 1];
const { leaves: [{ type: lastType }] } = lastTwig;
if (lastType === SELECTOR_PSEUDO_ELEMENT || lastType === SELECTOR_ID) {
find = FIND_PREV;
twig = lastTwig;
} else if (firstType === SELECTOR_PSEUDO_ELEMENT ||
firstType === SELECTOR_ID ||
targetType === TARGET_ALL) {
firstType === SELECTOR_ID) {
find = FIND_NEXT;
twig = firstTwig;
} else if (targetType === TARGET_ALL && branchLen === BIT_02) {
const { name: comboName } = firstCombo;
if (/^[+~]$/.test(comboName)) {
find = FIND_PREV;
twig = lastTwig;
} else {
find = FIND_NEXT;
twig = firstTwig;
}
} else if (targetType === TARGET_ALL) {
find = FIND_NEXT;
twig = firstTwig;
} else {
Expand Down Expand Up @@ -2765,11 +2776,10 @@ export class Matcher {
nodes.add(node);
}
matched = true;
break;
} else {
matched = false;
combo = nextCombo;
nextNodes = new Set(arr);
matched = false;
}
} else {
matched = false;
Expand Down Expand Up @@ -2809,11 +2819,10 @@ export class Matcher {
const [node] = this._sortNodes(arr);
nodes.add(node);
matched = true;
break;
} else {
matched = false;
combo = nextCombo;
nextNodes = new Set(arr);
matched = false;
}
} else {
matched = false;
Expand Down Expand Up @@ -2851,10 +2860,9 @@ export class Matcher {
if (j === 0) {
nodes.add(node);
matched = true;
break;
} else {
matched = false;
nextNodes = new Set(arr);
matched = false;
}
} else {
matched = false;
Expand Down Expand Up @@ -2887,10 +2895,9 @@ export class Matcher {
if (j === 0) {
nodes.add(refNode);
matched = true;
break;
} else {
matched = false;
nextNodes = new Set(arr);
matched = false;
}
} else {
matched = false;
Expand Down
31 changes: 29 additions & 2 deletions test/matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11216,8 +11216,8 @@ describe('match AST leaf and DOM node', () => {
]
}
],
filtered: false,
find: 'next',
filtered: true,
find: 'prev',
skip: false
}
],
Expand Down Expand Up @@ -11543,6 +11543,33 @@ describe('match AST leaf and DOM node', () => {
], 'result');
});

it('should get matched node(s)', () => {
const matcher =
new Matcher('li:last-child, li:first-child + li', document);
matcher._collectNodes('first');
const res = matcher._matchNodes('all');
assert.deepEqual([...res], [
document.getElementById('li3'),
document.getElementById('li2')
], 'result');
});

it('should not match', () => {
const matcher =
new Matcher('ul:nth-child(2) > li, li:nth-child(4) + li', document);
matcher._collectNodes('first');
const res = matcher._matchNodes('first');
assert.deepEqual([...res], [], 'result');
});

it('should not match', () => {
const matcher =
new Matcher('ul:nth-child(2) > li, li:nth-child(4) + li', document);
matcher._collectNodes('all');
const res = matcher._matchNodes('all');
assert.deepEqual([...res], [], 'result');
});

it('should get matched node(s)', () => {
const matcher = new Matcher('ol > .li ~ li, ul > .li ~ li', document);
matcher._collectNodes('first');
Expand Down