diff --git a/runtime-tests/deno-jsx/jsx.test.tsx b/runtime-tests/deno-jsx/jsx.test.tsx
index 4b58a34a9..c0399afce 100644
--- a/runtime-tests/deno-jsx/jsx.test.tsx
+++ b/runtime-tests/deno-jsx/jsx.test.tsx
@@ -111,6 +111,29 @@ Deno.test('JSX: css', async () => {
)
})
+Deno.test('JSX: css with CSP nonce', async () => {
+ const className = css`
+ color: red;
+ `
+ const html = (
+
+
+
+
+
+
+
+
+ )
+
+ const awaitedHtml = await html
+ const htmlEscapedString = 'callbacks' in awaitedHtml ? awaitedHtml : await awaitedHtml.toString()
+ assertEquals(
+ await resolveCallback(htmlEscapedString, HtmlEscapedCallbackPhase.Stringify, false, {}),
+ ''
+ )
+})
+
Deno.test('JSX: normalize key', async () => {
const className =
const htmlFor =
diff --git a/src/helper/css/common.case.test.tsx b/src/helper/css/common.case.test.tsx
index 74422944c..112acd190 100644
--- a/src/helper/css/common.case.test.tsx
+++ b/src/helper/css/common.case.test.tsx
@@ -488,6 +488,21 @@ export const renderTest = (
'Hello!
'
)
})
+
+ it('Should render CSS styles with CSP nonce', async () => {
+ const headerClass = css`
+ background-color: blue;
+ `
+ const template = (
+ <>
+
+
+ >
+ )
+ expect(await toString(template)).toBe(
+ 'Hello!
'
+ )
+ })
})
})
}
diff --git a/src/helper/css/index.test.tsx b/src/helper/css/index.test.tsx
index 679f99e15..c8001d9c3 100644
--- a/src/helper/css/index.test.tsx
+++ b/src/helper/css/index.test.tsx
@@ -1,8 +1,8 @@
/** @jsxImportSource ../../jsx */
import { Hono } from '../../'
import { html } from '../../helper/html'
-import { isValidElement } from '../../jsx'
import type { JSXNode } from '../../jsx'
+import { isValidElement } from '../../jsx'
import { Suspense, renderToReadableStream } from '../../jsx/streaming'
import type { HtmlEscapedString } from '../../utils/html'
import { HtmlEscapedCallbackPhase, resolveCallback } from '../../utils/html'
@@ -58,6 +58,18 @@ describe('CSS Helper', () => {
Hello!
`
)
})
+
+ it('Should render CSS styles with `html` tag function and CSP nonce', async () => {
+ const headerClass = css`
+ background-color: blue;
+ `
+ const template = html`${Style({ nonce: '1234' })}
+ `
+ expect(await toString(template)).toBe(
+ `
+ Hello!
`
+ )
+ })
})
describe('cx()', () => {
@@ -227,6 +239,23 @@ describe('CSS Helper', () => {
})
})
+ app.get('/stream-with-nonce', (c) => {
+ const stream = renderToReadableStream(
+ <>
+
+ Loading...}>
+
+
+ >
+ )
+ return c.body(stream, {
+ headers: {
+ 'Content-Type': 'text/html; charset=UTF-8',
+ 'Transfer-Encoding': 'chunked',
+ },
+ })
+ })
+
it('/sync', async () => {
const res = await app.request('http://localhost/sync')
expect(res).not.toBeNull()
@@ -247,6 +276,22 @@ if(!d)return
do{n=d.nextSibling;n.remove()}while(n.nodeType!=8||n.nodeValue!='/$')
d.replaceWith(c.content)
})(document)
+`
+ )
+ })
+
+ it('/stream-with-nonce', async () => {
+ const res = await app.request('http://localhost/stream-with-nonce')
+ expect(res).not.toBeNull()
+ expect(await res.text()).toBe(
+ `Loading...
Hello!
`
)
})
diff --git a/src/helper/css/index.ts b/src/helper/css/index.ts
index f1aa02a8d..76437d597 100644
--- a/src/helper/css/index.ts
+++ b/src/helper/css/index.ts
@@ -50,7 +50,7 @@ interface ViewTransitionType {
}
interface StyleType {
- (args?: { children?: Promise }): HtmlEscapedString
+ (args?: { children?: Promise; nonce?: string }): HtmlEscapedString
}
/**
@@ -62,8 +62,9 @@ export const createCssContext = ({ id }: { id: Readonly }): DefaultConte
const [cssJsxDomObject, StyleRenderToDom] = createCssJsxDomObjects({ id })
const contextMap: WeakMap