Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiency: Compile RegEx once #1230

Merged
merged 2 commits into from
Oct 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/js/extensions/auto-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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');
}
Expand Down Expand Up @@ -207,12 +210,11 @@
},

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert that change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, just a sec.

matchEnd = match.index + match[0].length;
// If the regexp detected something as a link that has text immediately preceding/following it, bail out.
Expand Down