Skip to content
Myr

jsx-runtime.test.tsx

This file

Path

src/jsx/jsx-runtime.test.tsx

85 lines · 2.8 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–85why, one step back
1/** @jsxRuntime automatic **/
2/** @jsxImportSource . **/3import { Hono } from '../hono'4import { jsxAttr } from './jsx-runtime'56describe('jsx-runtime', () => {7 let app: Hono89 beforeEach(() => {10 app = new Hono()11 })1213 it('Should render HTML strings', async () => {14 app.get('/', (c) => {15 return c.html(<h1>Hello</h1>)16 })17 const res = await app.request('http://localhost/')18 expect(res.status).toBe(200)19 expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')20 expect(await res.text()).toBe('<h1>Hello</h1>')21 })2223 it('Should skip invalid attribute keys in jsxAttr()', () => {24 expect(String(jsxAttr('" onfocus="alert(1)', 'x'))).toBe('')25 expect(String(jsxAttr('foo<bar', 'x'))).toBe('')26 expect(String(jsxAttr('foo\\bar', 'x'))).toBe('')27 expect(String(jsxAttr('foo`bar', 'x'))).toBe('')28 })2930 it('Should skip invalid non-string attribute values in jsxAttr()', async () => {31 const invalidKey = '" onfocus="alert(1)'3233 expect(String(jsxAttr(invalidKey, { fontSize: 10 }))).toBe('')34 expect(String(await jsxAttr(invalidKey, Promise.resolve('/docs?q=1&lang=en')))).toBe('')35 })3637 it('Should render valid attribute values in jsxAttr()', async () => {38 expect(String(jsxAttr('style', { fontSize: 10, color: 'red' }))).toBe(39 'style="font-size:10px;color:red"'40 )41 expect(String(await jsxAttr('href', Promise.resolve('/docs?q=1&lang=en')))).toBe(42 'href="/docs?q=1&amp;lang=en"'43 )44 })4546 it('Should drop style values containing ";" in jsxAttr() to prevent CSS injection', () => {47 expect(48 String(jsxAttr('style', { color: 'red;background:blue', backgroundColor: 'white' }))49 ).toBe('style="background-color:white"')50 })5152 it('Should drop style values that hide declaration separators in CSS comments in jsxAttr()', () => {53 expect(54 String(55 jsxAttr('style', {56 color: 'red/*(*/;background:blue;position:fixed;top:0',57 backgroundColor: 'white',58 })59 )60 ).toBe('style="background-color:white"')61 })6263 it('Should drop style property names that can inject declarations in jsxAttr()', () => {64 expect(65 String(66 jsxAttr('style', {67 'color;background-image': 'url(https://attacker.example/a.png)',68 backgroundColor: 'white',69 })70 )71 ).toBe('style="background-color:white"')72 })7374 // https://en.reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored75 describe('Booleans, Null, and Undefined Are Ignored', () => {76 it.each([true, false, undefined, null])('%s', (item) => {77 expect((<span>{item}</span>).toString()).toBe('<span></span>')78 })7980 it('falsy value', () => {81 const template = <span>{0}</span>82 expect(template.toString()).toBe('<span>0</span>')83 })84 })85})