Skip to content

Commit

Permalink
Interpret \r\n as newline character (#500)
Browse files Browse the repository at this point in the history
Instead of whitespace + newline
  • Loading branch information
nfrasser authored Dec 4, 2024
1 parent a7629c2 commit da2da41
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit da2da41

Please sign in to comment.