feat(middleware/jsx): Support dangerouslySetInnerHTML and some special spec (#308)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(middleware/jsx): handle some special cases for jsx. * feat(middleware/jsx): Support dangerouslySetInnerHTML.
authored by Taku Amano · committed by GitHub · parent b965262 · Browse files · 2 files·+122 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+122 added−12 removed
- tests:+82 added−0 removed
- other:+40 added−12 removed
Provenance
- Commit
ddd905e- Tree
4a143f9- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
b965262
2 files changed+122 added−12 removed
modified:src/middleware/jsx/index.test.tsx+82 added
13 unmodified lines141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989913 unmodified lines expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8') expect(await res.text()).toBe('<!doctype html><h1>Hello</h1>') })})
describe('render to string', () => { it('Nested array', () => { const template = ( <p> {[[['a']], [['b']]].map((item1) => item1.map((item2) => item2.map((item3) => <span>{item3}</span>)) )} </p> ) expect(template.toString()).toBe('<p><span>a</span><span>b</span></p>') })
it('Props value is null', () => { const template = <span data-hello={null}>Hello</span> expect(template.toString()).toBe('<span>Hello</span>') })
it('Props value is undefined', () => { const template = <span data-hello={undefined}>Hello</span> expect(template.toString()).toBe('<span>Hello</span>') })
describe('dangerouslySetInnerHTML', () => { it('Should render dangerouslySetInnerHTML', () => { const template = <span dangerouslySetInnerHTML={{ __html: '" is allowed here' }}></span> expect(template.toString()).toBe('<span>" is allowed here</span>') })
it('Should get an error if both dangerouslySetInnerHTML and children are specified', () => { expect(() => ( <span dangerouslySetInnerHTML={{ __html: '" is allowed here' }}>Hello</span> )).toThrow() }) })
// https://en.reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored describe('Booleans, Null, and Undefined Are Ignored', () => { it.each([true, false, undefined, null])('%s', (item) => { expect((<span>{item}</span>).toString()).toBe('<span></span>') })
it('falsy value', () => { const template = <span>{0}</span> expect(template.toString()).toBe('<span>0</span>') }) })
// https://en.reactjs.org/docs/jsx-in-depth.html#props-default-to-true describe('Props Default to “True”', () => { it('default prop value', () => { const template = <span data-hello>Hello</span> expect(template.toString()).toBe('<span data-hello="true">Hello</span>') }) })
// https://en.reactjs.org/docs/jsx-in-depth.html#functions-as-children describe('Functions as Children', () => { it('Function', () => { function Repeat(props: any) { const items = [] for (let i = 0; i < props.numTimes; i++) { items.push((props.children as Function)(i)) } return <div>{items}</div> }
function ListOfTenThings() { return ( <Repeat numTimes={10}> {(index: string) => <div key={index}>This is item {index} in the list</div>} </Repeat> ) }
const template = <ListOfTenThings /> expect(template.toString()).toBe( '<div><div key="0">This is item 0 in the list</div><div key="1">This is item 1 in the list</div><div key="2">This is item 2 in the list</div><div key="3">This is item 3 in the list</div><div key="4">This is item 4 in the list</div><div key="5">This is item 5 in the list</div><div key="6">This is item 6 in the list</div><div key="7">This is item 7 in the list</div><div key="8">This is item 8 in the list</div><div key="9">This is item 9 in the list</div></div>' ) }) })})modified:src/middleware/jsx/index.ts+40 added-12 removed
4 unmodified lines5678991011121314 unmodified lines282930313232333434353637383739404142434445464748495051525354555640414243444546575848596061626364656667686970717273747576774 unmodified linesdeclare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace h.JSX { interface IntrinsicElements { [tagName: string]: Record<string, string> [tagName: string]: Record<string, any> } }}
14 unmodified lines props: Record<string, any>, ...children: (string | EscapedString)[]): EscapedString => { if (typeof tag === 'function') { return tag.call(null, { ...props, children }) return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children }) } let attrs = ''
let result = `<${tag}`
const propsKeys = Object.keys(props || {}) for (let i = 0, len = propsKeys.length; i < len; i++) { attrs += ` ${propsKeys[i]}="${escape(props[propsKeys[i]])}"` const v = props[propsKeys[i]] if (propsKeys[i] === 'dangerouslySetInnerHTML') { if (children.length > 0) { throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.' }
const escapedString = new String(v.__html) as EscapedString escapedString.isEscaped = true children = [escapedString] continue } else if (v === null || v === undefined) { continue }
result += ` ${propsKeys[i]}="${escape(v.toString())}"` }
const res: any = new String( `<${tag}${attrs}>${children .flat() .map((c) => ((c as any).isEscaped ? c : escape(c as string))) .join('')}</${tag}>` ) res.isEscaped = true result += '>'
return res const flattenChildren = children.flat(Infinity) for (let i = 0, len = flattenChildren.length; i < len; i++) { const child = flattenChildren[i] if (typeof child === 'boolean' || child === null || child === undefined) { continue } else if (typeof child === 'object' && (child as any).isEscaped) { result += child } else { result += escape(child.toString()) } }
result += `</${tag}>`
const escapedString = new String(result) as EscapedString escapedString.isEscaped = true
return escapedString}