base.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. // If the importer is in node compatibility mode or this is not an ESM
  21. // file that has been converted to a CommonJS file using a Babel-
  22. // compatible transform (i.e. "__esModule" has not been set), then set
  23. // "default" to the CommonJS "module.exports" for node compatibility.
  24. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  25. mod
  26. ));
  27. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  28. var base_exports = {};
  29. __export(base_exports, {
  30. Fragment: () => Fragment,
  31. JSXFragmentNode: () => JSXFragmentNode,
  32. JSXNode: () => JSXNode,
  33. booleanAttributes: () => booleanAttributes,
  34. cloneElement: () => cloneElement,
  35. getNameSpaceContext: () => getNameSpaceContext,
  36. isValidElement: () => isValidElement,
  37. jsx: () => jsx,
  38. jsxFn: () => jsxFn,
  39. memo: () => memo,
  40. reactAPICompatVersion: () => reactAPICompatVersion,
  41. shallowEqual: () => shallowEqual
  42. });
  43. module.exports = __toCommonJS(base_exports);
  44. var import_html = require("../helper/html");
  45. var import_html2 = require("../utils/html");
  46. var import_constants = require("./constants");
  47. var import_context = require("./context");
  48. var import_common = require("./intrinsic-element/common");
  49. var intrinsicElementTags = __toESM(require("./intrinsic-element/components"), 1);
  50. var import_utils = require("./utils");
  51. let nameSpaceContext = void 0;
  52. const getNameSpaceContext = () => nameSpaceContext;
  53. const toSVGAttributeName = (key) => /[A-Z]/.test(key) && // Presentation attributes are findable in style object. "clip-path", "font-size", "stroke-width", etc.
  54. // Or other un-deprecated kebab-case attributes. "overline-position", "paint-order", "strikethrough-position", etc.
  55. key.match(
  56. /^(?: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)/
  57. ) ? key.replace(/([A-Z])/g, "-$1").toLowerCase() : key;
  58. const emptyTags = [
  59. "area",
  60. "base",
  61. "br",
  62. "col",
  63. "embed",
  64. "hr",
  65. "img",
  66. "input",
  67. "keygen",
  68. "link",
  69. "meta",
  70. "param",
  71. "source",
  72. "track",
  73. "wbr"
  74. ];
  75. const booleanAttributes = [
  76. "allowfullscreen",
  77. "async",
  78. "autofocus",
  79. "autoplay",
  80. "checked",
  81. "controls",
  82. "default",
  83. "defer",
  84. "disabled",
  85. "download",
  86. "formnovalidate",
  87. "hidden",
  88. "inert",
  89. "ismap",
  90. "itemscope",
  91. "loop",
  92. "multiple",
  93. "muted",
  94. "nomodule",
  95. "novalidate",
  96. "open",
  97. "playsinline",
  98. "readonly",
  99. "required",
  100. "reversed",
  101. "selected"
  102. ];
  103. const childrenToStringToBuffer = (children, buffer) => {
  104. for (let i = 0, len = children.length; i < len; i++) {
  105. const child = children[i];
  106. if (typeof child === "string") {
  107. (0, import_html2.escapeToBuffer)(child, buffer);
  108. } else if (typeof child === "boolean" || child === null || child === void 0) {
  109. continue;
  110. } else if (child instanceof JSXNode) {
  111. child.toStringToBuffer(buffer);
  112. } else if (typeof child === "number" || child.isEscaped) {
  113. ;
  114. buffer[0] += child;
  115. } else if (child instanceof Promise) {
  116. buffer.unshift("", child);
  117. } else {
  118. childrenToStringToBuffer(child, buffer);
  119. }
  120. }
  121. };
  122. class JSXNode {
  123. tag;
  124. props;
  125. key;
  126. children;
  127. isEscaped = true;
  128. localContexts;
  129. constructor(tag, props, children) {
  130. this.tag = tag;
  131. this.props = props;
  132. this.children = children;
  133. }
  134. get type() {
  135. return this.tag;
  136. }
  137. // Added for compatibility with libraries that rely on React's internal structure
  138. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  139. get ref() {
  140. return this.props.ref || null;
  141. }
  142. toString() {
  143. const buffer = [""];
  144. this.localContexts?.forEach(([context, value]) => {
  145. context.values.push(value);
  146. });
  147. try {
  148. this.toStringToBuffer(buffer);
  149. } finally {
  150. this.localContexts?.forEach(([context]) => {
  151. context.values.pop();
  152. });
  153. }
  154. return buffer.length === 1 ? "callbacks" in buffer ? (0, import_html2.resolveCallbackSync)((0, import_html.raw)(buffer[0], buffer.callbacks)).toString() : buffer[0] : (0, import_html2.stringBufferToString)(buffer, buffer.callbacks);
  155. }
  156. toStringToBuffer(buffer) {
  157. const tag = this.tag;
  158. const props = this.props;
  159. let { children } = this;
  160. buffer[0] += `<${tag}`;
  161. const normalizeKey = nameSpaceContext && (0, import_context.useContext)(nameSpaceContext) === "svg" ? (key) => toSVGAttributeName((0, import_utils.normalizeIntrinsicElementKey)(key)) : (key) => (0, import_utils.normalizeIntrinsicElementKey)(key);
  162. for (let [key, v] of Object.entries(props)) {
  163. key = normalizeKey(key);
  164. if (key === "children") {
  165. } else if (key === "style" && typeof v === "object") {
  166. let styleStr = "";
  167. (0, import_utils.styleObjectForEach)(v, (property, value) => {
  168. if (value != null) {
  169. styleStr += `${styleStr ? ";" : ""}${property}:${value}`;
  170. }
  171. });
  172. buffer[0] += ' style="';
  173. (0, import_html2.escapeToBuffer)(styleStr, buffer);
  174. buffer[0] += '"';
  175. } else if (typeof v === "string") {
  176. buffer[0] += ` ${key}="`;
  177. (0, import_html2.escapeToBuffer)(v, buffer);
  178. buffer[0] += '"';
  179. } else if (v === null || v === void 0) {
  180. } else if (typeof v === "number" || v.isEscaped) {
  181. buffer[0] += ` ${key}="${v}"`;
  182. } else if (typeof v === "boolean" && booleanAttributes.includes(key)) {
  183. if (v) {
  184. buffer[0] += ` ${key}=""`;
  185. }
  186. } else if (key === "dangerouslySetInnerHTML") {
  187. if (children.length > 0) {
  188. throw new Error("Can only set one of `children` or `props.dangerouslySetInnerHTML`.");
  189. }
  190. children = [(0, import_html.raw)(v.__html)];
  191. } else if (v instanceof Promise) {
  192. buffer[0] += ` ${key}="`;
  193. buffer.unshift('"', v);
  194. } else if (typeof v === "function") {
  195. if (!key.startsWith("on") && key !== "ref") {
  196. throw new Error(`Invalid prop '${key}' of type 'function' supplied to '${tag}'.`);
  197. }
  198. } else {
  199. buffer[0] += ` ${key}="`;
  200. (0, import_html2.escapeToBuffer)(v.toString(), buffer);
  201. buffer[0] += '"';
  202. }
  203. }
  204. if (emptyTags.includes(tag) && children.length === 0) {
  205. buffer[0] += "/>";
  206. return;
  207. }
  208. buffer[0] += ">";
  209. childrenToStringToBuffer(children, buffer);
  210. buffer[0] += `</${tag}>`;
  211. }
  212. }
  213. class JSXFunctionNode extends JSXNode {
  214. toStringToBuffer(buffer) {
  215. const { children } = this;
  216. const props = { ...this.props };
  217. if (children.length) {
  218. props.children = children.length === 1 ? children[0] : children;
  219. }
  220. const res = this.tag.call(null, props);
  221. if (typeof res === "boolean" || res == null) {
  222. return;
  223. } else if (res instanceof Promise) {
  224. if (import_context.globalContexts.length === 0) {
  225. buffer.unshift("", res);
  226. } else {
  227. const currentContexts = import_context.globalContexts.map((c) => [c, c.values.at(-1)]);
  228. buffer.unshift(
  229. "",
  230. res.then((childRes) => {
  231. if (childRes instanceof JSXNode) {
  232. childRes.localContexts = currentContexts;
  233. }
  234. return childRes;
  235. })
  236. );
  237. }
  238. } else if (res instanceof JSXNode) {
  239. res.toStringToBuffer(buffer);
  240. } else if (typeof res === "number" || res.isEscaped) {
  241. buffer[0] += res;
  242. if (res.callbacks) {
  243. buffer.callbacks ||= [];
  244. buffer.callbacks.push(...res.callbacks);
  245. }
  246. } else {
  247. (0, import_html2.escapeToBuffer)(res, buffer);
  248. }
  249. }
  250. }
  251. class JSXFragmentNode extends JSXNode {
  252. toStringToBuffer(buffer) {
  253. childrenToStringToBuffer(this.children, buffer);
  254. }
  255. }
  256. const jsx = (tag, props, ...children) => {
  257. props ??= {};
  258. if (children.length) {
  259. props.children = children.length === 1 ? children[0] : children;
  260. }
  261. const key = props.key;
  262. delete props["key"];
  263. const node = jsxFn(tag, props, children);
  264. node.key = key;
  265. return node;
  266. };
  267. let initDomRenderer = false;
  268. const jsxFn = (tag, props, children) => {
  269. if (!initDomRenderer) {
  270. for (const k in import_common.domRenderers) {
  271. ;
  272. intrinsicElementTags[k][import_constants.DOM_RENDERER] = import_common.domRenderers[k];
  273. }
  274. initDomRenderer = true;
  275. }
  276. if (typeof tag === "function") {
  277. return new JSXFunctionNode(tag, props, children);
  278. } else if (intrinsicElementTags[tag]) {
  279. return new JSXFunctionNode(
  280. intrinsicElementTags[tag],
  281. props,
  282. children
  283. );
  284. } else if (tag === "svg" || tag === "head") {
  285. nameSpaceContext ||= (0, import_context.createContext)("");
  286. return new JSXNode(tag, props, [
  287. new JSXFunctionNode(
  288. nameSpaceContext,
  289. {
  290. value: tag
  291. },
  292. children
  293. )
  294. ]);
  295. } else {
  296. return new JSXNode(tag, props, children);
  297. }
  298. };
  299. const shallowEqual = (a, b) => {
  300. if (a === b) {
  301. return true;
  302. }
  303. const aKeys = Object.keys(a).sort();
  304. const bKeys = Object.keys(b).sort();
  305. if (aKeys.length !== bKeys.length) {
  306. return false;
  307. }
  308. for (let i = 0, len = aKeys.length; i < len; i++) {
  309. if (aKeys[i] === "children" && bKeys[i] === "children" && !a.children?.length && !b.children?.length) {
  310. continue;
  311. } else if (a[aKeys[i]] !== b[aKeys[i]]) {
  312. return false;
  313. }
  314. }
  315. return true;
  316. };
  317. const memo = (component, propsAreEqual = shallowEqual) => {
  318. let computed = null;
  319. let prevProps = void 0;
  320. const wrapper = ((props) => {
  321. if (prevProps && !propsAreEqual(prevProps, props)) {
  322. computed = null;
  323. }
  324. prevProps = props;
  325. return computed ||= component(props);
  326. });
  327. wrapper[import_constants.DOM_MEMO] = propsAreEqual;
  328. wrapper[import_constants.DOM_RENDERER] = component;
  329. return wrapper;
  330. };
  331. const Fragment = ({
  332. children
  333. }) => {
  334. return new JSXFragmentNode(
  335. "",
  336. {
  337. children
  338. },
  339. Array.isArray(children) ? children : children ? [children] : []
  340. );
  341. };
  342. const isValidElement = (element) => {
  343. return !!(element && typeof element === "object" && "tag" in element && "props" in element);
  344. };
  345. const cloneElement = (element, props, ...children) => {
  346. let childrenToClone;
  347. if (children.length > 0) {
  348. childrenToClone = children;
  349. } else {
  350. const c = element.props.children;
  351. childrenToClone = Array.isArray(c) ? c : [c];
  352. }
  353. return jsx(
  354. element.tag,
  355. { ...element.props, ...props },
  356. ...childrenToClone
  357. );
  358. };
  359. const reactAPICompatVersion = "19.0.0-hono-jsx";
  360. // Annotate the CommonJS export names for ESM import in node:
  361. 0 && (module.exports = {
  362. Fragment,
  363. JSXFragmentNode,
  364. JSXNode,
  365. booleanAttributes,
  366. cloneElement,
  367. getNameSpaceContext,
  368. isValidElement,
  369. jsx,
  370. jsxFn,
  371. memo,
  372. reactAPICompatVersion,
  373. shallowEqual
  374. });