mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
22 lines
684 B
TypeScript
22 lines
684 B
TypeScript
import { escape } from 'entities';
|
|
|
|
/**
|
|
* @param strings The constant portions of the template string.
|
|
* @param values The templated values.
|
|
* @returns The built HTML.
|
|
* @example
|
|
* ```
|
|
* const unsafe = `oops <script>alert(1)</script>`;
|
|
* testing.innerHTML = html`foo bar baz ${unsafe}`;
|
|
* console.assert(testing === "foo bar baz oops<script>alert(1)</script>");
|
|
* ```
|
|
*/
|
|
export function html(strings: TemplateStringsArray, ...values: (string | number)[]) {
|
|
const built = [];
|
|
for (let i = 0; i < strings.length; i++) {
|
|
built.push(strings[i] || '');
|
|
const val = values[i];
|
|
built.push(escape((val || '').toString()));
|
|
}
|
|
return built.join('');
|
|
}
|