From da2da410d527916db63df965d2f1e07626ce0233 Mon Sep 17 00:00:00 2001 From: Nick Frasser <1693461+nfrasser@users.noreply.github.com> Date: Wed, 4 Dec 2024 00:16:44 -0500 Subject: [PATCH] Interpret \r\n as newline character (#500) Instead of whitespace + newline --- packages/linkifyjs/src/scanner.mjs | 12 +++++++++--- test/spec/linkifyjs/scanner.test.mjs | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/linkifyjs/src/scanner.mjs b/packages/linkifyjs/src/scanner.mjs index 406df67..687ecd8 100644 --- a/packages/linkifyjs/src/scanner.mjs +++ b/packages/linkifyjs/src/scanner.mjs @@ -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 @@ -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); diff --git a/test/spec/linkifyjs/scanner.test.mjs b/test/spec/linkifyjs/scanner.test.mjs index f58f764..ef92786 100644 --- a/test/spec/linkifyjs/scanner.test.mjs +++ b/test/spec/linkifyjs/scanner.test.mjs @@ -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], ['/']],