Skip to content

Commit

Permalink
refactor(jsx/precompile): Normalization of keys from JSX to HTML is d…
Browse files Browse the repository at this point in the history
…one by runtime, not by hono
  • Loading branch information
usualoma committed Sep 21, 2024
1 parent b337b23 commit 4405e69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 18 additions & 3 deletions runtime-tests/deno-jsx/jsx.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,24 @@ Deno.test('JSX: css', async () => {
)
})

Deno.test('JSX: className', async () => {
const html = <div className='foo'></div>
assertEquals(html.toString(), '<div class="foo"></div>')
Deno.test('JSX: normalize key', async () => {
const className = <div className='foo'></div>
const htmlFor = <div htmlFor='foo'></div>
const crossOrigin = <div crossOrigin='foo'></div>
const httpEquiv = <div httpEquiv='foo'></div>
const itemProp = <div itemProp='foo'></div>
const fetchPriority = <div fetchPriority='foo'></div>
const noModule = <div noModule='foo'></div>
const formAction = <div formAction='foo'></div>

assertEquals(className.toString(), '<div class="foo"></div>')
assertEquals(htmlFor.toString(), '<div for="foo"></div>')
assertEquals(crossOrigin.toString(), '<div crossorigin="foo"></div>')
assertEquals(httpEquiv.toString(), '<div http-equiv="foo"></div>')
assertEquals(itemProp.toString(), '<div itemprop="foo"></div>')
assertEquals(fetchPriority.toString(), '<div fetchpriority="foo"></div>')
assertEquals(noModule.toString(), '<div nomodule="foo"></div>')
assertEquals(formAction.toString(), '<div formaction="foo"></div>')
})

Deno.test('JSX: null or undefined', async () => {
Expand Down
3 changes: 1 addition & 2 deletions src/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ export type { JSX } from './jsx-dev-runtime'
import { html, raw } from '../helper/html'
import type { HtmlEscapedString, StringBuffer, HtmlEscaped } from '../utils/html'
import { escapeToBuffer, stringBufferToString } from '../utils/html'
import { normalizeIntrinsicElementKey, styleObjectForEach } from './utils'
import { styleObjectForEach } from './utils'

Check warning on line 12 in src/jsx/jsx-runtime.ts

View check run for this annotation

Codecov / codecov/patch

src/jsx/jsx-runtime.ts#L11-L12

Added lines #L11 - L12 were not covered by tests

export { html as jsxTemplate }

export const jsxAttr = (
key: string,
v: string | Promise<string> | Record<string, string | number | null | undefined | boolean>
): HtmlEscapedString | Promise<HtmlEscapedString> => {
key = normalizeIntrinsicElementKey(key)
const buffer: StringBuffer = [`${key}="`] as StringBuffer
if (key === 'style' && typeof v === 'object') {

Check warning on line 21 in src/jsx/jsx-runtime.ts

View check run for this annotation

Codecov / codecov/patch

src/jsx/jsx-runtime.ts#L17-L21

Added lines #L17 - L21 were not covered by tests
// object to style strings
Expand Down

0 comments on commit 4405e69

Please sign in to comment.