From 8582f23b6ad64a3e247904d711de9b60666e2f29 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 15 Dec 2021 03:41:26 -0600 Subject: [PATCH 1/3] fix: On empty className w/ compat, class attr will no longer be duplicated --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index fa973a0a..7714f36f 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,8 @@ const SHALLOW = { shallow: true }; // components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names. const UNNAMED = []; -const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; +const VOID_ELEMENTS = + /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; const UNSAFE_NAME = /[\s\n\\/='"\0<>]/; @@ -247,7 +248,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) { if (name === 'defaultValue') { name = 'value'; } else if (name === 'className') { - if (props.class) continue; + if (typeof props.class !== 'undefined') continue; name = 'class'; } else if (isSvgMode && name.match(/^xlink:?./)) { name = name.toLowerCase().replace(/^xlink:?/, 'xlink:'); From 615e2de79aaf860d3619007bfb928acd4129edc9 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 15 Dec 2021 03:57:00 -0600 Subject: [PATCH 2/3] test: Adding compat test suite --- package.json | 3 ++- test/compat.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/compat.test.js diff --git a/package.json b/package.json index a0ee9cbe..9b1df539 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "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", "copy-typescript-definition": "copyfiles -f src/*.d.ts dist", "test": "eslint src test && tsc && npm run test:mocha && npm run bench", - "test:mocha": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/**/*.test.js", + "test:mocha": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/**/[!compat]*.test.js && npm run test:mocha:compat", + "test:mocha:compat": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/compat.test.js", "format": "prettier src/**/*.{d.ts,js} test/**/*.js --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" diff --git a/test/compat.test.js b/test/compat.test.js new file mode 100644 index 00000000..e6fc6b96 --- /dev/null +++ b/test/compat.test.js @@ -0,0 +1,12 @@ +import { render } from '../src'; +import { createElement } from 'preact/compat'; +import { expect } from 'chai'; + +describe('compat', () => { + it('should not duplicate class attribute when className is empty', async () => { + let rendered = render(createElement('div', { className: '' })); + let expected = `
`; + + expect(rendered).to.equal(expected); + }); +}); From 298d05e5a29620ee9865b4cdb14c28464eebbd47 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 15 Dec 2021 04:01:14 -0600 Subject: [PATCH 3/3] docs: Adding changeset --- .changeset/fifty-rings-jog.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fifty-rings-jog.md diff --git a/.changeset/fifty-rings-jog.md b/.changeset/fifty-rings-jog.md new file mode 100644 index 00000000..ad3f5693 --- /dev/null +++ b/.changeset/fifty-rings-jog.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +On empty className w/ compat, class attribute will no longer be duplicated