Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support dangerouslySetInnerHTML={undefined} with renderToStringAsync #381

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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