Skip to content

Commit

Permalink
fix: If currentNode and root are the same, do not include them in the…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
Pirikara and jrandolf committed May 11, 2022
1 parent 4854ad5 commit a61144d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit a61144d

Please sign in to comment.