From c3014d690bc0357ab0e4b04a3fc132e2ea8b4fb0 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 22 May 2022 22:47:17 +0300 Subject: [PATCH] add possibility to wrap node --- .gitignore | 3 --- .vscode/settings.json | 3 --- ren/str.test.ts | 15 ++++++++++++++- ren/str.ts | 26 ++++++++++++++++++-------- 4 files changed, 32 insertions(+), 15 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 5bcf213..8c55080 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,5 @@ /* -# editors -!/.vscode - # makefile !/makefile diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ff30c44..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "editor.tabSize": 2 -} \ No newline at end of file diff --git a/ren/str.test.ts b/ren/str.test.ts index d44e9c9..2d0617e 100644 --- a/ren/str.test.ts +++ b/ren/str.test.ts @@ -1,5 +1,6 @@ import { assertEquals } from "testing/asserts.ts"; -import { E, F, TextNode } from "../core/node.ts"; +import { TextNode } from "../core/node.ts"; +import { E, F } from "./node.ts"; import { StrRenderer } from "./str.ts"; @@ -131,3 +132,15 @@ Deno.test({ assertEquals(res, ""); }, }); + +Deno.test({ + name: "should wrap node", + fn: () => { + const layout = E("body", [], []); + + const ren = new StrRenderer({ wrapNode: (node) => E("html", [], [node]) }); + const res = ren.render(layout); + + assertEquals(res, ""); + }, +}); diff --git a/ren/str.ts b/ren/str.ts index accbd3f..7a56b10 100644 --- a/ren/str.ts +++ b/ren/str.ts @@ -14,27 +14,37 @@ import { Renderer } from "./types.ts"; interface StrRendererOpts { doctype?: string; forceRenderDoctype?: boolean; + wrapNode?: (node: AnyNode) => AnyNode; } export class StrRenderer implements Renderer { - #opts: StrRendererOpts; + #doctype: string; + #forceRenderDoctype: boolean; + #wrapNode: (node: AnyNode) => AnyNode; constructor(opts?: StrRendererOpts) { - this.#opts = opts ?? {}; + this.#doctype = opts?.doctype ?? "html"; + this.#forceRenderDoctype = opts?.forceRenderDoctype ?? false; + this.#wrapNode = opts?.wrapNode ?? identity; } render(node: AnyNode): string { - const shouldRenderDoctype = this.#opts.forceRenderDoctype || - (isElem(node) && node.tagName === "html"); + const wrappedNode = this.#wrapNode(node); + const shouldRenderDoctype = this.#forceRenderDoctype || + (isElem(wrappedNode) && wrappedNode.tagName === "html"); return concat([ - shouldRenderDoctype && encodeDoctype(this.#opts.doctype), - encodeAnyNode(node), + shouldRenderDoctype && encodeDoctype(this.#doctype), + encodeAnyNode(wrappedNode), ]); } } -function encodeDoctype(value?: string): string { - return ``; +function identity(val: T): T { + return val; +} + +function encodeDoctype(value: string): string { + return ``; } function encodeAnyNode(node: AnyNode): string {