Skip to content

Commit 678588c

Browse files
committed
support data attribute
1 parent 6376f62 commit 678588c

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

.changeset/perfect-ducks-rule.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+
Support `data` attribute

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"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",
3535
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",
3636
"test": "eslint src test && tsc && npm run test:mocha && npm run test:mocha:compat && npm run test:mocha:debug && npm run bench",
37-
"test:mocha": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/*.test.js",
37+
"test:mocha": "cross-env BABEL_ENV=test mocha -r @babel/register -r test/setup.js test/*.test.js",
3838
"test:mocha:compat": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js 'test/compat/index.test.js'",
3939
"test:mocha:debug": "BABEL_ENV=test mocha -r @babel/register -r test/setup.js 'test/debug/index.test.js'",
4040
"format": "prettier src/**/*.{d.ts,js} test/**/*.js --write",

src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
311311
name = name.toLowerCase().replace(XLINK_REPLACE_REGEX, 'xlink:');
312312
} else if (UNSAFE_NAME.test(name)) {
313313
continue;
314-
} else if (
315-
((name[0] === 'a' && name[1] === 'r') || name === 'draggable') &&
316-
v != null
317-
) {
314+
} else if ((name[4] === '-' || name === 'draggable') && v != null) {
318315
// serialize boolean aria-xyz or draggable attribute values as strings
319316
// `draggable` is an enumerated attribute and not Boolean. A value of `true` or `false` is mandatory
320317
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable

test/render.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ describe('render', () => {
142142
expect(rendered).to.equal(expected);
143143
});
144144

145+
it('should support false aria-* attributes', () => {
146+
let rendered = render(<div aria-checked={false} />);
147+
expect(rendered).to.equal(`<div aria-checked="false"></div>`);
148+
});
149+
150+
it('should support false data-* attributes', () => {
151+
let rendered = render(<div data-checked={false} />);
152+
expect(rendered).to.equal(`<div data-checked="false"></div>`);
153+
});
154+
145155
describe('attribute name sanitization', () => {
146156
it('should omit attributes with invalid names', () => {
147157
let rendered = render(

0 commit comments

Comments
 (0)