Skip to content

Commit c47064b

Browse files
committed
fix render empty string attrs keys
1 parent 7557c70 commit c47064b

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

lib/posthtml-render.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ function postHTMLRender(tree, options) {
8484
function attrs(obj) {
8585
var attr = '';
8686
for (var key in obj) {
87-
if (obj[key] && typeof obj[key] === 'string' || typeof obj[key] === 'number') {
88-
attr += ' ' + key + '="' + obj[key] + '"';
89-
} else if (obj[key] === '' || typeof obj[key] === 'boolean' && obj[key]) {
90-
attr += ' ' + key;
91-
}
87+
if (
88+
typeof obj[key] === 'boolean' &&
89+
obj[key]
90+
) attr += ' ' + key;
91+
else if (
92+
typeof obj[key] === 'string' ||
93+
typeof obj[key] === 'number'
94+
) attr += ' ' + key + '="' + obj[key] + '"';
9295
}
9396
return attr;
9497
}

posthtml-render.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ describe('PostHTML-Render test', function() {
3131
expect(render({ content: [555] })).to.eql('<div>555</div>');
3232
});
3333

34-
it('node array in content', function () {
35-
expect(render({content: [
34+
it('node array in content', function() {
35+
expect(render({ content: [
3636
[
3737
555,
38-
{tag: 'div', content: 666},
38+
{ tag: 'div', content: 666 },
3939
777
4040
]
41-
]})).to.eql('<div>555<div>666</div>777</div>')
42-
})
41+
]})).to.eql('<div>555<div>666</div>777</div>');
42+
});
4343

4444
it('string node', function() {
4545
expect(render(['Hello world!'])).to.eql('Hello world!');
@@ -65,12 +65,18 @@ describe('PostHTML-Render test', function() {
6565

6666
it('content test', function() {
6767
expect(render({ content: [{ content: [{ content: ['Test', {}] }] }] }))
68-
.to.eql('<div><div><div>Test<div></div></div></div></div>');
68+
.to.eql('<div><div><div>Test<div></div></div></div></div>');
6969
});
7070

7171
describe('attrs', function() {
7272
it('key', function() {
73-
expect(render({ attrs: { id: 'header' } })).to.eql('<div id="header"></div>');
73+
expect(render({ attrs: { id: 'header' } }))
74+
.to.eql('<div id="header"></div>');
75+
});
76+
77+
it('empty key', function() {
78+
expect(render({ attrs: { alt: '' } }))
79+
.to.eql('<div alt=""></div>');
7480
});
7581

7682
it('multi attrs', function() {
@@ -79,15 +85,13 @@ describe('PostHTML-Render test', function() {
7985
});
8086

8187
it('true', function() {
82-
expect(render({ attrs: { disabled: true } })).to.eql('<div disabled></div>');
88+
expect(render({ attrs: { disabled: true } }))
89+
.to.eql('<div disabled></div>');
8390
});
8491

8592
it('false', function() {
86-
expect(render({ attrs: { disabled: false } })).to.eql('<div></div>');
87-
});
88-
89-
it('empty attrs', function() {
90-
expect(render({ attrs: { disabled: '' } })).to.eql('<div disabled></div>');
93+
expect(render({ attrs: { disabled: false } }))
94+
.to.eql('<div></div>');
9195
});
9296

9397
it('number attrs', function() {

0 commit comments

Comments
 (0)