@kosatyi/ejs 0.0.42 → 0.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/element.mjs +110 -0
- package/package.json +1 -1
package/dist/element.mjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const isUndefined = (v) => typeof v === 'undefined';
|
|
2
|
+
|
|
3
|
+
Object.prototype.toString.call(
|
|
4
|
+
typeof process !== 'undefined' ? process : 0
|
|
5
|
+
) === '[object process]';
|
|
6
|
+
|
|
7
|
+
const symbolEntities = {
|
|
8
|
+
"'": "'",
|
|
9
|
+
'\\': '\\',
|
|
10
|
+
'\r': 'r',
|
|
11
|
+
'\n': 'n',
|
|
12
|
+
'\t': 't',
|
|
13
|
+
'\u2028': 'u2028',
|
|
14
|
+
'\u2029': 'u2029',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const htmlEntities = {
|
|
18
|
+
'&': '&',
|
|
19
|
+
'<': '<',
|
|
20
|
+
'>': '>',
|
|
21
|
+
'"': '"',
|
|
22
|
+
"'": ''',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const regexKeys = (obj) =>
|
|
26
|
+
new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
|
|
27
|
+
|
|
28
|
+
const htmlEntitiesMatch = regexKeys(htmlEntities);
|
|
29
|
+
|
|
30
|
+
regexKeys(symbolEntities);
|
|
31
|
+
|
|
32
|
+
const entities = (string = '') => {
|
|
33
|
+
return ('' + string).replace(
|
|
34
|
+
htmlEntitiesMatch,
|
|
35
|
+
(match) => htmlEntities[match]
|
|
36
|
+
)
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const each = (object, callback) => {
|
|
40
|
+
let prop;
|
|
41
|
+
for (prop in object) {
|
|
42
|
+
if (hasProp(object, prop)) {
|
|
43
|
+
callback(object[prop], prop, object);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const map = (object, callback, context) => {
|
|
49
|
+
const result = [];
|
|
50
|
+
each(
|
|
51
|
+
object,
|
|
52
|
+
(value, key, object) => {
|
|
53
|
+
let item = callback(value, key, object);
|
|
54
|
+
if (isUndefined(item) === false) {
|
|
55
|
+
result.push(item);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return result
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const hasProp = (object, prop) => {
|
|
62
|
+
return object && object.hasOwnProperty(prop)
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const selfClosed = [
|
|
66
|
+
'area',
|
|
67
|
+
'base',
|
|
68
|
+
'br',
|
|
69
|
+
'col',
|
|
70
|
+
'embed',
|
|
71
|
+
'hr',
|
|
72
|
+
'img',
|
|
73
|
+
'input',
|
|
74
|
+
'link',
|
|
75
|
+
'meta',
|
|
76
|
+
'param',
|
|
77
|
+
'source',
|
|
78
|
+
'track',
|
|
79
|
+
'wbr',
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const space = ' ';
|
|
83
|
+
const quote = '"';
|
|
84
|
+
const equal = '=';
|
|
85
|
+
const slash = '/';
|
|
86
|
+
const lt = '<';
|
|
87
|
+
const gt = '>';
|
|
88
|
+
|
|
89
|
+
const element = (tag, attrs, content) => {
|
|
90
|
+
const result = [];
|
|
91
|
+
const hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
92
|
+
const attributes = map(attrs, (value, key) => {
|
|
93
|
+
if (value !== null && value !== undefined) {
|
|
94
|
+
return [
|
|
95
|
+
entities(key),
|
|
96
|
+
[quote, entities(value), quote].join(''),
|
|
97
|
+
].join(equal)
|
|
98
|
+
}
|
|
99
|
+
}).join(space);
|
|
100
|
+
result.push([lt, tag, space, attributes, gt].join(''));
|
|
101
|
+
if (content) {
|
|
102
|
+
result.push(content instanceof Array ? content.join('') : content);
|
|
103
|
+
}
|
|
104
|
+
if (hasClosedTag) {
|
|
105
|
+
result.push([lt, slash, tag, gt].join(''));
|
|
106
|
+
}
|
|
107
|
+
return result.join('')
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export { element };
|