Skip to content

Commit 984f8a1

Browse files
readline: fix unicode line separators being ignored
1 parent ffc1cf6 commit 984f8a1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/internal/readline/interface.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ const { StringDecoder } = require('string_decoder');
7575
const kHistorySize = 30;
7676
const kMaxUndoRedoStackSize = 2048;
7777
const kMincrlfDelay = 100;
78-
// \r\n, \n, or \r followed by something other than \n
79-
const lineEnding = /\r?\n|\r(?!\n)/g;
78+
/**
79+
* The end of a line is signaled by either one of the following:
80+
* - \r\n
81+
* - \n
82+
* - \r followed by something other than \n
83+
* - \u2028 (Unicode 'LINE SEPARATOR')
84+
* - \u2029 (Unicode 'PARAGRAPH SEPARATOR')
85+
*/
86+
const lineEnding = /\r?\n|\r(?!\n)|\u2028|\u2029/g;
8087

8188
const kLineObjectStream = Symbol('line object stream');
8289
const kQuestionCancel = Symbol('kQuestionCancel');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('node:assert');
4+
const readline = require('node:readline');
5+
const { Readable } = require('node:stream');
6+
7+
const str = '123\n456\r789\u{2028}ABC\u{2029}DEF';
8+
9+
const rli = new readline.Interface({
10+
input: Readable.from(str),
11+
});
12+
13+
const linesRead = [];
14+
rli.on('line', (line) => linesRead.push(line));
15+
16+
rli.on('close', common.mustCall(() => {
17+
const regexpLines = str.split(/^/m).map((line) => line.trim());
18+
// Readline interprets different lines in the same way js regular expressions do
19+
assert.deepStrictEqual(linesRead, regexpLines);
20+
}));

0 commit comments

Comments
 (0)