Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import type { AnyNode, Element } from "domhandler";
import { findAll, findOne } from "domutils";
import { parseDocument } from "htmlparser2";
/** Parse an HTML fragment / document and return the DOM root list. */
export function parse(html: string): AnyNode[] {
return parseDocument(html).children;
}
/** Find the first element matching a predicate (deep search). */
export function findFirst(
html: string,
predicate: (el: Element) => boolean
): Element | null {
const roots = parse(html);
return findOne(predicate, roots, true);
}
/** Find every element matching a predicate (deep search). */
export function findEvery(
html: string,
predicate: (el: Element) => boolean
): Element[] {
const roots = parse(html);
return findAll(predicate, roots);
}
/** Extract text content from an element (recursive). */
export function textOf(el: Element | AnyNode | null | undefined): string {
if (el === null || el === undefined) return "";
if ("data" in el && typeof el.data === "string" && el.type === "text") {
return el.data;
}
if ("children" in el && Array.isArray(el.children)) {
return el.children.map((c) => textOf(c as AnyNode)).join("");
}
return "";
}