| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 668284b commit a0326f0
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -179,10 +179,10 @@ const kArrayType = 1; | |||
| 179 | 179 | const kArrayExtrasType = 2; | |
| 180 | 180 | ||
| 181 | 181 | /* eslint-disable no-control-regex */ | |
| 182 | - const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c\x7f-\x9f]/; | ||
| 183 | - const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c\x7f-\x9f]/g; | ||
| 184 | - const strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c\x7f-\x9f]/; | ||
| 185 | - const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c\x7f-\x9f]/g; | ||
| 182 | + const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/; | ||
| 183 | + const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g; | ||
| 184 | + const strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/; | ||
| 185 | + const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g; | ||
| 186 | 186 | /* eslint-enable no-control-regex */ | |
| 187 | 187 | ||
| 188 | 188 | const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/; | |
@@ -463,7 +463,10 @@ function addQuotes(str, quotes) { | |||
| 463 | 463 | return `'${str}'`; | |
| 464 | 464 | } | |
| 465 | 465 | ||
| 466 | - const escapeFn = (str) => meta[StringPrototypeCharCodeAt(str)]; | ||
| 466 | + function escapeFn(str) { | ||
| 467 | + const charCode = StringPrototypeCharCodeAt(str); | ||
| 468 | + return meta.length > charCode ? meta[charCode] : `\\u${charCode.toString(16)}`; | ||
| 469 | + } | ||
| 467 | 470 | ||
| 468 | 471 | // Escape control characters, single quotes and the backslash. | |
| 469 | 472 | // This is similar to JSON stringify escaping. | |
@@ -501,8 +504,7 @@ function strEscape(str) { | |||
| 501 | 504 | ||
| 502 | 505 | let result = ''; | |
| 503 | 506 | let last = 0; | |
| 504 | - const lastIndex = str.length; | ||
| 505 | - for (let i = 0; i < lastIndex; i++) { | ||
| 507 | + for (let i = 0; i < str.length; i++) { | ||
| 506 | 508 | const point = StringPrototypeCharCodeAt(str, i); | |
| 507 | 509 | if (point === singleQuote || | |
| 508 | 510 | point === 92 || | |
@@ -514,10 +516,20 @@ function strEscape(str) { | |||
| 514 | 516 | result += `${StringPrototypeSlice(str, last, i)}${meta[point]}`; | |
| 515 | 517 | } | |
| 516 | 518 | last = i + 1; | |
| 519 | + } else if (point >= 0xd800 && point <= 0xdfff) { | ||
| 520 | + if (point <= 0xdbff && i + 1 < str.length) { | ||
| 521 | + const point = StringPrototypeCharCodeAt(str, i + 1); | ||
| 522 | + if (point >= 0xdc00 && point <= 0xdfff) { | ||
| 523 | + i++; | ||
| 524 | + continue; | ||
| 525 | + } | ||
| 526 | + } | ||
| 527 | + result += `${StringPrototypeSlice(str, last, i)}${`\\u${point.toString(16)}`}`; | ||
| 528 | + last = i + 1; | ||
| 517 | 529 | } | |
| 518 | 530 | } | |
| 519 | 531 | ||
| 520 | - if (last !== lastIndex) { | ||
| 532 | + if (last !== str.length) { | ||
| 521 | 533 | result += StringPrototypeSlice(str, last); | |
| 522 | 534 | } | |
| 523 | 535 | return addQuotes(result, singleQuote); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -837,6 +837,48 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}'); | |||
| 837 | 837 | ); | |
| 838 | 838 | } | |
| 839 | 839 | ||
| 840 | + // Escape unpaired surrogate pairs. | ||
| 841 | + { | ||
| 842 | + const edgeChar = String.fromCharCode(0xd799); | ||
| 843 | + | ||
| 844 | + for (let charCode = 0xD800; charCode < 0xDFFF; charCode++) { | ||
| 845 | + const surrogate = String.fromCharCode(charCode); | ||
| 846 | + | ||
| 847 | + assert.strictEqual( | ||
| 848 | + util.inspect(surrogate), | ||
| 849 | + `'\\u${charCode.toString(16)}'` | ||
| 850 | + ); | ||
| 851 | + assert.strictEqual( | ||
| 852 | + util.inspect(`${'a'.repeat(200)}${surrogate}`), | ||
| 853 | + `'${'a'.repeat(200)}\\u${charCode.toString(16)}'` | ||
| 854 | + ); | ||
| 855 | + assert.strictEqual( | ||
| 856 | + util.inspect(`${surrogate}${'a'.repeat(200)}`), | ||
| 857 | + `'\\u${charCode.toString(16)}${'a'.repeat(200)}'` | ||
| 858 | + ); | ||
| 859 | + if (charCode < 0xdc00) { | ||
| 860 | + const highSurrogate = surrogate; | ||
| 861 | + const lowSurrogate = String.fromCharCode(charCode + 1024); | ||
| 862 | + assert( | ||
| 863 | + !util.inspect( | ||
| 864 | + `${edgeChar}${highSurrogate}${lowSurrogate}${edgeChar}` | ||
| 865 | + ).includes('\\u') | ||
| 866 | + ); | ||
| 867 | + assert.strictEqual( | ||
| 868 | + (util.inspect( | ||
| 869 | + `${highSurrogate}${highSurrogate}${lowSurrogate}` | ||
| 870 | + ).match(/\\u/g) ?? []).length, | ||
| 871 | + 1 | ||
| 872 | + ); | ||
| 873 | + } else { | ||
| 874 | + assert.strictEqual( | ||
| 875 | + util.inspect(`${edgeChar}${surrogate}${edgeChar}`), | ||
| 876 | + `'${edgeChar}\\u${charCode.toString(16)}${edgeChar}'` | ||
| 877 | + ); | ||
| 878 | + } | ||
| 879 | + } | ||
| 880 | + } | ||
| 881 | + | ||
| 840 | 882 | // Test util.inspect.styles and util.inspect.colors. | |
| 841 | 883 | { | |
| 842 | 884 | function testColorStyle(style, input, implicit) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments