Skip to content

Commit 63bde4b

Browse files
Merge pull request #209 from preactjs/fix/compat-empty-classname
fix: On empty className w/ compat, class attr will no longer be duplicated
2 parents 6214991 + 298d05e commit 63bde4b

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

.changeset/fifty-rings-jog.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+
On empty className w/ compat, class attribute will no longer be duplicated

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external preact && microbundle dist/jsx.js -o dist/jsx.js -f cjs --external preact",
3131
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",
3232
"test": "eslint src test && tsc && npm run test:mocha && npm run bench",
33-
"test:mocha": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/**/*.test.js",
33+
"test:mocha": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/**/[!compat]*.test.js && npm run test:mocha:compat",
34+
"test:mocha:compat": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/compat.test.js",
3435
"format": "prettier src/**/*.{d.ts,js} test/**/*.js --write",
3536
"prepublishOnly": "npm run build",
3637
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const SHALLOW = { shallow: true };
1515
// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
1616
const UNNAMED = [];
1717

18-
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
18+
const VOID_ELEMENTS =
19+
/^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
1920

2021
const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
2122

@@ -247,7 +248,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
247248
if (name === 'defaultValue') {
248249
name = 'value';
249250
} else if (name === 'className') {
250-
if (props.class) continue;
251+
if (typeof props.class !== 'undefined') continue;
251252
name = 'class';
252253
} else if (isSvgMode && name.match(/^xlink:?./)) {
253254
name = name.toLowerCase().replace(/^xlink:?/, 'xlink:');

test/compat.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { render } from '../src';
2+
import { createElement } from 'preact/compat';
3+
import { expect } from 'chai';
4+
5+
describe('compat', () => {
6+
it('should not duplicate class attribute when className is empty', async () => {
7+
let rendered = render(createElement('div', { className: '' }));
8+
let expected = `<div class></div>`;
9+
10+
expect(rendered).to.equal(expected);
11+
});
12+
});

0 commit comments

Comments
 (0)