Changeset 0c6b92a for imaps-frontend/node_modules/jsesc/jsesc.js
- Timestamp:
- 12/12/24 17:06:06 (5 weeks ago)
- Branches:
- main
- Parents:
- d565449
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/node_modules/jsesc/jsesc.js
rd565449 r0c6b92a 29 29 }; 30 30 31 const fourHexEscape = (hex) => { 32 return '\\u' + ('0000' + hex).slice(-4); 33 } 34 35 const hexadecimal = (code, lowercase) => { 36 let hexadecimal = code.toString(16); 37 if (lowercase) return hexadecimal; 38 return hexadecimal.toUpperCase(); 39 }; 40 31 41 const toString = object.toString; 32 42 const isArray = Array.isArray; 33 const isBuffer = Buffer.isBuffer; 43 const isBuffer = (value) => { 44 return typeof Buffer === 'function' && Buffer.isBuffer(value); 45 }; 34 46 const isObject = (value) => { 35 47 // This is a very simple check, but it’s good enough for what we need. … … 58 70 // https://mathiasbynens.be/notes/javascript-escapes#single 59 71 const singleEscapes = { 60 '"': '\\"',61 '\'': '\\\'',62 72 '\\': '\\\\', 63 73 '\b': '\\b', … … 69 79 // '\v': '\\x0B' 70 80 }; 71 const regexSingleEscape = /[ "'\\\b\f\n\r\t]/;81 const regexSingleEscape = /[\\\b\f\n\r\t]/; 72 82 73 83 const regexDigit = /[0-9]/; 74 const regexWhitelist = /[ !#-&\(-\[\]-_a-~]/; 84 const regexWhitespace = /[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/; 85 86 const escapeEverythingRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^]/g; 87 const escapeNonAsciiRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^ !#-&\(-\[\]-_a-~]/g; 75 88 76 89 const jsesc = (argument, options) => { … … 235 248 } 236 249 237 const string = argument; 238 // Loop over each code unit in the string and escape it 239 let index = -1; 240 const length = string.length; 241 result = ''; 242 while (++index < length) { 243 const character = string.charAt(index); 244 if (options.es6) { 245 const first = string.charCodeAt(index); 246 if ( // check if it’s the start of a surrogate pair 247 first >= 0xD800 && first <= 0xDBFF && // high surrogate 248 length > index + 1 // there is a next code unit 249 ) { 250 const second = string.charCodeAt(index + 1); 251 if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate 252 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae 253 const codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; 254 let hexadecimal = codePoint.toString(16); 255 if (!lowercaseHex) { 256 hexadecimal = hexadecimal.toUpperCase(); 257 } 258 result += '\\u{' + hexadecimal + '}'; 259 ++index; 260 continue; 261 } 262 } 263 } 264 if (!options.escapeEverything) { 265 if (regexWhitelist.test(character)) { 266 // It’s a printable ASCII character that is not `"`, `'` or `\`, 267 // so don’t escape it. 268 result += character; 269 continue; 270 } 271 if (character == '"') { 272 result += quote == character ? '\\"' : character; 273 continue; 274 } 275 if (character == '`') { 276 result += quote == character ? '\\`' : character; 277 continue; 278 } 279 if (character == '\'') { 280 result += quote == character ? '\\\'' : character; 281 continue; 282 } 283 } 250 const regex = options.escapeEverything ? escapeEverythingRegex : escapeNonAsciiRegex; 251 result = argument.replace(regex, (char, pair, lone, quoteChar, index, string) => { 252 if (pair) { 253 if (options.minimal) return pair; 254 const first = pair.charCodeAt(0); 255 const second = pair.charCodeAt(1); 256 if (options.es6) { 257 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae 258 const codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; 259 const hex = hexadecimal(codePoint, lowercaseHex); 260 return '\\u{' + hex + '}'; 261 } 262 return fourHexEscape(hexadecimal(first, lowercaseHex)) + fourHexEscape(hexadecimal(second, lowercaseHex)); 263 } 264 265 if (lone) { 266 return fourHexEscape(hexadecimal(lone.charCodeAt(0), lowercaseHex)); 267 } 268 284 269 if ( 285 char acter== '\0' &&270 char == '\0' && 286 271 !json && 287 272 !regexDigit.test(string.charAt(index + 1)) 288 273 ) { 289 result += '\\0'; 290 continue; 291 } 292 if (regexSingleEscape.test(character)) { 274 return '\\0'; 275 } 276 277 if (quoteChar) { 278 if (quoteChar == quote || options.escapeEverything) { 279 return '\\' + quoteChar; 280 } 281 return quoteChar; 282 } 283 284 if (regexSingleEscape.test(char)) { 293 285 // no need for a `hasOwnProperty` check here 294 result += singleEscapes[character]; 295 continue; 296 } 297 const charCode = character.charCodeAt(0); 298 if (options.minimal && charCode != 0x2028 && charCode != 0x2029) { 299 result += character; 300 continue; 301 } 302 let hexadecimal = charCode.toString(16); 303 if (!lowercaseHex) { 304 hexadecimal = hexadecimal.toUpperCase(); 305 } 306 const longhand = hexadecimal.length > 2 || json; 307 const escaped = '\\' + (longhand ? 'u' : 'x') + 308 ('0000' + hexadecimal).slice(longhand ? -4 : -2); 309 result += escaped; 310 continue; 286 return singleEscapes[char]; 287 } 288 289 if (options.minimal && !regexWhitespace.test(char)) { 290 return char; 291 } 292 293 const hex = hexadecimal(char.charCodeAt(0), lowercaseHex); 294 if (json || hex.length > 2) { 295 return fourHexEscape(hex); 296 } 297 298 return '\\x' + ('00' + hex).slice(-2); 299 }); 300 301 if (quote == '`') { 302 result = result.replace(/\$\{/g, '\\${'); 303 } 304 if (options.isScriptContext) { 305 // https://mathiasbynens.be/notes/etago 306 result = result 307 .replace(/<\/(script|style)/gi, '<\\/$1') 308 .replace(/<!--/g, json ? '\\u003C!--' : '\\x3C!--'); 311 309 } 312 310 if (options.wrap) { 313 311 result = quote + result + quote; 314 312 } 315 if (quote == '`') {316 result = result.replace(/\$\{/g, '\\\$\{');317 }318 if (options.isScriptContext) {319 // https://mathiasbynens.be/notes/etago320 return result321 .replace(/<\/(script|style)/gi, '<\\/$1')322 .replace(/<!--/g, json ? '\\u003C!--' : '\\x3C!--');323 }324 313 return result; 325 314 }; 326 315 327 jsesc.version = ' 2.5.2';316 jsesc.version = '3.0.2'; 328 317 329 318 module.exports = jsesc;
Note:
See TracChangeset
for help on using the changeset viewer.