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: If currentNode and root are the same, do not include them in the result #8332

Merged
merged 3 commits into from May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/common/QueryHandler.ts
Expand Up @@ -125,7 +125,7 @@ const pierceHandler = makeQueryHandler({
if (currentNode instanceof ShadowRoot) {
continue;
}
if (!found && currentNode.matches(selector)) {
if (currentNode !== root && !found && currentNode.matches(selector)) {
found = currentNode;
}
} while (!found && iter.nextNode());
Expand All @@ -149,7 +149,7 @@ const pierceHandler = makeQueryHandler({
if (currentNode instanceof ShadowRoot) {
continue;
}
if (currentNode.matches(selector)) {
if (currentNode !== root && currentNode.matches(selector)) {
result.push(currentNode);
}
} while (iter.nextNode());
Expand Down
20 changes: 20 additions & 0 deletions test/queryselector.spec.ts
Expand Up @@ -105,6 +105,26 @@ describe('querySelector', function () {
);
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'.
Expand Down