mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
parent
4854ad5b15
commit
a61144d437
@ -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());
|
||||
@ -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());
|
||||
|
@ -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'.
|
||||
|
Loading…
Reference in New Issue
Block a user