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 ba04dac 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> | undefinedMerge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 12–15why, one step back12export type HtmlEscaped = {13 isEscaped: true14 callbacks?: HtmlEscapedCallback[]15}Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
line 16why, one step back16export type HtmlEscapedString = string & HtmlEscapedfeat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 17–31why, one step back1718/**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 */Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 32why, one step back32export type StringBuffer = (string | Promise<string>)[]feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
line 33why, one step back33fix(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}Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
lines 41–45why, 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 = /[&<>'"]/Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 46–48why, one step back4647export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => {48 let str = ''Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 49why, one step back49 const callbacks: HtmlEscapedCallback[] = []feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 50–56why, one step back50 for (let i = buffer.length - 1; ; i--) {51 str += buffer[i]52 i--53 if (i < 0) {54 break55 }56Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 57–58why, one step back57 let r = await buffer[i]58 if (typeof r === 'object') {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 59why, one step back59 callbacks.push(...((r as HtmlEscapedString).callbacks || []))Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 60why, one step back60 }feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 61–62why, one step back6162 const isEscaped = (r as HtmlEscapedString).isEscapedMerge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 63–64why, one step back63 r = await (typeof r === 'object' ? (r as HtmlEscapedString).toString() : r)64 if (typeof r === 'object') {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 65why, one step back65 callbacks.push(...((r as HtmlEscapedString).callbacks || []))Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 66why, one step back66 }feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 67–74why, one step back6768 if ((r as HtmlEscapedString).isEscaped ?? isEscaped) {69 str += r70 } else {71 const buf = [str]72 escapeToBuffer(r, buf)73 str = buf[0]74 }Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 75–76why, one step back75 }76Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 77why, one step back77 return raw(str, callbacks)Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 78why, one step back78}Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
lines 79–116why, one step back7980export 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)Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 117–118why, one step back117}118Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca
line 119why, one step back119export const resolveCallback = async (Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
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,Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
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) {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
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])Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
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 }Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
line 150why, one step back150}