summaryrefslogtreecommitdiffhomepage
path: root/services/blog/nuldoc-src/dom.ts
blob: abe7ff89784cf7630e8d0ff24ad43a6802a50c7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
export type Text = {
  kind: "text";
  content: string;
};

export type RawHTML = {
  kind: "raw";
  html: string;
};

export type Element = {
  kind: "element";
  name: string;
  attributes: Record<string, string>;
  children: Node[];
};

export type Node = Element | Text | RawHTML;

export function text(content: string): Text {
  return {
    kind: "text",
    content,
  };
}

export function rawHTML(html: string): RawHTML {
  return {
    kind: "raw",
    html,
  };
}

export function elem(
  name: string,
  attributes?: Record<string, string>,
  ...children: Node[]
): Element {
  return {
    kind: "element",
    name,
    attributes: attributes || {},
    children,
  };
}

export function addClass(e: Element, klass: string) {
  const classes = e.attributes.class;
  if (classes === undefined) {
    e.attributes.class = klass;
  } else {
    const classList = classes.split(" ");
    classList.push(klass);
    classList.sort();
    e.attributes.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);
}

export function forEachElementOfType(
  root: Element,
  elementName: string,
  f: (e: Element) => void,
) {
  forEachChildRecursively(root, (n) => {
    if (n.kind === "element" && n.name === elementName) {
      f(n);
    }
  });
}

export function processTextNodesInElement(
  e: Element,
  f: (text: string) => Node[],
) {
  const newChildren: Node[] = [];
  for (const child of e.children) {
    if (child.kind === "text") {
      newChildren.push(...f(child.content));
    } else {
      newChildren.push(child);
    }
  }
  e.children = newChildren;
}