Skip to content

Commit

Permalink
Optimize regexp for edge whitespace matching. Fix #429.
Browse files Browse the repository at this point in the history
  • Loading branch information
martincizek committed Mar 22, 2023
1 parent 6c956bd commit 77078bd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 2 additions & 0 deletions test/internals-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 77078bd

Please sign in to comment.