jsx-runtime.ts
This file
Path
src/jsx/jsx-runtime.ts
51 lines · 1.7 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–51why, one step back1/**2 * @module3 * This module provides Hono's JSX runtime.4 */56export { jsxDEV as jsx, Fragment } from './jsx-dev-runtime'7export { jsxDEV as jsxs } from './jsx-dev-runtime'8export type { JSX } from './jsx-dev-runtime'9import { html, raw } from '../helper/html'10import type { HtmlEscapedString, StringBuffer, HtmlEscaped } from '../utils/html'11import { escapeToBuffer, stringBufferToString } from '../utils/html'12import { isValidAttributeName, styleObjectForEach } from './utils'1314export { html as jsxTemplate }1516export const jsxAttr = (17 key: string,18 v: string | Promise<string> | Record<string, string | number | null | undefined | boolean>19): HtmlEscapedString | Promise<HtmlEscapedString> => {20 if (!isValidAttributeName(key)) {21 return raw('')22 }23 const buffer: StringBuffer = [`${key}="`] as StringBuffer24 if (key === 'style' && typeof v === 'object') {25 // object to style strings26 let styleStr = ''27 styleObjectForEach(v as Record<string, string | number>, (property, value) => {28 if (value != null) {29 styleStr += `${styleStr ? ';' : ''}${property}:${value}`30 }31 })32 escapeToBuffer(styleStr, buffer)33 buffer[0] += '"'34 } else if (typeof v === 'string') {35 escapeToBuffer(v, buffer)36 buffer[0] += '"'37 } else if (v === null || v === undefined) {38 return raw('')39 } else if (typeof v === 'number' || (v as unknown as HtmlEscaped).isEscaped) {40 buffer[0] += `${v}"`41 } else if (v instanceof Promise) {42 buffer.unshift('"', v)43 } else {44 escapeToBuffer(v.toString(), buffer)45 buffer[0] += '"'46 }4748 return buffer.length === 1 ? raw(buffer[0]) : stringBufferToString(buffer, undefined)49}5051export const jsxEscape = (value: string) => value