Skip to content

Commit 01aeb38

Browse files
TimothyGujasnell
authored andcommitted
readline: properly handle 0-width characters
PR-URL: #13918 Reviewed-By: James M Snell <[email protected]>
1 parent a71d205 commit 01aeb38

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

lib/readline.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,14 @@ Interface.prototype._getDisplayPos = function(str) {
682682
row += 1;
683683
continue;
684684
}
685-
if (isFullWidthCodePoint(code)) {
685+
const width = getStringWidth(code);
686+
if (width === 0 || width === 1) {
687+
offset += width;
688+
} else { // width === 2
686689
if ((offset + 1) % col === 0) {
687690
offset++;
688691
}
689692
offset += 2;
690-
} else {
691-
offset++;
692693
}
693694
}
694695
var cols = offset % col;
Lines changed: 31 additions & 0 deletions
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)