Skip to content
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/fifty-oranges-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix JSX renderer to correctly render enumerated properties
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"test": "oxlint && tsc && npm run test:vitest:run && npm run bench",
"test:vitest": "vitest",
"test:vitest:run": "vitest run",
"format": "prettier src/**/*.{d.ts,js} test/**/*.js --write",
"format": "prettier src/**/*.{d.ts,js} test/**/*.{js,jsx} --write",
"prepublishOnly": "npm run build",
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
},
Expand Down
7 changes: 7 additions & 0 deletions src/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
VOID_ELEMENTS,
NAMESPACE_REPLACE_REGEX,
SVG_CAMEL_CASE,
HTML_ENUMERATED,
HTML_LOWER_CASE,
getContext,
setDirty,
Expand All @@ -22,6 +23,7 @@ import { options, Fragment } from 'preact';
const UNNAMED = [];

const EMPTY_ARR = [];
const EMPTY_STR = '';

/**
* Render Preact JSX + Components to a pretty-printed HTML-like string.
Expand Down Expand Up @@ -254,6 +256,11 @@ function _renderToStringPretty(
name = 'http-equiv';
} else if (NAMESPACE_REPLACE_REGEX.test(name)) {
name = name.replace(NAMESPACE_REPLACE_REGEX, '$1:$2').toLowerCase();
} else if (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mirrors the check in index.js.

(name.at(4) === '-' || HTML_ENUMERATED.has(name)) &&
v != null
) {
v = v + EMPTY_STR;
} else if (isSvgMode) {
if (SVG_CAMEL_CASE.test(name)) {
name =
Expand Down
9 changes: 9 additions & 0 deletions test/jsx.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,13 @@ describe('jsx', () => {
it('should not render function children', () => {
expect(renderJsx(<div>{() => {}}</div>)).to.equal('<div></div>');
});

it('should render enumerated attributes', () => {
expect(renderJsx(<div draggable={true} />)).to.equal(
'<div draggable="true"></div>'
);
expect(renderJsx(<div draggable={false} />)).to.equal(
'<div draggable="false"></div>'
);
});
});