get rid of r() and RawHtml from html.ts

This commit is contained in:
Siddharth Singh 2024-08-06 01:51:43 +05:30
parent 16f4048604
commit 612c845f95
No known key found for this signature in database

View file

@ -1,29 +1,5 @@
import { escape } from 'entities'; import { escape } from 'entities';
interface RawHtml {
raw: true;
contents: string;
}
/**
* Options for r()
*/
interface RawHtmlOptions {
joiner?: string;
}
/**
* Prevent values from being escaped by html``.
* @param val Any value.
* @returns An object that tells html`` to not escape `val` while building the HTML string.
*/
export function r(val: any, options?: RawHtmlOptions): RawHtml {
return {
raw: true,
contents: Array.isArray(val) ? val.join(options?.joiner ?? ' ') : val.toString(),
};
}
/** /**
* @param strings The constant portions of the template string. * @param strings The constant portions of the template string.
* @param values The templated values. * @param values The templated values.
@ -35,16 +11,12 @@ export function r(val: any, options?: RawHtmlOptions): RawHtml {
* console.assert(testing === "foo bar baz oops<script>alert(1)</script>"); * console.assert(testing === "foo bar baz oops<script>alert(1)</script>");
* ``` * ```
*/ */
export function html(strings: TemplateStringsArray, ...values: (string | number | RawHtml)[]) { export function html(strings: TemplateStringsArray, ...values: (string | number)[]) {
const built = []; const built = [];
for (let i = 0; i < strings.length; i++) { for (let i = 0; i < strings.length; i++) {
built.push(strings[i] || ''); built.push(strings[i] || '');
const val = values[i]; const val = values[i];
if (typeof val !== 'undefined' && typeof val !== 'object') { built.push(escape((val || '').toString()));
built.push(escape((val || '').toString()));
} else {
built.push(val?.contents || '');
}
} }
return built.join(''); return built.join('');
} }