Skip to content
Myr

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 file

Take 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 back
1export 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 back
8 phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase]

Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca

lines 9–11why, one step back
9 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> | undefined

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 12–15why, one step back
12export type HtmlEscaped = {
13 isEscaped: true14 callbacks?: HtmlEscapedCallback[]15}

Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac

line 16why, one step back
16export type HtmlEscapedString = string & HtmlEscaped

feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438

lines 17–31why, one step back
17
18/**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 back
32export 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 back
33

fix(html): Remove circular dependencies in `hono/html` (#2143)javascripterbefore adoptionf44c705

lines 34–40why, one step back
34export 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 back
41
42// 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 back
46
47export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => {48 let str = ''

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 49why, one step back
49 const callbacks: HtmlEscapedCallback[] = []

feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438

lines 50–56why, one step back
50 for (let i = buffer.length - 1; ; i--) {
51 str += buffer[i]52 i--53 if (i < 0) {54 break55 }56

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

lines 57–58why, one step back
57 let r = await buffer[i]
58 if (typeof r === 'object') {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 59why, one step back
59 callbacks.push(...((r as HtmlEscapedString).callbacks || []))

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

line 60why, one step back
60 }

feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438

lines 61–62why, one step back
61
62 const isEscaped = (r as HtmlEscapedString).isEscaped

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

lines 63–64why, one step back
63 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 back
65 callbacks.push(...((r as HtmlEscapedString).callbacks || []))

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

line 66why, one step back
66 }

feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438

lines 67–74why, one step back
67
68 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 back
75 }
76

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 77why, one step back
77 return raw(str, callbacks)

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

line 78why, one step back
78}

Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac

lines 79–116why, one step back
79
80export 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 = '&quot;'95 break96 case 39: // '97 escape = '&#39;'98 break99 case 38: // &100 escape = '&amp;'101 break102 case 60: // <103 escape = '&lt;'104 break105 case 62: // >106 escape = '&gt;'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 back
117}
118

Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca

line 119why, one step back
119export const resolveCallback = async (

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 120why, one step back
120 str: string | HtmlEscapedString,

Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7

line 121why, one step back
121 phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase],

Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca

lines 122–123why, one step back
122 preserveCallbacks: boolean,
123 context: object,

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 124–125why, one step back
124 buffer?: [string]
125): Promise<string> => {

Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca

lines 126–127why, one step back
126 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 back
128 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 back
135
136 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 back
143 )

Merge pull request #1895 from honojs/nextYusuke Wadabefore adoptionbe9f4ca

lines 144–149why, one step back
144
145 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 back
150}