Skip to content

Commit

Permalink
render empty string for null __html
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed May 23, 2024
1 parent 86e1a84 commit 8875ca9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ function _renderToString(
break;

case 'dangerouslySetInnerHTML':
html = v && v.__html;
html = v && v.__html != null ? v.__html : '';
continue;

// serialize object styles to a CSS string
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 8875ca9

Please sign in to comment.