From 77078bd8fd1afce81762d6c70e6f701b86039301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C4=8C=C3=AD=C5=BEek?= Date: Wed, 22 Mar 2023 12:23:53 +0100 Subject: [PATCH] Optimize regexp for edge whitespace matching. Fix #429. --- src/node.js | 2 +- test/internals-test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node.js b/src/node.js index 9a2a1f19..be8d308f 100644 --- a/src/node.js +++ b/src/node.js @@ -39,7 +39,7 @@ function flankingWhitespace (node, options) { } function edgeWhitespace (string) { - var m = string.match(/^(([ \t\r\n]*)(\s*))[\s\S]*?((\s*?)([ \t\r\n]*))$/) + var m = string.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/) return { leading: m[1], // whole string for whitespace-only strings leadingAscii: m[2], diff --git a/test/internals-test.js b/test/internals-test.js index 31d4a076..ae2a526a 100644 --- a/test/internals-test.js +++ b/test/internals-test.js @@ -24,8 +24,10 @@ test('edge whitespace detection',function (t) { [`${WS}\xa0`, ews(WS, `\xa0`, '', '')], [`HELLO WORLD`, ews('', '', '', '')], [``, ews('', '', '', '')], + [`TEST${Array(32768).join(' ')}END`, ews('', '', '', '')], // performance check ] t.plan(TEST_CASES.length) + t.timeoutAfter(300) var edgeWhitespace = turndownModule.__get__('edgeWhitespace') TEST_CASES.forEach(function (c) { t.deepEqual(edgeWhitespace(c[0]), c[1])