Skip to content

Commit

Permalink
Ignore functions passed as children
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Oct 4, 2022
1 parent ad35c4c commit 203b79a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .changeset/curly-bananas-do.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'preact-render-to-string': patch
---

Fix object children being rendered as `undefined`
Fix object and function children being rendered as `undefined`
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {

// Text VNodes: escape as HTML
if (typeof vnode !== 'object') {
if (typeof vnode === 'function') return '';
return encodeEntities(vnode);
}

Expand Down
1 change: 1 addition & 0 deletions src/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function _renderToStringPretty(

// #text nodes
if (typeof vnode !== 'object') {
if (typeof vnode === 'function') return '';
return encodeEntities(vnode);
}

Expand Down
4 changes: 4 additions & 0 deletions test/jsx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,8 @@ describe('jsx', () => {
it('should prevent JSON injection', () => {
expect(renderJsx(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
});

it('should not render function children', () => {
expect(renderJsx(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});
4 changes: 4 additions & 0 deletions test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,8 @@ describe('pretty', () => {
'<div></div>'
);
});

it('should not render function children', () => {
expect(prettyRender(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});
4 changes: 4 additions & 0 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1264,4 +1264,8 @@ describe('render', () => {
it('should prevent JSON injection', () => {
expect(render(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
});

it('should not render function children', () => {
expect(render(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});

0 comments on commit 203b79a

Please sign in to comment.