Skip to content

Commit

Permalink
Accept non-strings in htmlEscapeTag and htmlUnescapeTag (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored and sindresorhus committed May 30, 2019
1 parent 235683e commit 633c0dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.htmlUnescape = htmlString => htmlString
exports.htmlEscapeTag = (strings, ...values) => {
let output = strings[0];
for (let i = 0; i < values.length; i++) {
output = output + exports.htmlEscape(values[i]) + strings[i + 1];
output = output + exports.htmlEscape(String(values[i])) + strings[i + 1];
}

return output;
Expand All @@ -26,7 +26,7 @@ exports.htmlEscapeTag = (strings, ...values) => {
exports.htmlUnescapeTag = (strings, ...values) => {
let output = strings[0];
for (let i = 0; i < values.length; i++) {
output = output + exports.htmlUnescape(values[i]) + strings[i + 1];
output = output + exports.htmlUnescape(String(values[i])) + strings[i + 1];
}

return output;
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ test('htmlEscapeTag', t => {
t.is(htmlEscapeTag`Hello <em><>${'<>'}</em>`, 'Hello <em><>&lt;&gt;</em>');
});

test('htmlEscapeTag non-strings', t => {
t.is(htmlEscapeTag`foobarz${undefined}`, 'foobarzundefined');
t.is(htmlEscapeTag`🦄 ${true}`, '🦄 true');
t.is(htmlEscapeTag`Hello <em><>${1}</em>`, 'Hello <em><>1</em>');
});

test('htmlUnescapeTag', t => {
t.is(htmlUnescapeTag`foobarz${'&amp;&lt;&gt;&quot;&#39;'}`, 'foobarz&<>"\'');
t.is(htmlUnescapeTag`🦄 ${'&amp;'} 🐐`, '🦄 & 🐐');
t.is(htmlUnescapeTag`Hello <em><>${'&lt;&gt;'}</em>`, 'Hello <em><><></em>');
});

test('htmlUnescapeTag non-strings', t => {
t.is(htmlUnescapeTag`foobarz${undefined}`, 'foobarzundefined');
t.is(htmlUnescapeTag`🦄 ${true}`, '🦄 true');
t.is(htmlUnescapeTag`Hello <em><>${1}</em>`, 'Hello <em><>1</em>');
});

test('htmlEscapeTag & htmlUnescapeTag', t => {
const input = '&<>"\'';
const actual = htmlUnescapeTag`${htmlEscapeTag`${input}`}`;
Expand Down

0 comments on commit 633c0dc

Please sign in to comment.