Archived
1
0
Fork 0

add hooks

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-28 23:28:31 +03:00
parent c3014d690b
commit 0a70e34a13
2 changed files with 34 additions and 10 deletions

View file

@ -1,6 +1,7 @@
import { Nilable } from "./utils.ts"; import { Nilable } from "./utils.ts";
export type Attrs = Record<string, string>; export type Attrs = Record<string, string>;
export type AttrEntry = [key: string, value: string];
export type AnyNode = Fragment | FragmentNode; export type AnyNode = Fragment | FragmentNode;
export function isTextNode(val: unknown): val is TextNode { export function isTextNode(val: unknown): val is TextNode {

View file

@ -1,5 +1,6 @@
import { import {
AnyNode, AnyNode,
AttrEntry,
Attrs, Attrs,
Elem, Elem,
Fragment, Fragment,
@ -15,17 +16,31 @@ interface StrRendererOpts {
doctype?: string; doctype?: string;
forceRenderDoctype?: boolean; forceRenderDoctype?: boolean;
wrapNode?: (node: AnyNode) => AnyNode; wrapNode?: (node: AnyNode) => AnyNode;
onVisitAttr?: (entry: AttrEntry, params: OnVisitAttrParams) => AttrEntry;
}
interface StrRendererHooks {
onVisitAttr: (entry: AttrEntry, params: OnVisitAttrParams) => AttrEntry;
}
export interface OnVisitAttrParams {
readonly tagName: string;
readonly attrs: Attrs;
} }
export class StrRenderer implements Renderer<string> { export class StrRenderer implements Renderer<string> {
#doctype: string; #doctype: string;
#forceRenderDoctype: boolean; #forceRenderDoctype: boolean;
#wrapNode: (node: AnyNode) => AnyNode; #wrapNode: (node: AnyNode) => AnyNode;
#hooks: StrRendererHooks;
constructor(opts?: StrRendererOpts) { constructor(opts?: StrRendererOpts) {
this.#doctype = opts?.doctype ?? "html"; this.#doctype = opts?.doctype ?? "html";
this.#forceRenderDoctype = opts?.forceRenderDoctype ?? false; this.#forceRenderDoctype = opts?.forceRenderDoctype ?? false;
this.#wrapNode = opts?.wrapNode ?? identity; this.#wrapNode = opts?.wrapNode ?? identity;
this.#hooks = {
onVisitAttr: opts?.onVisitAttr ?? identity,
};
} }
render(node: AnyNode): string { render(node: AnyNode): string {
@ -34,7 +49,7 @@ export class StrRenderer implements Renderer<string> {
(isElem(wrappedNode) && wrappedNode.tagName === "html"); (isElem(wrappedNode) && wrappedNode.tagName === "html");
return concat([ return concat([
shouldRenderDoctype && encodeDoctype(this.#doctype), shouldRenderDoctype && encodeDoctype(this.#doctype),
encodeAnyNode(wrappedNode), encodeAnyNode(wrappedNode, this.#hooks),
]); ]);
} }
} }
@ -47,36 +62,44 @@ function encodeDoctype(value: string): string {
return `<!doctype ${value}>`; return `<!doctype ${value}>`;
} }
function encodeAnyNode(node: AnyNode): string { function encodeAnyNode(node: AnyNode, hooks: StrRendererHooks): string {
return isTextNode(node) return isTextNode(node)
? encodeTextNode(node) ? encodeTextNode(node)
: isFragment(node) : isFragment(node)
? encodeHtmlFragment(node) ? encodeHtmlFragment(node, hooks)
: encodeHtmlElement(node); : encodeHtmlElement(node, hooks);
} }
function encodeTextNode(node: TextNode): string { function encodeTextNode(node: TextNode): string {
return node.innerText; return node.innerText;
} }
function encodeHtmlFragment(node: Fragment): string { function encodeHtmlFragment(node: Fragment, hooks: StrRendererHooks): string {
return concat(node.children.map(encodeAnyNode)); return concat(node.children.map((ch) => encodeAnyNode(ch, hooks)));
} }
function encodeHtmlElement( function encodeHtmlElement(
{ tagName, attrs, children }: Elem, { tagName, attrs, children }: Elem,
hooks: StrRendererHooks,
): string { ): string {
const open = `<${join(" ", [tagName, encodeAttrs(attrs)])}>`; const open = `<${join(" ", [tagName, encodeAttrs(tagName, attrs, hooks)])}>`;
if (isSelfClosedTagName(tagName)) return open; if (isSelfClosedTagName(tagName)) return open;
const encodedChildren = children.map(encodeAnyNode); const encodedChildren = children.map((ch) => encodeAnyNode(ch, hooks));
return `${open}${concat(encodedChildren)}</${tagName}>`; return `${open}${concat(encodedChildren)}</${tagName}>`;
} }
function encodeAttrs(attrs: Attrs): string { function encodeAttrs(
tagName: string,
attrs: Attrs,
hooks: StrRendererHooks,
): string {
return join( return join(
" ", " ",
Object.entries(attrs).map(([key, value]) => encodeAttr(key, value)), Object.entries(attrs).map((entry) => {
const [key, value] = hooks.onVisitAttr(entry, { tagName, attrs });
return encodeAttr(key, value);
}),
); );
} }