Skip to content

Commit

Permalink
fix(deno/jsx-precompile): use html() tag function if value is a promi…
Browse files Browse the repository at this point in the history
…se (#2283)
  • Loading branch information
usualoma authored Feb 28, 2024
1 parent a3d9f13 commit b4912f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion deno_dist/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { jsxDEV as jsxs } from './jsx-dev-runtime.ts'

import { raw, html } from '../helper/html/index.ts'
export { html as jsxTemplate }
export const jsxAttr = (name: string, value: string) => raw(name + '="' + html`${value}` + '"')
export const jsxAttr = (name: string, value: string | Promise<string>) =>
typeof value === 'string' ? raw(name + '="' + html`${value}` + '"') : html`${name}="${value}"`
export const jsxEscape = (value: string) => value
25 changes: 25 additions & 0 deletions runtime_tests/deno-jsx/jsx.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @jsxImportSource ../../deno_dist/jsx */
import { Style, css } from '../../deno_dist/helper/css/index.ts'
import { Suspense, renderToReadableStream } from '../../deno_dist/jsx/streaming.ts'
import type { HtmlEscapedString } from '../../deno_dist/utils/html.ts'
import { resolveCallback, HtmlEscapedCallbackPhase } from '../../deno_dist/utils/html.ts'
import { assertEquals } from '../deno/deps.ts'

Deno.test('JSX', () => {
Expand Down Expand Up @@ -79,3 +81,26 @@ d.replaceWith(c.content)
</script>`,
])
})

Deno.test('JSX: css', async () => {
const className = css`
color: red;
`
const html = (
<html>
<head>
<Style />
</head>
<body>
<div class={className}></div>
</body>
</html>
)

const awaitedHtml = await html
const htmlEscapedString = 'callbacks' in awaitedHtml ? awaitedHtml : await awaitedHtml.toString()
assertEquals(
await resolveCallback(htmlEscapedString, HtmlEscapedCallbackPhase.Stringify, false, {}),
'<html><head><style id="hono-css">.css-3142110215{color:red}</style></head><body><div class="css-3142110215"></div></body></html>'
)
})
3 changes: 2 additions & 1 deletion src/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { jsxDEV as jsxs } from './jsx-dev-runtime'

import { raw, html } from '../helper/html'
export { html as jsxTemplate }
export const jsxAttr = (name: string, value: string) => raw(name + '="' + html`${value}` + '"')
export const jsxAttr = (name: string, value: string | Promise<string>) =>
typeof value === 'string' ? raw(name + '="' + html`${value}` + '"') : html`${name}="${value}"`
export const jsxEscape = (value: string) => value

0 comments on commit b4912f3

Please sign in to comment.