File size: 10,253 Bytes
edaa6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { escapeToBuffer, raw, resolveCallbackSync, stringBufferToString } from "../utils/html.js";
import { DOM_MEMO, DOM_RENDERER, RENDERED_JSX } from "./constants.js";
import { captureRenderContext, createContext, globalContexts, runWithRenderContext, useContext } from "./context.js";
import { domRenderers } from "./intrinsic-element/common.js";
import { components_exports } from "./intrinsic-element/components.js";
import { isValidAttributeName, isValidTagName, normalizeIntrinsicElementKey, styleObjectForEach } from "./utils.js";
//#region src/jsx/base.ts
let nameSpaceContext = void 0;
const getNameSpaceContext = () => nameSpaceContext;
const toSVGAttributeName = (key) => /[A-Z]/.test(key) && key.match(/^(?:al|basel|clip(?:Path|Rule)$|co|do|fill|fl|fo|gl|let|lig|i|marker[EMS]|o|pai|pointe|sh|st[or]|text[^L]|tr|u|ve|w)/) ? key.replace(/([A-Z])/g, "-$1").toLowerCase() : key;
const emptyTags = [
	"area",
	"base",
	"br",
	"col",
	"embed",
	"hr",
	"img",
	"input",
	"keygen",
	"link",
	"meta",
	"param",
	"source",
	"track",
	"wbr"
];
const booleanAttributes = /* @__PURE__ */ new Set([
	"allowfullscreen",
	"async",
	"autofocus",
	"autoplay",
	"checked",
	"controls",
	"default",
	"defer",
	"disabled",
	"download",
	"formnovalidate",
	"hidden",
	"inert",
	"ismap",
	"itemscope",
	"loop",
	"multiple",
	"muted",
	"nomodule",
	"novalidate",
	"open",
	"playsinline",
	"readonly",
	"required",
	"reversed",
	"selected",
	"shadowrootclonable",
	"shadowrootdelegatesfocus",
	"shadowrootserializable"
]);
const resolveFunctionComponentResult = (result, suspendedContext) => result.then((resolved) => {
	if (typeof resolved !== "string" && !Array.isArray(resolved) && !(resolved instanceof JSXNode)) return resolved;
	const children = Array.isArray(resolved) ? resolved : [resolved];
	const render = () => {
		const buffer = [""];
		childrenToStringToBuffer(children, buffer);
		return buffer.length === 1 ? raw(buffer[0], buffer.callbacks) : stringBufferToString(buffer, buffer.callbacks);
	};
	return suspendedContext ? suspendedContext(render) : runWithRenderContext(render);
});
const markRenderedJSX = (value) => {
	value[RENDERED_JSX] = true;
	return value;
};
const isEscapedAttribute = (value) => value instanceof String && value.isEscaped && !(RENDERED_JSX in value);
const objectToAttributeString = (v) => {
	const s = v.toString();
	return s instanceof Promise ? s.then(String) : String(s);
};
const attributeToBuffer = (buffer, prefix, key, v, tag) => {
	if (key === "ref" || v === null || v === void 0) return;
	if (typeof v === "boolean" && booleanAttributes.has(key)) {
		if (v) buffer[0] += `${prefix}${key}=""`;
		return;
	}
	if (typeof v === "function") {
		if (!key.startsWith("on")) throw new Error(`Invalid prop '${key}' of type 'function' supplied to ${tag ? `'${tag}'` : "a JSX element"}.`);
		return;
	}
	buffer[0] += `${prefix}${key}="`;
	if (key === "style" && typeof v === "object") {
		let styleStr = "";
		styleObjectForEach(v, (property, value) => {
			if (value != null) styleStr += `${styleStr ? ";" : ""}${property}:${value}`;
		});
		escapeToBuffer(styleStr, buffer);
	} else if (typeof v === "string") escapeToBuffer(v, buffer);
	else if (typeof v === "number" || isEscapedAttribute(v)) buffer[0] += `${v}`;
	else {
		const s = v instanceof Promise ? v.then((r) => typeof r === "object" && r !== null && !isEscapedAttribute(r) ? objectToAttributeString(r) : r) : objectToAttributeString(v);
		if (s instanceof Promise) {
			buffer.unshift("\"", s);
			return;
		}
		escapeToBuffer(s, buffer);
	}
	buffer[0] += "\"";
};
const childrenToStringToBuffer = (children, buffer) => {
	for (let i = 0, len = children.length; i < len; i++) {
		const child = children[i];
		if (typeof child === "string") escapeToBuffer(child, buffer);
		else if (typeof child === "boolean" || child === null || child === void 0) continue;
		else if (child instanceof JSXNode) child.toStringToBuffer(buffer);
		else if (typeof child === "number") buffer[0] += child;
		else if (child.isEscaped) {
			buffer[0] += child;
			const callbacks = child.callbacks;
			if (callbacks) {
				buffer.callbacks ||= [];
				buffer.callbacks.push(...callbacks);
			}
		} else if (child instanceof Promise) buffer.unshift("", child);
		else if (Array.isArray(child)) childrenToStringToBuffer(child, buffer);
	}
};
const renderChildren = (children) => runWithRenderContext(() => {
	const buffer = [""];
	childrenToStringToBuffer(children, buffer);
	return buffer.length === 1 ? raw(buffer[0], buffer.callbacks) : stringBufferToString(buffer, buffer.callbacks);
});
const isUntrustedObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof JSXNode) && !(value instanceof Promise) && !value.isEscaped && typeof value.toString === "function";
const renderUntrustedObject = (value) => {
	const stringified = value.toString();
	const escape = (result) => renderChildren([String(result)]);
	return stringified instanceof Promise ? stringified.then(escape) : escape(stringified);
};
var JSXNode = class {
	tag;
	props;
	key;
	children;
	isEscaped = true;
	constructor(tag, props, children) {
		if (typeof tag !== "function" && !isValidTagName(tag)) throw new Error(`Invalid JSX tag name: ${tag}`);
		this.tag = tag;
		this.props = props;
		this.children = children;
	}
	get type() {
		return this.tag;
	}
	get ref() {
		return this.props.ref || null;
	}
	toString() {
		const render = () => {
			const buffer = [""];
			this.toStringToBuffer(buffer);
			return buffer.length === 1 ? "callbacks" in buffer ? resolveCallbackSync(raw(buffer[0], buffer.callbacks)).toString() : buffer[0] : stringBufferToString(buffer, buffer.callbacks).then(markRenderedJSX);
		};
		return runWithRenderContext(render);
	}
	toStringToBuffer(buffer) {
		const tag = this.tag;
		const props = this.props;
		let { children } = this;
		buffer[0] += `<${tag}`;
		const normalizeKey = tag === "svg" || nameSpaceContext && useContext(nameSpaceContext) === "svg" ? (key) => toSVGAttributeName(normalizeIntrinsicElementKey(key)) : (key) => normalizeIntrinsicElementKey(key);
		for (let [key, v] of Object.entries(props)) {
			key = normalizeKey(key);
			if (!isValidAttributeName(key)) continue;
			if (key === "children") {} else if (key === "dangerouslySetInnerHTML" && typeof v === "object" && v !== null && "__html" in v) {
				if (children.length > 0) throw new Error("Can only set one of `children` or `props.dangerouslySetInnerHTML`.");
				children = [raw(v.__html)];
			} else attributeToBuffer(buffer, " ", key, v, tag);
		}
		if (emptyTags.includes(tag) && children.length === 0) {
			buffer[0] += "/>";
			return;
		}
		buffer[0] += ">";
		childrenToStringToBuffer(children, buffer);
		buffer[0] += `</${tag}>`;
	}
};
var JSXFunctionNode = class extends JSXNode {
	toStringToBuffer(buffer) {
		const { children } = this;
		const props = { ...this.props };
		if (children.length) props.children = children.length === 1 ? children[0] : children;
		const res = this.tag.call(null, props);
		if (typeof res === "boolean" || res == null) return;
		else if (res instanceof Promise) {
			if (globalContexts.length === 0) buffer.unshift("", resolveFunctionComponentResult(res));
			else buffer.unshift("", resolveFunctionComponentResult(res, captureRenderContext()));
		} else if (res instanceof JSXNode) res.toStringToBuffer(buffer);
		else if (Array.isArray(res)) childrenToStringToBuffer(res, buffer);
		else if (typeof res === "number" || res.isEscaped) {
			buffer[0] += res;
			if (res.callbacks) {
				buffer.callbacks ||= [];
				buffer.callbacks.push(...res.callbacks);
			}
		} else escapeToBuffer(res, buffer);
	}
};
var JSXFragmentNode = class extends JSXNode {
	toStringToBuffer(buffer) {
		childrenToStringToBuffer(this.children, buffer);
	}
};
const jsx = (tag, props, ...children) => {
	props ??= {};
	if (children.length) props.children = children.length === 1 ? children[0] : children;
	const key = props.key;
	delete props["key"];
	const node = jsxFn(tag, props, children);
	node.key = key;
	return node;
};
let initDomRenderer = false;
const jsxFn = (tag, props, children) => {
	if (!initDomRenderer) {
		for (const k in domRenderers) components_exports[k][DOM_RENDERER] = domRenderers[k];
		initDomRenderer = true;
	}
	if (typeof tag === "function") return new JSXFunctionNode(tag, props, children);
	else if (components_exports[tag]) return new JSXFunctionNode(components_exports[tag], props, children);
	else if (tag === "svg" || tag === "head") {
		nameSpaceContext ||= createContext("");
		return new JSXNode(tag, props, [new JSXFunctionNode(nameSpaceContext, { value: tag }, children)]);
	} else return new JSXNode(tag, props, children);
};
const shallowEqual = (a, b) => {
	if (a === b) return true;
	const aKeys = Object.keys(a).sort();
	const bKeys = Object.keys(b).sort();
	if (aKeys.length !== bKeys.length) return false;
	for (let i = 0, len = aKeys.length; i < len; i++) if (aKeys[i] === "children" && bKeys[i] === "children" && !a.children?.length && !b.children?.length) continue;
	else if (a[aKeys[i]] !== b[aKeys[i]]) return false;
	return true;
};
const memo = (component, propsAreEqual = shallowEqual) => {
	const wrapper = ((props) => component(props));
	wrapper[DOM_MEMO] = propsAreEqual;
	wrapper[DOM_RENDERER] = component;
	return wrapper;
};
const Fragment = ({ children }) => {
	return new JSXFragmentNode("", { children }, Array.isArray(children) ? children : children ? [children] : []);
};
const isValidElement = (element) => {
	return !!(element && typeof element === "object" && "tag" in element && "props" in element);
};
const cloneElement = (element, props, ...children) => {
	let childrenToClone;
	if (children.length > 0) childrenToClone = children;
	else {
		const c = element.props.children;
		childrenToClone = c !== void 0 ? Array.isArray(c) ? c : [c] : [];
	}
	return jsx(element.tag, {
		...element.props,
		...props
	}, ...childrenToClone);
};
const reactAPICompatVersion = "19.0.0-hono-jsx";
//#endregion
export { Fragment, JSXFragmentNode, JSXNode, attributeToBuffer, booleanAttributes, cloneElement, getNameSpaceContext, isUntrustedObject, isValidElement, jsx, jsxFn, markRenderedJSX, memo, reactAPICompatVersion, renderChildren, renderUntrustedObject, shallowEqual };