Skip to content

Commit b4b27b2

Browse files
TimothyGuaddaleax
authored andcommitted
readline: properly handle 0-width characters
PR-URL: #13918 Reviewed-By: James M Snell <[email protected]>
1 parent 4843d4d commit b4b27b2

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/readline.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -670,13 +670,14 @@ Interface.prototype._getDisplayPos = function(str) {
670670
row += 1;
671671
continue;
672672
}
673-
if (isFullWidthCodePoint(code)) {
673+
const width = getStringWidth(code);
674+
if (width === 0 || width === 1) {
675+
offset += width;
676+
} else { // width === 2
674677
if ((offset + 1) % col === 0) {
675678
offset++;
676679
}
677680
offset += 2;
678-
} else {
679-
offset++;
680681
}
681682
}
682683
var cols = offset % col;
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
require('../common');
3+
const { PassThrough } = require('stream');
4+
const readline = require('readline');
5+
const assert = require('assert');
6+
7+
const ctrlU = { ctrl: true, name: 'u' };
8+
9+
{
10+
const input = new PassThrough();
11+
const rl = readline.createInterface({
12+
terminal: true,
13+
input: input,
14+
prompt: ''
15+
});
16+
17+
for (const [cursor, string] of [
18+
[1, 'a'],
19+
[2, 'ab'],
20+
[2, '丁'],
21+
[0, '\u0301'], // COMBINING ACUTE ACCENT
22+
[1, 'a\u0301'], // á
23+
[0, '\u20DD'], // COMBINING ENCLOSING CIRCLE
24+
[2, 'a\u20DDb'], // a⃝b
25+
[0, '\u200E'] // LEFT-TO-RIGHT MARK
26+
]) {
27+
rl.write(string);
28+
assert.strictEqual(rl._getCursorPos().cols, cursor);
29+
rl.write(null, ctrlU);
30+
}
31+
}

0 commit comments

Comments
 (0)