diff --git a/ren/str.test.ts b/ren/html_str.test.ts similarity index 85% rename from ren/str.test.ts rename to ren/html_str.test.ts index 891919b..823dcb1 100644 --- a/ren/str.test.ts +++ b/ren/html_str.test.ts @@ -2,14 +2,14 @@ import { assertEquals } from "testing/asserts.ts"; import { TextNode } from "../core/node.ts"; import { E, F } from "./node.ts"; -import { StrRenderer } from "./str.ts"; +import { HtmlStrRenderer } from "./html_str.ts"; Deno.test({ name: "should render text node", fn: () => { const tn = new TextNode("hello world"); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(tn); assertEquals(res, "hello world"); @@ -21,7 +21,7 @@ Deno.test({ fn: () => { const el = E("p", [], "hello world"); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(el); assertEquals(res, "

hello world

"); @@ -33,7 +33,7 @@ Deno.test({ fn: () => { const frag = F([]); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(frag); assertEquals(res, ""); @@ -49,7 +49,7 @@ Deno.test({ E("p", [], "world"), ]); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(frag); assertEquals(res, 'hello world

world

'); @@ -65,7 +65,7 @@ Deno.test({ ]), ]); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(layout); assertEquals(res, '

hello world

'); @@ -81,7 +81,7 @@ Deno.test({ ]), ]); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(layout); assertEquals( @@ -96,7 +96,7 @@ Deno.test({ fn: () => { const layout = E("html", []); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(layout); assertEquals(res, ""); @@ -108,7 +108,7 @@ Deno.test({ fn: () => { const layout = E("html", []); - const ren = new StrRenderer({ + const ren = new HtmlStrRenderer({ doctype: 'html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"', }); @@ -126,7 +126,7 @@ Deno.test({ fn: () => { const layout = E("body", [], []); - const ren = new StrRenderer({ forceRenderDoctype: true }); + const ren = new HtmlStrRenderer({ forceRenderDoctype: true }); const res = ren.render(layout); assertEquals(res, ""); @@ -138,7 +138,9 @@ Deno.test({ fn: () => { const layout = E("body", [], []); - const ren = new StrRenderer({ wrapNode: (node) => E("html", [], [node]) }); + const ren = new HtmlStrRenderer({ + wrapNode: (node) => E("html", [], [node]), + }); const res = ren.render(layout); assertEquals(res, ""); @@ -154,7 +156,7 @@ Deno.test({ rel: "nofollow noopener", }, "hello world"); - const ren = new StrRenderer({ + const ren = new HtmlStrRenderer({ onVisitAttr: ([key, val]) => [`data-${key}`, val], }); const res = ren.render(layout); @@ -173,7 +175,7 @@ Deno.test({ href: "/hello/world", }, "hello world"); - const ren = new StrRenderer({ + const ren = new HtmlStrRenderer({ onVisitAttr: ([key, val]) => [key, "/eng" + val], }); const res = ren.render(layout); @@ -190,7 +192,7 @@ Deno.test({ fn: () => { const layout = E("input", { type: "number", disabled: false }); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(layout); assertEquals( @@ -205,7 +207,7 @@ Deno.test({ fn: () => { const layout = E("input", { type: "number", disabled: true }); - const ren = new StrRenderer(); + const ren = new HtmlStrRenderer(); const res = ren.render(layout); assertEquals( diff --git a/ren/str.ts b/ren/html_str.ts similarity index 86% rename from ren/str.ts rename to ren/html_str.ts index bd0685d..5aa279c 100644 --- a/ren/str.ts +++ b/ren/html_str.ts @@ -13,14 +13,14 @@ import { import { concat, join } from "../core/utils.ts"; import { Renderer } from "./types.ts"; -interface StrRendererOpts { +interface HtmlStrRendererOpts { doctype?: string; forceRenderDoctype?: boolean; wrapNode?: (node: AnyNode) => AnyNode; onVisitAttr?: (entry: AttrEntry, params: OnVisitAttrParams) => AttrEntry; } -interface StrRendererHooks { +interface HtmlStrRendererHooks { onVisitAttr: (entry: AttrEntry, params: OnVisitAttrParams) => AttrEntry; } @@ -29,13 +29,13 @@ export interface OnVisitAttrParams { readonly attrs: Attrs; } -export class StrRenderer implements Renderer { +export class HtmlStrRenderer implements Renderer { #doctype: string; #forceRenderDoctype: boolean; #wrapNode: (node: AnyNode) => AnyNode; - #hooks: StrRendererHooks; + #hooks: HtmlStrRendererHooks; - constructor(opts?: StrRendererOpts) { + constructor(opts?: HtmlStrRendererOpts) { this.#doctype = opts?.doctype ?? "html"; this.#forceRenderDoctype = opts?.forceRenderDoctype ?? false; this.#wrapNode = opts?.wrapNode ?? identity; @@ -63,7 +63,7 @@ function encodeDoctype(value: string): string { return ``; } -function encodeAnyNode(node: AnyNode, hooks: StrRendererHooks): string { +function encodeAnyNode(node: AnyNode, hooks: HtmlStrRendererHooks): string { return isTextNode(node) ? encodeTextNode(node) : isFragment(node) @@ -75,13 +75,16 @@ function encodeTextNode(node: TextNode): string { return node.innerText; } -function encodeHtmlFragment(node: Fragment, hooks: StrRendererHooks): string { +function encodeHtmlFragment( + node: Fragment, + hooks: HtmlStrRendererHooks, +): string { return concat(node.children.map((ch) => encodeAnyNode(ch, hooks))); } function encodeHtmlElement( { tagName, attrs, children }: Elem, - hooks: StrRendererHooks, + hooks: HtmlStrRendererHooks, ): string { const open = `<${join(" ", [tagName, encodeAttrs(tagName, attrs, hooks)])}>`; if (isSelfClosedTagName(tagName)) return open; @@ -93,7 +96,7 @@ function encodeHtmlElement( function encodeAttrs( tagName: string, attrs: Attrs, - hooks: StrRendererHooks, + hooks: HtmlStrRendererHooks, ): string { return join( " ",