Skip to content

Commit

Permalink
fix: Support dangerouslySetInnerHTML={undefined} with `renderToStri…
Browse files Browse the repository at this point in the history
…ngAsync` (#381)
  • Loading branch information
rschristian authored Jul 24, 2024
1 parent c8e54a1 commit 481b4f3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-ladybugs-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Support `dangerouslySetInnerHTML={undefined}` with `renderToStringAsync`
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export async function renderToStringAsync(vnode, context) {

// Resolving nested Promises with a maximum depth of 25
while (
resolved.some((element) => typeof element.then === 'function') &&
resolved.some(
(element) => element && typeof element.then === 'function'
) &&
count++ < 25
) {
resolved = (await Promise.all(resolved)).flat();
Expand Down
43 changes: 43 additions & 0 deletions test/compat/async.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,47 @@ describe('Async renderToString', () => {

expect(rendered).to.equal(`<div>2</div>`);
});

describe('dangerouslySetInnerHTML', () => {
it('should support dangerouslySetInnerHTML', async () => {
// some invalid HTML to make sure we're being flakey:
let html = '<a href="foo">asdf</a> some text <ul><li>foo<li>bar</ul>';
let rendered = await renderToStringAsync(
<div id="f" dangerouslySetInnerHTML={{ __html: html }} />
);
expect(rendered).to.equal(`<div id="f">${html}</div>`);
});

it('should accept undefined dangerouslySetInnerHTML', async () => {
const Test = () => (
<Fragment>
<div>hi</div>
<div dangerouslySetInnerHTML={undefined} />
</Fragment>
);

const rendered = await renderToStringAsync(<Test />);
expect(rendered).to.equal('<div>hi</div><div></div>');
});

it('should accept null __html', async () => {
const Test = () => (
<Fragment>
<div>hi</div>
<div dangerouslySetInnerHTML={{ __html: null }} />
</Fragment>
);
const rendered = await renderToStringAsync(<Test />);
expect(rendered).to.equal('<div>hi</div><div></div>');
});

it('should override children', async () => {
let rendered = await renderToStringAsync(
<div dangerouslySetInnerHTML={{ __html: 'foo' }}>
<b>bar</b>
</div>
);
expect(rendered).to.equal('<div>foo</div>');
});
});
});
30 changes: 20 additions & 10 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,6 @@ 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 Expand Up @@ -835,6 +825,26 @@ describe('render', () => {
expect(rendered).to.equal(`<div id="f">${html}</div>`);
});

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

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

it('should override children', () => {
let rendered = render(
<div dangerouslySetInnerHTML={{ __html: 'foo' }}>
Expand Down

0 comments on commit 481b4f3

Please sign in to comment.