2022-09-19 10:49:13 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-15 16:48:55 +00:00
|
|
|
import {
|
|
|
|
createTextContent,
|
|
|
|
isSuitableNodeForTextMatching,
|
|
|
|
} from './TextContent.js';
|
|
|
|
|
|
|
|
/**
|
2023-02-14 21:31:30 +00:00
|
|
|
* Queries the given node for all nodes matching the given text selector.
|
2022-09-15 16:48:55 +00:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-02-14 21:31:30 +00:00
|
|
|
export const textQuerySelectorAll = function* (
|
2022-09-19 11:22:10 +00:00
|
|
|
root: Node,
|
|
|
|
selector: string
|
2023-02-14 21:31:30 +00:00
|
|
|
): Generator<Element> {
|
|
|
|
let yielded = false;
|
2022-09-15 16:48:55 +00:00
|
|
|
for (const node of root.childNodes) {
|
|
|
|
if (node instanceof Element && isSuitableNodeForTextMatching(node)) {
|
2023-02-14 21:31:30 +00:00
|
|
|
let matches: Generator<Element, boolean>;
|
|
|
|
if (!node.shadowRoot) {
|
|
|
|
matches = textQuerySelectorAll(node, selector);
|
2022-09-15 16:48:55 +00:00
|
|
|
} else {
|
2023-02-14 21:31:30 +00:00
|
|
|
matches = textQuerySelectorAll(node.shadowRoot, selector);
|
2022-09-15 16:48:55 +00:00
|
|
|
}
|
2023-02-14 21:31:30 +00:00
|
|
|
for (const match of matches) {
|
|
|
|
yield match;
|
|
|
|
yielded = true;
|
2022-09-15 16:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 21:31:30 +00:00
|
|
|
if (yielded) {
|
|
|
|
return;
|
2022-09-15 16:48:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
if (root instanceof Element && isSuitableNodeForTextMatching(root)) {
|
2022-09-15 16:48:55 +00:00
|
|
|
const textContent = createTextContent(root);
|
|
|
|
if (textContent.full.includes(selector)) {
|
2023-02-14 21:31:30 +00:00
|
|
|
yield root;
|
2022-09-15 16:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|