Skip to content

Commit

Permalink
Allow empty string text nodes
Browse files Browse the repository at this point in the history
Currently, if you try to render a node like this:
```js
text('')
```
It will go through this part of the code to get HTML escaped, however, because the `||` operator considers `''` a falsy value, it goes for `node.name` instead, which does not exist on a VDOM node. The first line in this `escapeHtml` function converts this `undefined` value to string, which ends up being the string `'undefined'`.

I think this is a bug?

Hope this helps!
  • Loading branch information
loteoo authored and frenzzy committed Jun 29, 2022
1 parent 8bc4069 commit ecdb74c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const voidElements = new Set([

// credits to https://github.com/component/escape-html
export function escapeHtml(value) {
if (value == null) return '';
const str = '' + value
if (typeof value === 'number') {
// better performance for safe values
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,28 @@ describe('renderToString(view, state, actions)', () => {
expect(html).toBe('<div>foo bar baz</div>')
})

it('should correctly render empty text nodes', () => {
const VNode = {
tag: 'div',
props: {},
key: null,
children: [
{
tag: '',
props: {},
key: null,
children: [],
type: 3,
node: null,
},
],
type: 1,
node: null,
}
const html = renderToString(VNode)
expect(html).toBe('<div></div>')
})

it('should support Hyperapp V2 lazy nodes', () => {
const VNode = {
lazy: {
Expand Down

0 comments on commit ecdb74c

Please sign in to comment.