Skip to content

Commit da2da41

Browse files
authored
Interpret \r\n as newline character (#500)
Instead of whitespace + newline
1 parent a7629c2 commit da2da41

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/linkifyjs/src/scanner.mjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import * as tk from './text.mjs';
1010
import * as re from './regexp.mjs';
1111
import assign from './assign.mjs';
1212

13-
const NL = '\n'; // New line character
13+
const CR = '\r'; // carriage-return character
14+
const LF = '\n'; // line-feed character
1415
const EMOJI_VARIATION = '\ufe0f'; // Variation selector, follows heart and others
1516
const EMOJI_JOINER = '\u200d'; // zero-width joiner
1617
const OBJECT_REPLACEMENT = '\ufffc'; // whitespace placeholder that sometimes appears in rich text editors
@@ -121,10 +122,15 @@ export function init(customSchemes = []) {
121122
// Whitespace jumps
122123
// Tokens of only non-newline whitespace are arbitrarily long
123124
// If any whitespace except newline, more whitespace!
125+
const Nl = tt(Start, LF, tk.NL, { [fsm.whitespace]: true });
126+
const Cr = tt(Start, CR, tk.WS, { [fsm.whitespace]: true });
124127
const Ws = tr(Start, re.SPACE, tk.WS, { [fsm.whitespace]: true });
125128
tt(Start, OBJECT_REPLACEMENT, Ws);
126-
tt(Start, NL, tk.NL, { [fsm.whitespace]: true });
127-
tt(Ws, NL); // non-accepting state to avoid mixing whitespaces
129+
tt(Cr, LF, Nl); // \r\n
130+
tt(Cr, OBJECT_REPLACEMENT, Ws);
131+
tr(Cr, re.SPACE, Ws);
132+
tt(Ws, CR); // non-accepting state to avoid mixing whitespaces
133+
tt(Ws, LF); // non-accepting state to avoid mixing whitespaces
128134
tr(Ws, re.SPACE, Ws);
129135
tt(Ws, OBJECT_REPLACEMENT, Ws);
130136

test/spec/linkifyjs/scanner.test.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const tests = [
1313
['.', [t.DOT], ['.']],
1414
['-', [t.HYPHEN], ['-']],
1515
['\n', [t.NL], ['\n']],
16+
['\r\n', [t.NL], ['\r\n']],
17+
[' \r\n', [t.WS, t.NL], [' ', '\r\n']],
18+
['\r\n ', [t.NL, t.WS], ['\r\n', ' ']],
19+
['\r \n', [t.WS, t.NL], ['\r ', '\n']],
1620
['+', [t.PLUS], ['+']],
1721
['#', [t.POUND], ['#']],
1822
['/', [t.SLASH], ['/']],

0 commit comments

Comments
 (0)