Skip to content

Commit

Permalink
Merge pull request #391 from preactjs/cursed-class-name
Browse files Browse the repository at this point in the history
fix: allow any stringified value in class + className
  • Loading branch information
marvinhagemeister authored Aug 29, 2024
2 parents 19ebe20 + d80e4dc commit 6a60e81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/metal-bottles-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Allow any value for `class` + `className` as long as it's stringified to match browser behavior.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,9 @@ function _renderToString(
for (let name in props) {
let v = props[name];

if (typeof v == 'function') continue;
if (typeof v == 'function' && name !== 'class' && name !== 'className') {
continue;
}

switch (name) {
case 'children':
Expand Down
12 changes: 12 additions & 0 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,18 @@ describe('render', () => {
}
}
});

// https://github.com/preactjs/preact-render-to-string/issues/390
// The DOM API casts any value to a string here.
it('should cast "className" and "class" value to string', () => {
const proxy = new Proxy(() => {}, {
get() {
return () => 'foo';
}
});
let rendered = render(<div className={proxy} />);
expect(rendered).to.equal(`<div class="foo"></div>`);
});
});

describe('precompiled JSX', () => {
Expand Down

0 comments on commit 6a60e81

Please sign in to comment.