summaryrefslogtreecommitdiffhomepage
path: root/services/blog/nuldoc-src/dom.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-06-27 23:39:31 +0900
committernsfisis <nsfisis@gmail.com>2025-06-27 23:39:31 +0900
commit674fe965550444db87edc7937ff6932e1a918d9d (patch)
treee8a80dd958d3e082485286bf5785a7992b6e6b0e /services/blog/nuldoc-src/dom.ts
parentfe4d1d625b53796c5f20399790e5ff8c7a7e1608 (diff)
downloadnsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.tar.gz
nsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.tar.zst
nsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.zip
feat(meta): rename vhosts/ directory to services/
Diffstat (limited to 'services/blog/nuldoc-src/dom.ts')
-rw-r--r--services/blog/nuldoc-src/dom.ts102
1 files changed, 102 insertions, 0 deletions
diff --git a/services/blog/nuldoc-src/dom.ts b/services/blog/nuldoc-src/dom.ts
new file mode 100644
index 00000000..ed7ffd31
--- /dev/null
+++ b/services/blog/nuldoc-src/dom.ts
@@ -0,0 +1,102 @@
+export type Text = {
+ kind: "text";
+ content: string;
+ raw: false;
+};
+
+export type RawHTML = {
+ kind: "text";
+ content: string;
+ raw: true;
+};
+
+export type Element = {
+ kind: "element";
+ name: string;
+ attributes: Map<string, string>;
+ children: Node[];
+};
+
+export type Node = Element | Text | RawHTML;
+
+export function addClass(e: Element, klass: string) {
+ const classes = e.attributes.get("class");
+ if (classes === undefined) {
+ e.attributes.set("class", klass);
+ } else {
+ const classList = classes.split(" ");
+ classList.push(klass);
+ classList.sort();
+ e.attributes.set("class", classList.join(" "));
+ }
+}
+
+export function findFirstChildElement(
+ e: Element,
+ name: string,
+): Element | null {
+ for (const c of e.children) {
+ if (c.kind === "element" && c.name === name) {
+ return c;
+ }
+ }
+ return null;
+}
+
+export function findChildElements(e: Element, name: string): Element[] {
+ const cs = [];
+ for (const c of e.children) {
+ if (c.kind === "element" && c.name === name) {
+ cs.push(c);
+ }
+ }
+ return cs;
+}
+
+export function innerText(e: Element): string {
+ let t = "";
+ forEachChild(e, (c) => {
+ if (c.kind === "text") {
+ t += c.content;
+ }
+ });
+ return t;
+}
+
+export function forEachChild(e: Element, f: (n: Node) => void) {
+ for (const c of e.children) {
+ f(c);
+ }
+}
+
+export async function forEachChildAsync(
+ e: Element,
+ f: (n: Node) => Promise<void>,
+): Promise<void> {
+ for (const c of e.children) {
+ await f(c);
+ }
+}
+
+export function forEachChildRecursively(e: Element, f: (n: Node) => void) {
+ const g = (c: Node) => {
+ f(c);
+ if (c.kind === "element") {
+ forEachChild(c, g);
+ }
+ };
+ forEachChild(e, g);
+}
+
+export async function forEachChildRecursivelyAsync(
+ e: Element,
+ f: (n: Node) => Promise<void>,
+): Promise<void> {
+ const g = async (c: Node) => {
+ await f(c);
+ if (c.kind === "element") {
+ await forEachChildAsync(c, g);
+ }
+ };
+ await forEachChildAsync(e, g);
+}