Skip to content

Commit

Permalink
Fix querySelectorAll() handler (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK authored Nov 8, 2024
1 parent 4604a17 commit 02925c1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/js/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,8 @@ export class Finder {
}
} else if (targetType === TARGET_ALL) {
if (dir === DIR_NEXT) {
let { combo } = branch[0];
const { combo: firstCombo } = branch[0];
let combo = firstCombo;
for (const node of entryNodes) {
let nextNodes = new Set([node]);
for (let j = 1; j < branchLen; j++) {
Expand All @@ -2607,8 +2608,10 @@ export class Finder {
const n = [...nodes];
nodes = new Set([...n, ...nextNodes]);
sort = true;
combo = firstCombo;
} else {
nodes = nextNodes;
combo = firstCombo;
}
} else {
combo = nextCombo;
Expand Down
6 changes: 3 additions & 3 deletions src/js/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,14 @@ export const filterSelector = (selector, opt = {}) => {
}
// include pseudo-classes that are known to work correctly
if (selector.includes(':')) {
if (/:(?:is|not)\(/.test(selector)) {
if (descend) {
return false;
} else if (/:(?:is|not)\(/.test(selector)) {
if (complex) {
return !REG_LOGICAL_COMPLEX.test(selector);
} else {
return !REG_LOGICAL_COMPOUND.test(selector);
}
} else if (descend) {
return false;
} else {
return !REG_WO_LOGICAL.test(selector);
}
Expand Down
38 changes: 38 additions & 0 deletions test/finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13758,5 +13758,43 @@ describe('Finder', () => {
const res = finder.find('all');
assert.deepEqual([...res], [], 'result');
});

it('should get matched node(s)', () => {
const parent = document.getElementById('div0');
const div = document.createElement('div');
const div2 = document.createElement('div');
const span = document.createElement('span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');
const span4 = document.createElement('span');
const span5 = document.createElement('span');
const span6 = document.createElement('span');
const span7 = document.createElement('span');
const span8 = document.createElement('span');
div.id = 'div01';
div.classList.add('foobar');
div.appendChild(span);
div.appendChild(span2);
div.appendChild(span3);
div.appendChild(span4);
div2.id = 'div02';
div2.classList.add('foobar');
div2.appendChild(span5);
div2.appendChild(span6);
div2.appendChild(span7);
div2.appendChild(span8);
parent.append(div, div2);
const finder = new Finder(window);
finder.setup('.foobar > :not([hidden]) ~ :not([hidden])', parent);
const res = finder.find('all');
assert.deepEqual([...res], [
span2,
span3,
span4,
span6,
span7,
span8
], 'result');
});
});
});
92 changes: 63 additions & 29 deletions test/utility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,26 +1019,26 @@ describe('utility functions', () => {
assert.throws(() => func('foo'), TypeError, 'Unexpected type String');
});

it('should get result', () => {
it('should get false', () => {
const res = func(document);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
const res = func(node);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
const parent = document.getElementById('div0');
parent.appendChild(node);
const res = func(node);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node = document.createElement('div');
Object.defineProperty(node, 'isContentEditable', {
value: true,
Expand All @@ -1050,7 +1050,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node = document.createElement('div');
const parent = document.getElementById('div0');
parent.appendChild(node);
Expand All @@ -1059,7 +1059,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node = document.createElement('div');
node.setAttribute('contenteditable', 'true');
const parent = document.getElementById('div0');
Expand All @@ -1068,7 +1068,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node = document.createElement('div');
node.setAttribute('contenteditable', 'plaintext-only');
const parent = document.getElementById('div0');
Expand All @@ -1077,7 +1077,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node = document.createElement('div');
node.setAttribute('contenteditable', '');
const parent = document.getElementById('div0');
Expand All @@ -1086,7 +1086,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
node.setAttribute('contenteditable', 'foo');
const parent = document.getElementById('div0');
Expand All @@ -1095,7 +1095,7 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
node.setAttribute('contenteditable', 'inherit');
const parent = document.getElementById('div0');
Expand All @@ -1104,7 +1104,7 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node1 = document.createElement('div');
node1.setAttribute('contenteditable', 'inherit');
const node2 = document.createElement('div');
Expand All @@ -1116,7 +1116,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node1 = document.createElement('div');
const node2 = document.createElement('div');
node2.setAttribute('contenteditable', 'true');
Expand Down Expand Up @@ -1671,20 +1671,20 @@ describe('utility functions', () => {
describe('is namespace declared', () => {
const func = util.isNamespaceDeclared;

it('should get result', () => {
it('should get false', () => {
const res = func();
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('foo:div');
const parent = document.getElementById('div0');
parent.appendChild(node);
const res = func('foo', node);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const node =
document.createElementNS('https://example.com/foo', 'foo:div');
const parent = document.getElementById('div0');
Expand All @@ -1693,7 +1693,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const frag = document.createDocumentFragment();
const node =
document.createElementNS('https://example.com/foo', 'foo:div');
Expand All @@ -1702,7 +1702,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const frag = document.createDocumentFragment();
const node =
document.createElementNS('https://example.com/foo', 'foo:div');
Expand Down Expand Up @@ -1736,7 +1736,7 @@ describe('utility functions', () => {
'Unexpected type String');
});

it('should get result', () => {
it('should get true', () => {
const nodeA = document.createElement('ul');
const nodeB = document.createElement('li');
const parent = document.getElementById('div0');
Expand All @@ -1746,7 +1746,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const nodeA = document.createElement('li');
const nodeB = document.createElement('ul');
const parent = document.getElementById('div0');
Expand All @@ -1756,7 +1756,7 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get true', () => {
const nodeA = document.createElement('li');
const nodeB = document.createElement('li');
const base = document.createElement('ul');
Expand All @@ -1768,7 +1768,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const nodeA = document.createElement('li');
const nodeB = document.createElement('li');
const base = document.createElement('ul');
Expand All @@ -1780,20 +1780,20 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
const base = document.documentElement;
const res = func(node, base);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const node = document.createElement('div');
const res = func(node, node);
assert.isFalse(res, 'result');
});

it('should get result', () => {
it('should get false', () => {
const tmpl = document.createElement('template');
const node = document.createElement('div');
tmpl.appendChild(node);
Expand Down Expand Up @@ -2081,17 +2081,17 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get false', () => {
it('should get true', () => {
const res = func(':read-only');
assert.isTrue(res, 'result');
});

it('should get false', () => {
it('should get true', () => {
const res = func(':read-write');
assert.isTrue(res, 'result');
});

it('should get false', () => {
it('should get true', () => {
const res = func(':empty');
assert.isTrue(res, 'result');
});
Expand Down Expand Up @@ -2126,6 +2126,40 @@ describe('utility functions', () => {
assert.isFalse(res, 'result');
});

it('should get true', () => {
const res = func('.foo + .bar');
assert.isTrue(res, 'result');
});

it('should get true', () => {
const res = func(':first-child :last-child');
assert.isTrue(res, 'result');
});

it('should get false', () => {
const res = func(':first-child :last-child', {
descend: true
});
assert.isFalse(res, 'result');
});

it('should get true', () => {
const res = func(':is(.foo, .bar)');
assert.isTrue(res, 'result');
});

it('should get false', () => {
const res = func(':is(.foo .bar, .bar)');
assert.isFalse(res, 'result');
});

it('should get true', () => {
const res = func(':is(.foo .bar, .bar)', {
complex: true
});
assert.isTrue(res, 'result');
});

it('should get true', () => {
const res = func(':nth-child(even)');
assert.isTrue(res, 'result');
Expand Down Expand Up @@ -2252,7 +2286,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get false', () => {
it('should get true', () => {
const res = func(':not(.foo > .bar)', {
complex: true
});
Expand Down Expand Up @@ -2288,7 +2322,7 @@ describe('utility functions', () => {
assert.isTrue(res, 'result');
});

it('should get false', () => {
it('should get true', () => {
const res = func(':not(:is(.foo > .bar))', {
complex: true
});
Expand Down

0 comments on commit 02925c1

Please sign in to comment.