0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00

Skip closing tag if it is an empty element (#378)

* skip closing tag if it is an empty element

* Format code

* Empty elements are closed by '/>'
This commit is contained in:
TANIGUCHI Masaya 2022-07-15 08:07:54 +09:00 committed by GitHub
parent 8e9282ea9c
commit 39731af200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -27,6 +27,11 @@ describe('render to string', () => {
expect(template.toString()).toBe('<p><span>a</span><span>b</span></p>')
})
it('Empty elements are rended withtout closing tag', () => {
const template = (<input/>)
expect(template.toString()).toBe('<input/>')
})
it('Props value is null', () => {
const template = <span data-hello={null}>Hello</span>
expect(template.toString()).toBe('<span>Hello</span>')

View File

@ -10,6 +10,24 @@ declare global {
}
}
const emptyTags = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
]
export const jsx = (
tag: string | Function,
props: Record<string, any>,
@ -41,6 +59,9 @@ export const jsx = (
}
if (tag !== '') {
if (emptyTags.includes(tag)) {
result += '/'
}
result += '>'
}
@ -56,7 +77,7 @@ export const jsx = (
}
}
if (tag !== '') {
if (tag !== '' && !emptyTags.includes(tag)) {
result += `</${tag}>`
}