Skip to content

Commit 689e88d

Browse files
authored
fix(core): render empty string for null __html (#360)
1 parent 86e1a84 commit 689e88d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/yellow-ghosts-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-render-to-string': patch
3+
---
4+
5+
Correctly render `null` as an `__html` value as an empty string

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function renderToString(vnode, context, _rendererState) {
5555
parent[CHILDREN] = [vnode];
5656

5757
try {
58-
return _renderToString(
58+
const rendered = _renderToString(
5959
vnode,
6060
context || EMPTY_OBJ,
6161
false,
@@ -64,6 +64,11 @@ export function renderToString(vnode, context, _rendererState) {
6464
false,
6565
_rendererState
6666
);
67+
68+
if (Array.isArray(rendered)) {
69+
return rendered.join('');
70+
}
71+
return rendered;
6772
} catch (e) {
6873
if (e.then) {
6974
throw new Error('Use "renderToStringAsync" for suspenseful rendering.');
@@ -682,7 +687,6 @@ function _renderToString(
682687

683688
if (Array.isArray(html)) return [startTag, ...html, endTag];
684689
else if (typeof html !== 'string') return [startTag, html, endTag];
685-
686690
return startTag + html + endTag;
687691
}
688692

test/render.test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ describe('render', () => {
387387
);
388388
});
389389

390+
it('should accept nullish __html', () => {
391+
const Test = (props) => (
392+
<Fragment>
393+
<div>hi</div>
394+
<div dangerouslySetInnerHTML={{ __html: null }} />
395+
</Fragment>
396+
);
397+
expect(render(<Test />)).to.equal('<div>hi</div><div></div>');
398+
});
399+
390400
it('should apply defaultProps', () => {
391401
const Test = (props) => <div {...props} />;
392402
Test.defaultProps = {

0 commit comments

Comments
 (0)