Skip to content

Commit 6a60e81

Browse files
Merge pull request #391 from preactjs/cursed-class-name
fix: allow any stringified value in class + className
2 parents 19ebe20 + d80e4dc commit 6a60e81

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

.changeset/metal-bottles-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-render-to-string': patch
3+
---
4+
5+
Allow any value for `class` + `className` as long as it's stringified to match browser behavior.

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ function _renderToString(
543543
for (let name in props) {
544544
let v = props[name];
545545

546-
if (typeof v == 'function') continue;
546+
if (typeof v == 'function' && name !== 'class' && name !== 'className') {
547+
continue;
548+
}
547549

548550
switch (name) {
549551
case 'children':

test/render.test.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,18 @@ describe('render', () => {
16781678
}
16791679
}
16801680
});
1681+
1682+
// https://github.com/preactjs/preact-render-to-string/issues/390
1683+
// The DOM API casts any value to a string here.
1684+
it('should cast "className" and "class" value to string', () => {
1685+
const proxy = new Proxy(() => {}, {
1686+
get() {
1687+
return () => 'foo';
1688+
}
1689+
});
1690+
let rendered = render(<div className={proxy} />);
1691+
expect(rendered).to.equal(`<div class="foo"></div>`);
1692+
});
16811693
});
16821694

16831695
describe('precompiled JSX', () => {

0 commit comments

Comments
 (0)