From f1d4a3acab521feb77c05dfc7c9d8f0506f61fc5 Mon Sep 17 00:00:00 2001 From: xerik Date: Mon, 10 Oct 2016 19:02:57 +0200 Subject: [PATCH 1/2] Efficiency: Compile RegEx once --- src/js/extensions/auto-link.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/js/extensions/auto-link.js b/src/js/extensions/auto-link.js index 708a9de23..8a4b32bb8 100644 --- a/src/js/extensions/auto-link.js +++ b/src/js/extensions/auto-link.js @@ -4,7 +4,8 @@ var WHITESPACE_CHARS, KNOWN_TLDS_FRAGMENT, LINK_REGEXP_TEXT, - KNOWN_TLDS_REGEXP; + KNOWN_TLDS_REGEXP, + LINK_REGEXP; WHITESPACE_CHARS = [' ', '\t', '\n', '\r', '\u00A0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2028', '\u2029']; @@ -26,6 +27,8 @@ KNOWN_TLDS_REGEXP = new RegExp('^(' + KNOWN_TLDS_FRAGMENT + ')$', 'i'); + LINK_REGEXP = new RegExp(LINK_REGEXP_TEXT, 'gi'); + function nodeIsNotInsideAnchorTag(node) { return !MediumEditor.util.getClosestTag(node, 'a'); } @@ -207,15 +210,14 @@ }, findLinkableText: function (contenteditable) { - var linkRegExp = new RegExp(LINK_REGEXP_TEXT, 'gi'), - textContent = contenteditable.textContent, + var textContent = contenteditable.textContent, match = null, matches = []; - while ((match = linkRegExp.exec(textContent)) !== null) { + while ((match = LINK_REGEXP.exec(textContent)) !== null) { var matchOk = true, matchEnd = match.index + match[0].length; - // If the regexp detected something as a link that has text immediately preceding/following it, bail out. + // If the regexp detec^ted something as a link that has text immediately preceding/following it, bail out. matchOk = (match.index === 0 || WHITESPACE_CHARS.indexOf(textContent[match.index - 1]) !== -1) && (matchEnd === textContent.length || WHITESPACE_CHARS.indexOf(textContent[matchEnd]) !== -1); // If the regexp detected a bare domain that doesn't use one of our expected TLDs, bail out. From b9797082b4e3a9278f945c6cc14e914b7cc9f691 Mon Sep 17 00:00:00 2001 From: xerik Date: Mon, 10 Oct 2016 19:14:00 +0200 Subject: [PATCH 2/2] Changed: Added comment I removed accidentally --- src/js/extensions/auto-link.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/extensions/auto-link.js b/src/js/extensions/auto-link.js index 8a4b32bb8..1f2e8f316 100644 --- a/src/js/extensions/auto-link.js +++ b/src/js/extensions/auto-link.js @@ -217,7 +217,7 @@ while ((match = LINK_REGEXP.exec(textContent)) !== null) { var matchOk = true, matchEnd = match.index + match[0].length; - // If the regexp detec^ted something as a link that has text immediately preceding/following it, bail out. + // If the regexp detected something as a link that has text immediately preceding/following it, bail out. matchOk = (match.index === 0 || WHITESPACE_CHARS.indexOf(textContent[match.index - 1]) !== -1) && (matchEnd === textContent.length || WHITESPACE_CHARS.indexOf(textContent[matchEnd]) !== -1); // If the regexp detected a bare domain that doesn't use one of our expected TLDs, bail out.