Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@hapi/hoek/lib/escapeHtml.js
2593 views
1
'use strict';
2
3
const internals = {};
4
5
6
module.exports = function (input) {
7
8
if (!input) {
9
return '';
10
}
11
12
let escaped = '';
13
14
for (let i = 0; i < input.length; ++i) {
15
16
const charCode = input.charCodeAt(i);
17
18
if (internals.isSafe(charCode)) {
19
escaped += input[i];
20
}
21
else {
22
escaped += internals.escapeHtmlChar(charCode);
23
}
24
}
25
26
return escaped;
27
};
28
29
30
internals.escapeHtmlChar = function (charCode) {
31
32
const namedEscape = internals.namedHtml[charCode];
33
if (typeof namedEscape !== 'undefined') {
34
return namedEscape;
35
}
36
37
if (charCode >= 256) {
38
return '&#' + charCode + ';';
39
}
40
41
const hexValue = charCode.toString(16).padStart(2, '0');
42
return `&#x${hexValue};`;
43
};
44
45
46
internals.isSafe = function (charCode) {
47
48
return (typeof internals.safeCharCodes[charCode] !== 'undefined');
49
};
50
51
52
internals.namedHtml = {
53
'38': '&amp;',
54
'60': '&lt;',
55
'62': '&gt;',
56
'34': '&quot;',
57
'160': '&nbsp;',
58
'162': '&cent;',
59
'163': '&pound;',
60
'164': '&curren;',
61
'169': '&copy;',
62
'174': '&reg;'
63
};
64
65
66
internals.safeCharCodes = (function () {
67
68
const safe = {};
69
70
for (let i = 32; i < 123; ++i) {
71
72
if ((i >= 97) || // a-z
73
(i >= 65 && i <= 90) || // A-Z
74
(i >= 48 && i <= 57) || // 0-9
75
i === 32 || // space
76
i === 46 || // .
77
i === 44 || // ,
78
i === 45 || // -
79
i === 58 || // :
80
i === 95) { // _
81
82
safe[i] = null;
83
}
84
}
85
86
return safe;
87
}());
88
89