Skip to content

Commit 1393e25

Browse files
committed
Combine surrogate pairs into one escape sequence when encoding.
1 parent 685e6fb commit 1393e25

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/js-yaml/dumper.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,21 @@ function foldLine(line, width) {
461461
// Escapes a double-quoted string.
462462
function escapeString(string) {
463463
var result = '';
464-
var char;
464+
var char, nextChar;
465465
var escapeSeq;
466466

467467
for (var i = 0; i < string.length; i++) {
468468
char = string.charCodeAt(i);
469+
// Check for surrogate pairs.
470+
if (char >= 0xD800 && char <= 0xDBFF/* high surrogate */) {
471+
nextChar = string.charCodeAt(i + 1);
472+
if (nextChar >= 0xDC00 && nextChar <= 0xDFFF/* low surrogate */) {
473+
// Combine the surrogate pair and store it escaped.
474+
result += encodeHex((char - 0xD800) * 0x400 + nextChar - 0xDC00 + 0x10000);
475+
// Advance index one extra since we already used that char here.
476+
i++; continue;
477+
}
478+
}
469479
escapeSeq = ESCAPE_SEQUENCES[char];
470480
result += !escapeSeq && isPrintable(char)
471481
? string[i]

0 commit comments

Comments
 (0)