Skip to content

Commit 2aed11d

Browse files
committed
Add special treatment for style and class DOM attributes to match DOM renderer
1 parent 53bc99b commit 2aed11d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/jsx.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ function attributeHook(name, value, context, opts, isComponent) {
2424
let type = typeof value;
2525

2626
// always skip null & undefined values, skip false DOM attributes, skip functions if told to
27-
if (value==null || (!isComponent && opts.skipFalseAttributes && value===false) || (type==='function' && !opts.functions)) return '';
27+
if (value==null || (type==='function' && !opts.functions)) return '';
28+
29+
if (opts.skipFalseAttributes && !isComponent && (value===false || ((name==='class' || name==='style') && value===''))) return '';
2830

2931
let indentChar = typeof opts.pretty==='string' ? opts.pretty : '\t';
3032
if (type!=='string') {

test/jsx.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ describe('jsx', () => {
3131
`);
3232
});
3333

34+
it('should not render empty class or style DOM attributes', () => {
35+
expect(renderJsx(<a b={false} />)).to.equal('<a></a>');
36+
expect(renderJsx(<a b="" />)).to.equal('<a b=""></a>');
37+
expect(renderJsx(<a class={false} />)).to.equal('<a></a>');
38+
expect(renderJsx(<a style={false} />)).to.equal('<a></a>');
39+
expect(renderJsx(<a class="" />)).to.equal('<a></a>');
40+
expect(renderJsx(<a style="" />)).to.equal('<a></a>');
41+
});
42+
3443
it('should render JSX attributes inline if short enough', () => {
3544
expect(renderJsx(
3645
<a b="c">bar</a>

0 commit comments

Comments
 (0)