Skip to content
Myr

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

5 files+87 added−4 removed

modified:deno_dist/helper/html/index.ts+2 added
modified:deno_dist/utils/html.ts+33 added-2 removed
modified:src/helper/html/index.test.ts+17 added
26 unmodified lines
27 expect(html`<p>${values}</p>`.toString()).toBe(28 '<p>Name:John &quot;Johnny&quot; Smith Contact:<a href="http://example.com/">My Website</a></p>'29 )30 })3132 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 &quot;Johnny&quot; Smith.</p>")39 })4041 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})4950describe('raw', () => {51 it('Should be marked as escaped.', () => {
7 unmodified lines
modified:src/helper/html/index.ts+2 added
40 unmodified lines
41 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 lines
modified:src/utils/html.ts+33 added-2 removed
3 unmodified lines
4 isEscaped: true5 callbacks?: HtmlEscapedCallback[]6}7export type HtmlEscapedString = string & HtmlEscaped89/**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 */23export type StringBuffer = (string | Promise<string>)[]2425import { raw } from '../helper/html'2627// 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.js
2 unmodified lines
3132export 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 }4142 let r = await buffer[i]43 if (typeof r === 'object') {44 callbacks.push(...((r as HtmlEscapedString).callbacks || []))45 }4647 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 }5253 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 }6162 return raw(str, callbacks)63}
57 unmodified lines