-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (36 loc) · 941 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*!
* html-tag <https://github.com/jonschlinkert/html-tag>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var typeOf = require('kind-of');
var isVoid = require('is-self-closing');
module.exports = function(tag, attribs, text) {
var voided = text === false || attribs === false;
if (typeOf(attribs) !== 'object') {
text = attribs;
attribs = {};
}
if (typeof text === 'undefined' || text === false) {
text = '';
}
if (typeof text !== 'string') {
throw new TypeError('expected text to be a string');
}
var html = '<' + tag;
for (var key in attribs) {
var val = attribs[key];
if (val === true) {
html += ' ' + key;
}
if (typeof val === 'string') {
html += ' ' + key + '="' + val + '"';
}
}
if (isVoid(tag) || voided === true) {
return html + '>' + text;
}
return html + '>' + text + '</' + tag + '>';
};