mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
get rid of r() and RawHtml from html.ts
This commit is contained in:
parent
16f4048604
commit
612c845f95
1 changed files with 2 additions and 30 deletions
|
|
@ -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('');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue