1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const encode_url_1 = __importDefault(require("./encode_url"));
|
6 | const escape_html_1 = __importDefault(require("./escape_html"));
|
7 | const regexUrl = /(cite|download|href|src|url)$/i;
|
8 | const regexMeta = /^(og:|twitter:)(audio|image|url|video)(:secure_url)?$/i;
|
9 | function encSrcset(str) {
|
10 | str.split(' ')
|
11 | .forEach(subStr => {
|
12 | if (subStr.match(/\S/)) {
|
13 | subStr = subStr.trim();
|
14 | str = str.replace(subStr, (0, encode_url_1.default)(subStr));
|
15 | }
|
16 | });
|
17 | return str;
|
18 | }
|
19 | function htmlTag(tag, attrs, text, escape = true) {
|
20 | if (!tag)
|
21 | throw new TypeError('tag is required!');
|
22 | let result = `<${(0, escape_html_1.default)(tag)}`;
|
23 | for (const i in attrs) {
|
24 | if (attrs[i] == null)
|
25 | result += '';
|
26 | else {
|
27 | if (i.match(regexUrl)
|
28 | || (tag === 'meta' && !String(attrs[i]).match(regexMeta) && String(Object.values(attrs)[0]).match(regexMeta))) {
|
29 | result += ` ${(0, escape_html_1.default)(i)}="${(0, encode_url_1.default)(String(attrs[i]))}"`;
|
30 | }
|
31 | else if (attrs[i] === true || i === attrs[i])
|
32 | result += ` ${(0, escape_html_1.default)(i)}`;
|
33 | else if (i.match(/srcset$/i))
|
34 | result += ` ${(0, escape_html_1.default)(i)}="${encSrcset(String(attrs[i]))}"`;
|
35 | else
|
36 | result += ` ${(0, escape_html_1.default)(i)}="${(0, escape_html_1.default)(String(attrs[i]))}"`;
|
37 | }
|
38 | }
|
39 | if (escape && text && tag !== 'style')
|
40 | text = (0, escape_html_1.default)(String(text));
|
41 | if (text && tag === 'style') {
|
42 | text = text.replace(/url\(['"](.*?)['"]\)/gi, (urlAttr, url) => {
|
43 | return `url("${(0, encode_url_1.default)(url)}")`;
|
44 | });
|
45 | }
|
46 | if (text == null)
|
47 | result += '>';
|
48 | else
|
49 | result += `>${text}</${(0, escape_html_1.default)(tag)}>`;
|
50 | return result;
|
51 | }
|
52 | module.exports = htmlTag;
|
53 |
|
\ | No newline at end of file |