UNPKG

5.6 kBJavaScriptView Raw
1"use strict";
2// deno-lint-ignore-file ban-types no-explicit-any
3var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
4 if (kind === "m") throw new TypeError("Private method is not writable");
5 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
6 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");
7 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
8};
9var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
10 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
11 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");
12 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
13};
14var _HTML_strings, _HTML_args, _UnsafeHTML_value, _Fallback_content, _Fallback_fallback;
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.unsafeHTML = exports.fallback = exports.js = exports.css = exports.html = exports.Fallback = exports.UnsafeHTML = exports.HTML = exports.AbstractHTML = void 0;
17const mod_js_1 = require("./deps/deno.land/x/escape_html@1.0.0/mod.js");
18const isIterable = (x) => typeof x === 'object' && x !== null && Symbol.iterator in x;
19const isAsyncIterable = (x) => typeof x === 'object' && x !== null && Symbol.asyncIterator in x;
20async function* unpackContent(content) {
21 const x = await content;
22 if (x == null || x === '' || x === false) {
23 yield '';
24 }
25 else if (x instanceof AbstractHTML) {
26 yield* x;
27 }
28 else if (isIterable(x)) {
29 for (const xi of x) {
30 yield* unpackContent(xi);
31 }
32 }
33 else if (isAsyncIterable(x)) {
34 for await (const xi of x) {
35 yield* unpackContent(xi);
36 }
37 }
38 else {
39 yield (0, mod_js_1.escapeHtml)(x);
40 }
41}
42async function* unpack(content) {
43 try {
44 yield* unpackContent(typeof content === 'function' ? content() : content);
45 }
46 catch (err) {
47 if (err instanceof AbstractHTML)
48 yield* err;
49 else
50 throw err;
51 }
52}
53class AbstractHTML {
54}
55exports.AbstractHTML = AbstractHTML;
56class HTML extends AbstractHTML {
57 constructor(strings, args) {
58 super();
59 _HTML_strings.set(this, void 0);
60 _HTML_args.set(this, void 0);
61 __classPrivateFieldSet(this, _HTML_strings, strings, "f");
62 __classPrivateFieldSet(this, _HTML_args, args, "f");
63 }
64 // async *[Symbol.asyncIterator]() {
65 // return aInterleaveFlattenSecond(this.strings, map(unpack)(this.args));
66 // }
67 async *[(_HTML_strings = new WeakMap(), _HTML_args = new WeakMap(), Symbol.asyncIterator)]() {
68 const stringsIt = __classPrivateFieldGet(this, _HTML_strings, "f")[Symbol.iterator]();
69 const argsIt = __classPrivateFieldGet(this, _HTML_args, "f")[Symbol.iterator]();
70 while (true) {
71 const { done: stringDone, value: string } = stringsIt.next();
72 if (stringDone)
73 break;
74 else
75 yield string;
76 const { done: argDone, value: arg } = argsIt.next();
77 if (argDone)
78 break;
79 else
80 yield* unpack(arg);
81 }
82 const { done: stringDone, value: string } = stringsIt.next();
83 if (stringDone)
84 return;
85 else
86 yield string;
87 }
88}
89exports.HTML = HTML;
90class UnsafeHTML extends AbstractHTML {
91 constructor(value) {
92 super();
93 _UnsafeHTML_value.set(this, void 0);
94 __classPrivateFieldSet(this, _UnsafeHTML_value, value || '', "f");
95 }
96 async *[(_UnsafeHTML_value = new WeakMap(), Symbol.asyncIterator)]() { yield __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); }
97 toString() { return __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); }
98 toJSON() { return __classPrivateFieldGet(this, _UnsafeHTML_value, "f"); }
99}
100exports.UnsafeHTML = UnsafeHTML;
101class Fallback extends AbstractHTML {
102 constructor(content, fallback) {
103 super();
104 _Fallback_content.set(this, void 0);
105 _Fallback_fallback.set(this, void 0);
106 __classPrivateFieldSet(this, _Fallback_content, content, "f");
107 __classPrivateFieldSet(this, _Fallback_fallback, fallback, "f");
108 }
109 async *[(_Fallback_content = new WeakMap(), _Fallback_fallback = new WeakMap(), Symbol.asyncIterator)]() {
110 try {
111 yield* unpack(__classPrivateFieldGet(this, _Fallback_content, "f"));
112 }
113 catch (e) {
114 yield* typeof __classPrivateFieldGet(this, _Fallback_fallback, "f") === 'function'
115 ? __classPrivateFieldGet(this, _Fallback_fallback, "f").call(this, e)
116 : __classPrivateFieldGet(this, _Fallback_fallback, "f");
117 }
118 }
119}
120exports.Fallback = Fallback;
121function html(strings, ...args) {
122 return new HTML(strings, args);
123}
124exports.html = html;
125exports.css = html;
126exports.js = html;
127function fallback(content, fallback) {
128 return new Fallback(content, fallback);
129}
130exports.fallback = fallback;
131function unsafeHTML(content) {
132 return new UnsafeHTML(content);
133}
134exports.unsafeHTML = unsafeHTML;
135//# sourceMappingURL=html.js.map
\No newline at end of file