diff options
| author | nsfisis <nsfisis@gmail.com> | 2022-12-23 23:27:09 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-03-06 01:46:04 +0900 |
| commit | 88ba6cfe220216f371f8756921059fac51a21262 (patch) | |
| tree | f272db2a0a3340f103df6618f19a101e65941b37 /nuldoc-src/dom.ts | |
| parent | 8f988a6e899aed678406ddfac1be4ef105439274 (diff) | |
| download | blog.nsfisis.dev-88ba6cfe220216f371f8756921059fac51a21262.tar.gz blog.nsfisis.dev-88ba6cfe220216f371f8756921059fac51a21262.tar.zst blog.nsfisis.dev-88ba6cfe220216f371f8756921059fac51a21262.zip | |
AsciiDoc to DocBook
Diffstat (limited to 'nuldoc-src/dom.ts')
| -rw-r--r-- | nuldoc-src/dom.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/nuldoc-src/dom.ts b/nuldoc-src/dom.ts new file mode 100644 index 0000000..51ef25a --- /dev/null +++ b/nuldoc-src/dom.ts @@ -0,0 +1,79 @@ +export type Text = { + kind: "text"; + content: string; +}; + +export type Element = { + kind: "element"; + name: string; + attributes: Map<string, string>; + children: Node[]; +}; + +export type Node = Element | Text; + +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 removeChildElements(e: Element, name: string) { + e.children = e.children.filter((c) => + c.kind !== "element" || c.name !== name + ); +} + +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 function forEachChildRecursively(e: Element, f: (n: Node) => void) { + const g = (c: Node) => { + f(c); + if (c.kind === "element") { + forEachChild(c, g); + } + }; + forEachChild(e, g); +} |
