Skip to content

Commit 1bba6ca

Browse files
committed
Fix falsey and boolean attributes so they render properly in HTML and XML modes
1 parent 3554071 commit 1bba6ca

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,15 @@ export default function renderToString(vnode, context, opts, inner) {
140140
if (name==='dangerouslySetInnerHTML') {
141141
html = v && v.__html;
142142
}
143-
else if (!falsey(v) && typeof v!=='function') {
143+
else if ((v || v===0) && typeof v!=='function') {
144+
if (v===true) {
145+
v = name;
146+
// in non-xml mode, allow boolean attributes
147+
if (!opts || !opts.xml) {
148+
s += ' ' + name;
149+
continue;
150+
}
151+
}
144152
s += ` ${name}="${encodeEntities(v)}"`;
145153
}
146154
}

test/render.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ describe('render', () => {
1919
expected = `<div></div>`;
2020

2121
expect(rendered).to.equal(expected);
22+
23+
expect(render(<div foo={0} />)).to.equal(`<div foo="0"></div>`)
24+
});
25+
26+
it('should collapse collapsible attributes', () => {
27+
let rendered = render(<div class="" style="" foo={true} bar />),
28+
expected = `<div foo bar></div>`;
29+
30+
expect(rendered).to.equal(expected);
2231
});
2332

2433
it('should omit functions', () => {
@@ -254,4 +263,22 @@ describe('render', () => {
254263
expect(rendered).to.equal('<div a="a" b="b" b1="b1" c="c"></div>');
255264
});
256265
});
266+
267+
describe('xml:true', () => {
268+
let renderXml = jsx => render(jsx, null, { xml:true });
269+
270+
it('should render end-tags', () => {
271+
expect(renderXml(<div />)).to.equal(`<div />`);
272+
expect(renderXml(<a />)).to.equal(`<a />`);
273+
expect(renderXml(<a>b</a>)).to.equal(`<a>b</a>`);
274+
});
275+
276+
it('should render boolean attributes with named values', () => {
277+
expect(renderXml(<div foo bar />)).to.equal(`<div foo="foo" bar="bar" />`);
278+
});
279+
280+
it('should exclude falsey attributes', () => {
281+
expect(renderXml(<div foo={false} bar={0} />)).to.equal(`<div bar="0" />`);
282+
});
283+
});
257284
});

0 commit comments

Comments
 (0)