html.ts
This file
Path
src/utils/html.ts
120 lines · 3.1 KiB · TypeScript
Last changed by
feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
History of this fileTake it
The walk stopped after 200 commits: lines older than 48a8729 point at it.
Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 1–6why, one step back1type HtmlEscapedCallbackOpts = { error?: Error; buffer?: [string] }2export type HtmlEscapedCallback = (opts: HtmlEscapedCallbackOpts) => Promise<string>3export type HtmlEscaped = {4 isEscaped: true5 callbacks?: HtmlEscapedCallback[]6}perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729
line 7why, one step back7export type HtmlEscapedString = string & HtmlEscapedfeat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 8–22why, one step back89/**10 * StringBuffer contains string and Promise<string> alternately11 * The length of the array will be odd, the odd numbered element will be a string,12 * and the even numbered element will be a Promise<string>.13 * When concatenating into a single string, it must be processed from the tail.14 * @example15 * [16 * 'framework.',17 * Promise.resolve('ultra fast'),18 * 'a ',19 * Promise.resolve('is '),20 * 'Hono',21 * ]22 */Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 23why, one step back23export type StringBuffer = (string | Promise<string>)[]feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
line 24why, one step back24Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 25why, one step back25import { raw } from '../helper/html'perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729
lines 26–27why, one step back2627// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.docs: support change of escapeTextForBrowser path in react (#1336)Sor4chibefore adoption8ab0ccd
line 28why, one step back28// https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/escapeTextForBrowser.jsperf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729
lines 29–30why, one step back2930const escapeRe = /[&<>'"]/Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 31–33why, one step back3132export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => {33 let str = ''Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 34why, one step back34 const callbacks: HtmlEscapedCallback[] = []feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 35–41why, one step back35 for (let i = buffer.length - 1; ; i--) {36 str += buffer[i]37 i--38 if (i < 0) {39 break40 }41Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 42–43why, one step back42 let r = await buffer[i]43 if (typeof r === 'object') {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 44why, one step back44 callbacks.push(...((r as HtmlEscapedString).callbacks || []))Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 45why, one step back45 }feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 46–47why, one step back4647 const isEscaped = (r as HtmlEscapedString).isEscapedMerge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 48–49why, one step back48 r = await (typeof r === 'object' ? (r as HtmlEscapedString).toString() : r)49 if (typeof r === 'object') {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 50why, one step back50 callbacks.push(...((r as HtmlEscapedString).callbacks || []))Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 51why, one step back51 }feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)Taku Amanobefore adoption1567438
lines 52–59why, one step back5253 if ((r as HtmlEscapedString).isEscaped ?? isEscaped) {54 str += r55 } else {56 const buf = [str]57 escapeToBuffer(r, buf)58 str = buf[0]59 }Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
lines 60–61why, one step back60 }61Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 62why, one step back62 return raw(str, callbacks)Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a
line 63why, one step back63}perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729
lines 64–101why, one step back6465export const escapeToBuffer = (str: string, buffer: StringBuffer): void => {66 const match = str.search(escapeRe)67 if (match === -1) {68 buffer[0] += str69 return70 }7172 let escape73 let index74 let lastIndex = 07576 for (index = match; index < str.length; index++) {77 switch (str.charCodeAt(index)) {78 case 34: // "79 escape = '"'80 break81 case 39: // '82 escape = '''83 break84 case 38: // &85 escape = '&'86 break87 case 60: // <88 escape = '<'89 break90 case 62: // >91 escape = '>'92 break93 default:94 continue95 }9697 buffer[0] += str.substring(lastIndex, index) + escape98 lastIndex = index + 199 }100101 buffer[0] += str.substring(lastIndex, index)Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 102–119why, one step back102}103104export const resolveStream = (105 str: string | HtmlEscapedString,106 buffer?: [string]107): Promise<string> => {108 if (!(str as HtmlEscapedString).callbacks?.length) {109 return Promise.resolve(str)110 }111 const callbacks = (str as HtmlEscapedString).callbacks as HtmlEscapedCallback[]112 if (buffer) {113 buffer[0] += str114 } else {115 buffer = [str]116 }117 return Promise.all(callbacks.map((c) => c({ buffer }))).then((res) =>118 Promise.all(res.map((str) => resolveStream(str, buffer))).then(() => (buffer as [string])[0])119 )perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729
line 120why, one step back120}