anki/ts/domlib/surround/find-within.ts
Henrik Giesel 30bbbaf00b
Use eslint for sorting our imports (#1637)
* Make eslint sort our imports

* fix missing deps in eslint rule (dae)

Caught on Linux due to the stricter sandboxing

* Remove exports-last eslint rule (for now?)

* Adjust browserslist settings

- We use ResizeObserver which is not supported in browsers like KaiOS,
  Baidu or Android UC

* Raise minimum iOS version 13.4

- It's the first version that supports ResizeObserver

* Apply new eslint rules to sort imports
2022-02-04 18:36:34 +10:00

71 lines
1.8 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { nodeIsElement } from "../../lib/dom";
import type { ChildNodeRange } from "./child-node-range";
import type { ElementMatcher, FoundMatch } from "./matcher";
import { nodeWithinRange } from "./within-range";
/**
* Elements returned should be in post-order
*/
function findWithinNodeInner(
node: Node,
matcher: ElementMatcher,
matches: FoundMatch[],
): void {
if (nodeIsElement(node)) {
for (const child of node.children) {
findWithinNodeInner(child, matcher, matches);
}
const matchType = matcher(node);
if (matchType) {
matches.push({ matchType, element: node });
}
}
}
/**
* Will not include parent node
*/
export function findWithinNode(node: Node, matcher: ElementMatcher): FoundMatch[] {
const matches: FoundMatch[] = [];
if (nodeIsElement(node)) {
for (const child of node.children) {
findWithinNodeInner(child, matcher, matches);
}
}
return matches;
}
export function findWithinRange(range: Range, matcher: ElementMatcher): FoundMatch[] {
const matches: FoundMatch[] = [];
findWithinNodeInner(range.commonAncestorContainer, matcher, matches);
return matches.filter((match: FoundMatch): boolean =>
nodeWithinRange(match.element, range),
);
}
export function findWithin(
childNodeRange: ChildNodeRange,
matcher: ElementMatcher,
): FoundMatch[] {
const { parent, startIndex, endIndex } = childNodeRange;
const matches: FoundMatch[] = [];
for (const node of Array.prototype.slice.call(
parent.childNodes,
startIndex,
endIndex,
)) {
findWithinNodeInner(node, matcher, matches);
}
return matches;
}