feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals (#1826)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(helper/html): Support Promise<string | HtmlEscapedString> in html tagged template literals * chore: denoify * test: fix lint error
authored by Taku Amano · committed by GitHub · parent 3817ccc · Browse files · 5 files·+87 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+87 added−4 removed
- tests:+17 added−0 removed
- other:+70 added−4 removed
Provenance
- Commit
1567438- Tree
1735533- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
3817ccc
5 files changed+87 added−4 removed
modified:deno_dist/helper/html/index.ts+2 added
40 unmodified lines414243444546474849505 unmodified lines40 unmodified lines buffer.unshift('', tmp) } else { buffer[0] += tmp } } else if (child instanceof Promise) { buffer.unshift('', child) } else { escapeToBuffer(child.toString(), buffer) } }5 unmodified linesmodified:deno_dist/utils/html.ts+33 added-2 removed
3 unmodified lines456789101112131415161718192021222324252627282 unmodified lines313233341935363738394041424344454647484950515253285455565758596061626357 unmodified lines3 unmodified lines isEscaped: true callbacks?: HtmlEscapedCallback[]}export type HtmlEscapedString = string & HtmlEscaped
/** * StringBuffer contains string and Promise<string> alternately * The length of the array will be odd, the odd numbered element will be a string, * and the even numbered element will be a Promise<string>. * When concatenating into a single string, it must be processed from the tail. * @example * [ * 'framework.', * Promise.resolve('ultra fast'), * 'a ', * Promise.resolve('is '), * 'Hono', * ] */export type StringBuffer = (string | Promise<string>)[]
import { raw } from '../helper/html/index.ts'
// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.// https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/escapeTextForBrowser.js2 unmodified lines
export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => { let str = '' const callbacks: HtmlEscapedCallback[] = [] for (let i = buffer.length - 1; i >= 0; i--) { for (let i = buffer.length - 1; ; i--) { str += buffer[i] i-- if (i < 0) { break }
let r = await buffer[i] if (typeof r === 'object') { callbacks.push(...((r as HtmlEscapedString).callbacks || [])) }
const isEscaped = (r as HtmlEscapedString).isEscaped r = await (typeof r === 'object' ? (r as HtmlEscapedString).toString() : r) if (typeof r === 'object') { callbacks.push(...((r as HtmlEscapedString).callbacks || [])) }
if ((r as HtmlEscapedString).isEscaped ?? isEscaped) { str += r str += r } else { const buf = [str] escapeToBuffer(r, buf) str = buf[0] } }
return raw(str, callbacks)}57 unmodified linesmodified:src/helper/html/index.test.ts+17 added
26 unmodified lines27 expect(html`<p>${values}</p>`.toString()).toBe(28 '<p>Name:John "Johnny" Smith Contact:<a href="http://example.com/">My Website</a></p>'29 )30 })31+32+ describe('Promise', () => {33+ it('Should return Promise<string> when some variables contains Promise<string> in variables', async () => {34+ const name = Promise.resolve('John "Johnny" Smith')35+ const res = html`<p>I'm ${name}.</p>`36+ expect(res).toBeInstanceOf(Promise)37+ // eslint-disable-next-line quotes38+ expect((await res).toString()).toBe("<p>I'm John "Johnny" Smith.</p>")39+ })40+41+ it('Should return raw value when some variables contains Promise<HtmlEscapedString> in variables', async () => {42+ const name = Promise.resolve(raw('John "Johnny" Smith'))43+ const res = html`<p>I'm ${name}.</p>`44+ expect(res).toBeInstanceOf(Promise)45+ expect((await res).toString()).toBe('<p>I\'m John "Johnny" Smith.</p>')46+ })47+ })48 })49 50 describe('raw', () => {51 it('Should be marked as escaped.', () => {7 unmodified linesmodified:src/helper/html/index.ts+2 added
40 unmodified lines41 buffer.unshift('', tmp)42 } else {43 buffer[0] += tmp44 }45+ } else if (child instanceof Promise) {46+ buffer.unshift('', child)47 } else {48 escapeToBuffer(child.toString(), buffer)49 }50 }5 unmodified linesmodified:src/utils/html.ts+33 added-2 removed
3 unmodified lines4 isEscaped: true5 callbacks?: HtmlEscapedCallback[]6 }7 export type HtmlEscapedString = string & HtmlEscaped8+9+/**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+ */23 export type StringBuffer = (string | Promise<string>)[]24+25 import { raw } from '../helper/html'26 27 // The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.28 // https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/escapeTextForBrowser.js2 unmodified lines31 32 export const stringBufferToString = async (buffer: StringBuffer): Promise<HtmlEscapedString> => {33 let str = ''34 const callbacks: HtmlEscapedCallback[] = []19- for (let i = buffer.length - 1; i >= 0; i--) {35+ for (let i = buffer.length - 1; ; i--) {36+ str += buffer[i]37+ i--38+ if (i < 0) {39+ break40+ }41+42 let r = await buffer[i]43 if (typeof r === 'object') {44 callbacks.push(...((r as HtmlEscapedString).callbacks || []))45 }46+47+ const isEscaped = (r as HtmlEscapedString).isEscaped48 r = await (typeof r === 'object' ? (r as HtmlEscapedString).toString() : r)49 if (typeof r === 'object') {50 callbacks.push(...((r as HtmlEscapedString).callbacks || []))51 }52+53+ if ((r as HtmlEscapedString).isEscaped ?? isEscaped) {28- str += r54+ str += r55+ } else {56+ const buf = [str]57+ escapeToBuffer(r, buf)58+ str = buf[0]59+ }60 }61 62 return raw(str, callbacks)63 }57 unmodified lines