html.ts
This file
Path
src/utils/html.ts
150 lines · 4.0 KiB · TypeScript
Last changed by
Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
History of this fileTake it
The walk stopped after 200 commits: lines older than 8addfd4 point at it.
Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 1–7why, one step back1export const HtmlEscapedCallbackPhase = {2 Stringify: 1,3 BeforeStream: 2,4 Stream: 3,5} as const6type HtmlEscapedCallbackOpts = {7 buffer?: [string]Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 8why, one step back8 phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase]Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 9–11why, one step back9 context: object // An object unique to each JSX tree. This object is used as the WeakMap key.10}11export type HtmlEscapedCallback = (opts: HtmlEscapedCallbackOpts) => Promise<string> | undefinedchore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
lines 12–33why, one step back12export type HtmlEscaped = {13 isEscaped: true14 callbacks?: HtmlEscapedCallback[]15}16export type HtmlEscapedString = string & HtmlEscaped1718/**19 * StringBuffer contains string and Promise<string> alternately20 * The length of the array will be odd, the odd numbered element will be a string,21 * and the even numbered element will be a Promise<string>.22 * When concatenating into a single string, it must be processed from the tail.23 * @example24 * [25 * 'framework.',26 * Promise.resolve('ultra fast'),27 * 'a ',28 * Promise.resolve('is '),29 * 'Hono',30 * ]31 */32export type StringBuffer = (string | Promise<string>)[]33fix(html): Remove circular dependencies in `hono/html` (#2143)javascripterbefore adoptionf44c705
lines 34–40why, one step back34export const raw = (value: unknown, callbacks?: HtmlEscapedCallback[]): HtmlEscapedString => {35 const escapedString = new String(value) as HtmlEscapedString36 escapedString.isEscaped = true37 escapedString.callbacks = callbacks3839 return escapedString40}chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
lines 41–118why, one step back4142// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.43// https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/escapeTextForBrowser.js4445const escapeRe = /[&<>'"]/4647export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => {48 let str = ''49 const callbacks: HtmlEscapedCallback[] = []50 for (let i = buffer.length - 1; ; i--) {51 str += buffer[i]52 i--53 if (i < 0) {54 break55 }5657 let r = await buffer[i]58 if (typeof r === 'object') {59 callbacks.push(...((r as HtmlEscapedString).callbacks || []))60 }6162 const isEscaped = (r as HtmlEscapedString).isEscaped63 r = await (typeof r === 'object' ? (r as HtmlEscapedString).toString() : r)64 if (typeof r === 'object') {65 callbacks.push(...((r as HtmlEscapedString).callbacks || []))66 }6768 if ((r as HtmlEscapedString).isEscaped ?? isEscaped) {69 str += r70 } else {71 const buf = [str]72 escapeToBuffer(r, buf)73 str = buf[0]74 }75 }7677 return raw(str, callbacks)78}7980export const escapeToBuffer = (str: string, buffer: StringBuffer): void => {81 const match = str.search(escapeRe)82 if (match === -1) {83 buffer[0] += str84 return85 }8687 let escape88 let index89 let lastIndex = 09091 for (index = match; index < str.length; index++) {92 switch (str.charCodeAt(index)) {93 case 34: // "94 escape = '"'95 break96 case 39: // '97 escape = '''98 break99 case 38: // &100 escape = '&'101 break102 case 60: // <103 escape = '<'104 break105 case 62: // >106 escape = '>'107 break108 default:109 continue110 }111112 buffer[0] += str.substring(lastIndex, index) + escape113 lastIndex = index + 1114 }115116 buffer[0] += str.substring(lastIndex, index)117}118Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
line 119why, one step back119export const resolveCallback = async (chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
line 120why, one step back120 str: string | HtmlEscapedString,Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 121why, one step back121 phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase],Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 122–123why, one step back122 preserveCallbacks: boolean,123 context: object,chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
lines 124–125why, one step back124 buffer?: [string]125): Promise<string> => {Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 126–127why, one step back126 const callbacks = (str as HtmlEscapedString).callbacks as HtmlEscapedCallback[]127 if (!callbacks?.length) {chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
lines 128–134why, one step back128 return Promise.resolve(str)129 }130 if (buffer) {131 buffer[0] += str132 } else {133 buffer = [str]134 }Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 135–142why, one step back135136 const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then((res) =>137 Promise.all(138 res139 // eslint-disable-next-line @typescript-eslint/no-explicit-any140 .filter<string>(Boolean as any)141 .map((str) => resolveCallback(str, phase, false, context, buffer))142 ).then(() => (buffer as [string])[0])chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
line 143why, one step back143 )Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
lines 144–149why, one step back144145 if (preserveCallbacks) {146 return raw(await resStr, callbacks)147 } else {148 return resStr149 }chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4
line 150why, one step back150}