puppeteer-core
Version:
A high-level API to control headless Chrome over the DevTools Protocol
47 lines (43 loc) • 1.06 kB
text/typescript
/**
* @license
* Copyright 2022 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import {
createTextContent,
isSuitableNodeForTextMatching,
} from './TextContent.js';
/**
* Queries the given node for all nodes matching the given text selector.
*
* @internal
*/
export const textQuerySelectorAll = function* (
root: Node,
selector: string,
): Generator<Element> {
let yielded = false;
for (const node of root.childNodes) {
if (node instanceof Element && isSuitableNodeForTextMatching(node)) {
for (const match of textQuerySelectorAll(node, selector)) {
yield match;
yielded = true;
}
}
}
if (root instanceof Element && root.shadowRoot) {
for (const match of textQuerySelectorAll(root.shadowRoot, selector)) {
yield match;
yielded = true;
}
}
if (yielded) {
return;
}
if (root instanceof Element && isSuitableNodeForTextMatching(root)) {
const textContent = createTextContent(root);
if (textContent.full.includes(selector)) {
yield root;
}
}
};