Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
readline: fix unicode line separators being ignored
  • Loading branch information
dario-piotrowicz committed Apr 12, 2025
commit 522370665d908a8fb987756c1b75672d27f90c99
11 changes: 9 additions & 2 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ const { StringDecoder } = require('string_decoder');
const kHistorySize = 30;
const kMaxUndoRedoStackSize = 2048;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
const lineEnding = /\r?\n|\r(?!\n)/g;
/**
* The end of a line is signaled by either one of the following:
* - \r\n
* - \n
* - \r followed by something other than \n
* - \u2028 (Unicode 'LINE SEPARATOR')
* - \u2029 (Unicode 'PARAGRAPH SEPARATOR')
*/
const lineEnding = /\r?\n|\r(?!\n)|\u2028|\u2029/g;
Comment thread
ljharb marked this conversation as resolved.

const kLineObjectStream = Symbol('line object stream');
const kQuestionCancel = Symbol('kQuestionCancel');
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-readline-line-separators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('node:assert');
const readline = require('node:readline');
const { Readable } = require('node:stream');

const str = '123\n456\r789\u{2028}ABC\u{2029}DEF';

const rli = new readline.Interface({
input: Readable.from(str),
});

const linesRead = [];
rli.on('line', (line) => linesRead.push(line));

rli.on('close', common.mustCall(() => {
const regexpLines = str.split(/^/m).map((line) => line.trim());
// Readline interprets different lines in the same way js regular expressions do
assert.deepStrictEqual(linesRead, regexpLines);
Comment thread
dario-piotrowicz marked this conversation as resolved.
Outdated
}));