Skip to content

Commit

Permalink
fix(core): render empty string for null __html (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed May 23, 2024
1 parent 86e1a84 commit 689e88d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-ghosts-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Correctly render `null` as an `__html` value as an empty string
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function renderToString(vnode, context, _rendererState) {
parent[CHILDREN] = [vnode];

try {
return _renderToString(
const rendered = _renderToString(
vnode,
context || EMPTY_OBJ,
false,
Expand All @@ -64,6 +64,11 @@ export function renderToString(vnode, context, _rendererState) {
false,
_rendererState
);

if (Array.isArray(rendered)) {
return rendered.join('');
}
return rendered;
} catch (e) {
if (e.then) {
throw new Error('Use "renderToStringAsync" for suspenseful rendering.');
Expand Down Expand Up @@ -682,7 +687,6 @@ function _renderToString(

if (Array.isArray(html)) return [startTag, ...html, endTag];
else if (typeof html !== 'string') return [startTag, html, endTag];

return startTag + html + endTag;
}

Expand Down
10 changes: 10 additions & 0 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ describe('render', () => {
);
});

it('should accept nullish __html', () => {
const Test = (props) => (
<Fragment>
<div>hi</div>
<div dangerouslySetInnerHTML={{ __html: null }} />
</Fragment>
);
expect(render(<Test />)).to.equal('<div>hi</div><div></div>');
});

it('should apply defaultProps', () => {
const Test = (props) => <div {...props} />;
Test.defaultProps = {
Expand Down

0 comments on commit 689e88d

Please sign in to comment.