Skip to content

Commit e2b94dc

Browse files
dario-piotrowiczRafaelGSS
authored andcommitted
readline: fix unicode line separators being ignored
PR-URL: #57591 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 70bd8bc commit e2b94dc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/internal/readline/interface.js

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

8491
const kLineObjectStream = Symbol('line object stream');
8592
const kQuestionCancel = Symbol('kQuestionCancel');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 = '012\n345\r67\r\n89\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+
assert.deepStrictEqual(linesRead, ['012', '345', '67', '89', 'ABC', 'DEF']);
18+
}));

0 commit comments

Comments
 (0)