UNPKG

@worker-tools/html

Version:

HTML templating and streaming response library for Worker Runtimes such as Cloudflare Workers.

126 lines 5.36 kB
// deno-lint-ignore-file ban-types no-explicit-any var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _HTML_strings, _HTML_args, _UnsafeHTML_value, _Fallback_content, _Fallback_fallback; import { escapeHtml } from './deps/deno.land/x/escape_html@1.0.0/mod.js'; const isIterable = (x) => typeof x === 'object' && x !== null && Symbol.iterator in x; const isAsyncIterable = (x) => typeof x === 'object' && x !== null && Symbol.asyncIterator in x; async function* unpackContent(content) { const x = await content; if (x == null || x === '' || x === false) { yield ''; } else if (x instanceof AbstractHTML) { yield* x; } else if (isIterable(x)) { for (const xi of x) { yield* unpackContent(xi); } } else if (isAsyncIterable(x)) { for await (const xi of x) { yield* unpackContent(xi); } } else { yield escapeHtml(x); } } async function* unpack(content) { try { yield* unpackContent(typeof content === 'function' ? content() : content); } catch (err) { if (err instanceof AbstractHTML) yield* err; else throw err; } } export class AbstractHTML { } export class HTML extends AbstractHTML { constructor(strings, args) { super(); _HTML_strings.set(this, void 0); _HTML_args.set(this, void 0); __classPrivateFieldSet(this, _HTML_strings, strings, "f"); __classPrivateFieldSet(this, _HTML_args, args, "f"); } // async *[Symbol.asyncIterator]() { // return aInterleaveFlattenSecond(this.strings, map(unpack)(this.args)); // } async *[(_HTML_strings = new WeakMap(), _HTML_args = new WeakMap(), Symbol.asyncIterator)]() { const stringsIt = __classPrivateFieldGet(this, _HTML_strings, "f")[Symbol.iterator](); const argsIt = __classPrivateFieldGet(this, _HTML_args, "f")[Symbol.iterator](); while (true) { const { done: stringDone, value: string } = stringsIt.next(); if (stringDone) break; else yield string; const { done: argDone, value: arg } = argsIt.next(); if (argDone) break; else yield* unpack(arg); } const { done: stringDone, value: string } = stringsIt.next(); if (stringDone) return; else yield string; } } export class UnsafeHTML extends AbstractHTML { constructor(value) { super(); _UnsafeHTML_value.set(this, void 0); __classPrivateFieldSet(this, _UnsafeHTML_value, value || '', "f"); } async *[(_UnsafeHTML_value = new WeakMap(), Symbol.asyncIterator)]() { yield __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); } toString() { return __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); } toJSON() { return __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); } } export class Fallback extends AbstractHTML { constructor(content, fallback) { super(); _Fallback_content.set(this, void 0); _Fallback_fallback.set(this, void 0); __classPrivateFieldSet(this, _Fallback_content, content, "f"); __classPrivateFieldSet(this, _Fallback_fallback, fallback, "f"); } async *[(_Fallback_content = new WeakMap(), _Fallback_fallback = new WeakMap(), Symbol.asyncIterator)]() { try { yield* unpack(__classPrivateFieldGet(this, _Fallback_content, "f")); } catch (e) { yield* typeof __classPrivateFieldGet(this, _Fallback_fallback, "f") === 'function' ? __classPrivateFieldGet(this, _Fallback_fallback, "f").call(this, e) : __classPrivateFieldGet(this, _Fallback_fallback, "f"); } } } export function html(strings, ...args) { return new HTML(strings, args); } // For the purpose of generating strings, there is no difference between html and css // so we can export this alias here to help with syntax highlighting and avoid confusion. export { html as css, html as js }; export function fallback(content, fallback) { return new Fallback(content, fallback); } export function unsafeHTML(content) { return new UnsafeHTML(content); } //# sourceMappingURL=html.js.map