Skip to content

Commit 175fae6

Browse files
committed
Improve worst-case performance of inline.text regex
The old regex may take quadratic time to scan for potential email addresses starting at every point. Fix it to avoid scanning from points that would have been in the middle of a previous scan. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent ba1de1e commit 175fae6

File tree

2 files changed

+2
-6
lines changed

2 files changed

+2
-6
lines changed

lib/marked.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,7 @@ inline.gfm = merge({}, inline.normal, {
615615
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
616616
_backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
617617
del: /^~+(?=\S)([\s\S]*?\S)~+/,
618-
text: edit(inline.text)
619-
.replace(']|', '~]|')
620-
.replace('|$', '|https?://|ftp://|www\\.|[a-zA-Z0-9.!#$%&\'*+/=?^_`{\\|}~-]+@|$')
621-
.getRegex()
618+
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_| {2,}\n|https?:\/\/|ftp:\/\/|www\.|$)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
622619
});
623620

624621
inline.gfm.url = edit(inline.gfm.url, 'i')

test/specs/gfm/gfm.0.28.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@
141141
"section": "Autolinks",
142142
"html": "<p><a href=\"mailto:[email protected]\">[email protected]</a></p>\n<p><a href=\"mailto:[email protected]\">[email protected]</a>.</p>\n<p>[email protected]</p>\n<p>[email protected]_</p>",
143143
144-
"example": 607,
145-
"shouldFail": true
144+
"example": 607
146145
},
147146
{
148147
"section": "Disallowed Raw HTML",

0 commit comments

Comments
 (0)