Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret \r\n as newline character #500

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions packages/linkifyjs/src/scanner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import * as tk from './text.mjs';
import * as re from './regexp.mjs';
import assign from './assign.mjs';

const NL = '\n'; // New line character
const CR = '\r'; // carriage-return character
const LF = '\n'; // line-feed character
const EMOJI_VARIATION = '\ufe0f'; // Variation selector, follows heart and others
const EMOJI_JOINER = '\u200d'; // zero-width joiner
const OBJECT_REPLACEMENT = '\ufffc'; // whitespace placeholder that sometimes appears in rich text editors
Expand Down Expand Up @@ -121,10 +122,15 @@ export function init(customSchemes = []) {
// Whitespace jumps
// Tokens of only non-newline whitespace are arbitrarily long
// If any whitespace except newline, more whitespace!
const Nl = tt(Start, LF, tk.NL, { [fsm.whitespace]: true });
const Cr = tt(Start, CR, tk.WS, { [fsm.whitespace]: true });
const Ws = tr(Start, re.SPACE, tk.WS, { [fsm.whitespace]: true });
tt(Start, OBJECT_REPLACEMENT, Ws);
tt(Start, NL, tk.NL, { [fsm.whitespace]: true });
tt(Ws, NL); // non-accepting state to avoid mixing whitespaces
tt(Cr, LF, Nl); // \r\n
tt(Cr, OBJECT_REPLACEMENT, Ws);
tr(Cr, re.SPACE, Ws);
tt(Ws, CR); // non-accepting state to avoid mixing whitespaces
tt(Ws, LF); // non-accepting state to avoid mixing whitespaces
tr(Ws, re.SPACE, Ws);
tt(Ws, OBJECT_REPLACEMENT, Ws);

Expand Down
4 changes: 4 additions & 0 deletions test/spec/linkifyjs/scanner.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const tests = [
['.', [t.DOT], ['.']],
['-', [t.HYPHEN], ['-']],
['\n', [t.NL], ['\n']],
['\r\n', [t.NL], ['\r\n']],
[' \r\n', [t.WS, t.NL], [' ', '\r\n']],
['\r\n ', [t.NL, t.WS], ['\r\n', ' ']],
['\r \n', [t.WS, t.NL], ['\r ', '\n']],
['+', [t.PLUS], ['+']],
['#', [t.POUND], ['#']],
['/', [t.SLASH], ['/']],
Expand Down
Loading