UNPKG

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