Blame view
node_modules/hoek/lib/escape.js
2.61 KB
f7563de62
|
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
// Declare internals var internals = {}; exports.escapeJavaScript = function (input) { if (!input) { return ''; } var escaped = ''; for (var i = 0, il = input.length; i < il; ++i) { var charCode = input.charCodeAt(i); if (internals.isSafe(charCode)) { escaped += input[i]; } else { escaped += internals.escapeJavaScriptChar(charCode); } } return escaped; }; exports.escapeHtml = function (input) { if (!input) { return ''; } var escaped = ''; for (var i = 0, il = input.length; i < il; ++i) { var charCode = input.charCodeAt(i); if (internals.isSafe(charCode)) { escaped += input[i]; } else { escaped += internals.escapeHtmlChar(charCode); } } return escaped; }; internals.escapeJavaScriptChar = function (charCode) { if (charCode >= 256) { return '\\u' + internals.padLeft('' + charCode, 4); } var hexValue = new Buffer(String.fromCharCode(charCode), 'ascii').toString('hex'); return '\\x' + internals.padLeft(hexValue, 2); }; internals.escapeHtmlChar = function (charCode) { var namedEscape = internals.namedHtml[charCode]; if (typeof namedEscape !== 'undefined') { return namedEscape; } if (charCode >= 256) { return '&#' + charCode + ';'; } var hexValue = new Buffer(String.fromCharCode(charCode), 'ascii').toString('hex'); return '&#x' + internals.padLeft(hexValue, 2) + ';'; }; internals.padLeft = function (str, len) { while (str.length < len) { str = '0' + str; } return str; }; internals.isSafe = function (charCode) { return (typeof internals.safeCharCodes[charCode] !== 'undefined'); }; internals.namedHtml = { '38': '&', '60': '<', '62': '>', '34': '"', '160': ' ', '162': '¢', '163': '£', '164': '¤', '169': '©', '174': '®' }; internals.safeCharCodes = (function () { var safe = {}; for (var i = 32; i < 123; ++i) { if ((i >= 97) || // a-z (i >= 65 && i <= 90) || // A-Z (i >= 48 && i <= 57) || // 0-9 i === 32 || // space i === 46 || // . i === 44 || // , i === 45 || // - i === 58 || // : i === 95) { // _ safe[i] = null; } } return safe; }()); |