Skip to content

Commit

Permalink
Allows empty attributes in html tag helper (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaviju authored and NoahDragon committed Aug 29, 2018
1 parent 2b5e282 commit 38933b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/html_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ function htmlTag(tag, attrs, text) {
var result = '<' + tag;

for (var i in attrs) {
if (attrs[i]) result += ' ' + i + '="' + attrs[i] + '"';
if (
attrs[i] !== undefined
&& attrs[i] !== null
) result += ' ' + i + '="' + attrs[i] + '"';
}

result += text == null ? '>' : '>' + text + '</' + tag + '>';
Expand Down
28 changes: 28 additions & 0 deletions test/scripts/html_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ describe('htmlTag', function() {
}, 'My blog').should.eql('<a href="http://zespia.tw">My blog</a>');
});

it('tag + empty ALT attr', function() {
htmlTag('img', {
src: 'http://placekitten.com/200/300',
alt: ''
}).should.eql('<img src="http://placekitten.com/200/300" alt="">');
});

it('passing a zero as attribute', function() {
htmlTag('a', {
href: 'http://zespia.tw',
tabindex: 0
}, 'My blog').should.eql('<a href="http://zespia.tw" tabindex="0">My blog</a>');
});

it('passing a null alt attribute', function() {
htmlTag('a', {
href: 'http://zespia.tw',
alt: null
}, 'My blog').should.eql('<a href="http://zespia.tw">My blog</a>');
});

it('passing a undefined alt attribute', function() {
htmlTag('a', {
href: 'http://zespia.tw',
alt: undefined
}, 'My blog').should.eql('<a href="http://zespia.tw">My blog</a>');
});

it('tag is required', function() {
try {
htmlTag();
Expand Down

0 comments on commit 38933b6

Please sign in to comment.