fix: If currentNode and root are the same, do not include them in the result (#8332)

* fix: If currentNode and root are the same, do not include them in the result

* fix: Tests that only child element is included in the result

Co-authored-by: jrandolf <101637635+jrandolf@users.noreply.github.com>
This commit is contained in:
Pirikara 2022-05-11 21:17:02 +09:00 committed by GitHub
parent 4854ad5b15
commit a61144d437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -125,7 +125,7 @@ const pierceHandler = makeQueryHandler({
if (currentNode instanceof ShadowRoot) { if (currentNode instanceof ShadowRoot) {
continue; continue;
} }
if (!found && currentNode.matches(selector)) { if (currentNode !== root && !found && currentNode.matches(selector)) {
found = currentNode; found = currentNode;
} }
} while (!found && iter.nextNode()); } while (!found && iter.nextNode());
@ -149,7 +149,7 @@ const pierceHandler = makeQueryHandler({
if (currentNode instanceof ShadowRoot) { if (currentNode instanceof ShadowRoot) {
continue; continue;
} }
if (currentNode.matches(selector)) { if (currentNode !== root && currentNode.matches(selector)) {
result.push(currentNode); result.push(currentNode);
} }
} while (iter.nextNode()); } while (iter.nextNode());

View File

@ -105,6 +105,26 @@ describe('querySelector', function () {
); );
expect(text.join(' ')).toBe('Hello World'); expect(text.join(' ')).toBe('Hello World');
}); });
it('should find first child element', async () => {
const { page } = getTestState();
const parentElement = await page.$('html > div');
const childElement = await parentElement.$('pierce/div');
const text = await childElement.evaluate(
(element: Element) => element.textContent
);
expect(text).toBe('Hello');
});
it('should find all child elements', async () => {
const { page } = getTestState();
const parentElement = await page.$('html > div');
const childElements = await parentElement.$$('pierce/div');
const text = await Promise.all(
childElements.map((div) =>
div.evaluate((element: Element) => element.textContent)
)
);
expect(text.join(' ')).toBe('Hello World');
});
}); });
// The tests for $$eval are repeated later in this file in the test group 'QueryAll'. // The tests for $$eval are repeated later in this file in the test group 'QueryAll'.