Skip to content

Commit 203b79a

Browse files
Ignore functions passed as children
1 parent ad35c4c commit 203b79a

File tree

6 files changed

+15
-1
lines changed

6 files changed

+15
-1
lines changed

.changeset/curly-bananas-do.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'preact-render-to-string': patch
33
---
44

5-
Fix object children being rendered as `undefined`
5+
Fix object and function children being rendered as `undefined`

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
195195

196196
// Text VNodes: escape as HTML
197197
if (typeof vnode !== 'object') {
198+
if (typeof vnode === 'function') return '';
198199
return encodeEntities(vnode);
199200
}
200201

src/pretty.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function _renderToStringPretty(
2929

3030
// #text nodes
3131
if (typeof vnode !== 'object') {
32+
if (typeof vnode === 'function') return '';
3233
return encodeEntities(vnode);
3334
}
3435

test/jsx.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,8 @@ describe('jsx', () => {
167167
it('should prevent JSON injection', () => {
168168
expect(renderJsx(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
169169
});
170+
171+
it('should not render function children', () => {
172+
expect(renderJsx(<div>{() => {}}</div>)).to.equal('<div></div>');
173+
});
170174
});

test/pretty.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,8 @@ describe('pretty', () => {
228228
'<div></div>'
229229
);
230230
});
231+
232+
it('should not render function children', () => {
233+
expect(prettyRender(<div>{() => {}}</div>)).to.equal('<div></div>');
234+
});
231235
});

test/render.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,4 +1264,8 @@ describe('render', () => {
12641264
it('should prevent JSON injection', () => {
12651265
expect(render(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
12661266
});
1267+
1268+
it('should not render function children', () => {
1269+
expect(render(<div>{() => {}}</div>)).to.equal('<div></div>');
1270+
});
12671271
});

0 commit comments

Comments
 (0)