feat(jsx): support HtmlEscapedString in html tag function (#2233)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(jsx): support HtmlEscapedString in html tag function * chore: denoify
authored by Taku Amano · committed by GitHub · parent e355089 · Browse files · 3 files·+44 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−16 removed
- tests:+22 added−0 removed
- other:+22 added−16 removed
Provenance
- Commit
86d1221- Tree
413b22a- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
e355089
3 files changed+44 added−16 removed
modified:deno_dist/helper/html/index.ts+11 added-8 removed
17 unmodified lines1819202122232425242526272829302627282930313233323435363738398 unmodified lines17 unmodified lines // eslint-disable-next-line @typescript-eslint/no-explicit-any const child = children[i] as any if (typeof child === 'string') { escapeToBuffer(child, buffer) } else if (typeof child === 'number') { ;(buffer[0] as string) += child } else if (typeof child === 'boolean' || child === null || child === undefined) { continue } else if ( (typeof child === 'object' && (child as HtmlEscaped).isEscaped) || typeof child === 'number' ) { const tmp = child.toString() if (tmp instanceof Promise) { buffer.unshift('', tmp) } else if (typeof child === 'object' && (child as HtmlEscaped).isEscaped) { if ((child as HtmlEscapedString).callbacks) { buffer.unshift('', child) } else { const tmp = child.toString() if (tmp instanceof Promise) { buffer.unshift('', tmp) } else { buffer[0] += tmp buffer[0] += tmp } } } else if (child instanceof Promise) { buffer.unshift('', child) } else {8 unmodified linesmodified:src/helper/html/index.test.ts+22 added
1234539 unmodified lines45464748495051525354555657585960616263646566676869707172737 unmodified linesimport { resolveCallback, HtmlEscapedCallbackPhase } from '../../utils/html'import { html, raw } from '.'
describe('Tagged Template Literals', () => { it('Should escape special characters', () => {39 unmodified lines expect(res).toBeInstanceOf(Promise) expect((await res).toString()).toBe('<p>I\'m John "Johnny" Smith.</p>') }) })
describe('HtmlEscapedString', () => { it('Should preserve callbacks', async () => { const name = raw('Hono', [ ({ buffer }) => { if (buffer) { buffer[0] = buffer[0].replace('Hono', 'Hono!') } return undefined }, ]) const res = html`<p>I'm ${name}.</p>` expect(res).toBeInstanceOf(Promise) // eslint-disable-next-line quotes expect((await res).toString()).toBe("<p>I'm Hono.</p>") expect(await resolveCallback(await res, HtmlEscapedCallbackPhase.Stringify, false, {})).toBe( // eslint-disable-next-line quotes "<p>I'm Hono!.</p>" ) }) })})
describe('raw', () => { it('Should be marked as escaped.', () => {7 unmodified linesmodified:src/helper/html/index.ts+11 added-8 removed
17 unmodified lines1819202122232425242526272829302627282930313233323435363738398 unmodified lines17 unmodified lines // eslint-disable-next-line @typescript-eslint/no-explicit-any const child = children[i] as any if (typeof child === 'string') { escapeToBuffer(child, buffer) } else if (typeof child === 'number') { ;(buffer[0] as string) += child } else if (typeof child === 'boolean' || child === null || child === undefined) { continue } else if ( (typeof child === 'object' && (child as HtmlEscaped).isEscaped) || typeof child === 'number' ) { const tmp = child.toString() if (tmp instanceof Promise) { buffer.unshift('', tmp) } else if (typeof child === 'object' && (child as HtmlEscaped).isEscaped) { if ((child as HtmlEscapedString).callbacks) { buffer.unshift('', child) } else { const tmp = child.toString() if (tmp instanceof Promise) { buffer.unshift('', tmp) } else { buffer[0] += tmp buffer[0] += tmp } } } else if (child instanceof Promise) { buffer.unshift('', child) } else {8 unmodified lines