From 68e19a0b2ee2cd134889676b559b4116f66db464 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 13 Aug 2013 19:20:10 -0700 Subject: [PATCH 1/4] better trim --- builds/4chan-X.user.js | 28 ++++++++-------------------- builds/crx/script.js | 28 ++++++++-------------------- src/Linkification/Linkify.coffee | 18 ++++++++---------- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index aeae57d34..659f6ceab 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4525,7 +4525,7 @@ return range; }, makeLink: function(range) { - var a, char, len, text, toggle, trim; + var a, char, i, len, text, toggle, trim; text = range.toString(); trim = function() { @@ -4535,37 +4535,25 @@ return text = text.slice(0, -1); }; if (/[(\[{<]/.test(text.charAt(0))) { - if (/[\)\]}>]/.test(text.charAt(text.length - 1))) { - trim(); - } - text.slice(1); + text = text.slice(1); if (range.startOffset !== range.startContainer.data.length) { range.setStart(range.startContainer, range.startOffset + 1); } } - while (/[\)\]}>,]/.exec(char = text.charAt(text.length - 1))) { - if (char === ',') { + while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { + if (/[.,]/.test(char)) { trim(); continue; } - len = text.length; + i = 0; + toggle = false; while (i < len) { - toggle = false; - switch (text[i++]) { - case '(': - case ')': - case '[': - case ']': - case '{': - case '}': - case '<': - case '>': - toggle = !toggle; + if (/[()\[\]{}<>]/.test(text[i++])) { + toggle = !toggle; } } if (toggle) { trim(); - continue; } break; } diff --git a/builds/crx/script.js b/builds/crx/script.js index 84ca2204d..d31066840 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4531,7 +4531,7 @@ return range; }, makeLink: function(range) { - var a, char, len, text, toggle, trim; + var a, char, i, len, text, toggle, trim; text = range.toString(); trim = function() { @@ -4541,37 +4541,25 @@ return text = text.slice(0, -1); }; if (/[(\[{<]/.test(text.charAt(0))) { - if (/[\)\]}>]/.test(text.charAt(text.length - 1))) { - trim(); - } - text.slice(1); + text = text.slice(1); if (range.startOffset !== range.startContainer.data.length) { range.setStart(range.startContainer, range.startOffset + 1); } } - while (/[\)\]}>,]/.exec(char = text.charAt(text.length - 1))) { - if (char === ',') { + while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { + if (/[.,]/.test(char)) { trim(); continue; } - len = text.length; + i = 0; + toggle = false; while (i < len) { - toggle = false; - switch (text[i++]) { - case '(': - case ')': - case '[': - case ']': - case '{': - case '}': - case '<': - case '>': - toggle = !toggle; + if (/[()\[\]{}<>]/.test(text[i++])) { + toggle = !toggle; } } if (toggle) { trim(); - continue; } break; } diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index 6be4c648b..0f935cea9 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -110,25 +110,23 @@ Linkify = range.setEnd range.endContainer, range.endOffset - 1 unless range.endOffset < 1 text = text.slice 0, -1 - # Clean brackets, hanging commas + # Clean leading brackets if /[(\[{<]/.test text.charAt 0 - trim() if /[\)\]}>]/.test text.charAt text.length - 1 - text.slice 1 + text = text.slice 1 unless range.startOffset is range.startContainer.data.length range.setStart range.startContainer, range.startOffset + 1 - while /[\)\]}>,]/.exec char = text.charAt text.length - 1 - if char is ',' + # Clean hanging brackets, commas, periods + while /[)\]}>.,]/.test char = text.charAt (len = text.length) - 1 + if /[.,]/.test char trim() continue - len = text.length + i = 0 + toggle = false while i < len - toggle = false - switch text[i++] - when '(', ')', '[', ']', '{', '}', '<', '>' then toggle = !toggle + if /[()\[\]{}<>]/.test text[i++] then toggle = !toggle if toggle trim() - continue break text = From fb8a856fbab5e82324f5fb345a297ba2c2f5951f Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 13 Aug 2013 19:24:13 -0700 Subject: [PATCH 2/4] Come on, I'm smarter than this --- builds/4chan-X.user.js | 14 ++------------ builds/crx/script.js | 14 ++------------ src/Linkification/Linkify.coffee | 8 +------- 3 files changed, 5 insertions(+), 31 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 659f6ceab..9916f84e2 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4525,7 +4525,7 @@ return range; }, makeLink: function(range) { - var a, char, i, len, text, toggle, trim; + var a, char, len, text, trim; text = range.toString(); trim = function() { @@ -4541,20 +4541,10 @@ } } while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { - if (/[.,]/.test(char)) { + if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/)).length % 2) { trim(); continue; } - i = 0; - toggle = false; - while (i < len) { - if (/[()\[\]{}<>]/.test(text[i++])) { - toggle = !toggle; - } - } - if (toggle) { - trim(); - } break; } text = text.contains(':') ? text : (text.contains('@') ? 'mailto:' : 'http://') + text; diff --git a/builds/crx/script.js b/builds/crx/script.js index d31066840..1b4513959 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4531,7 +4531,7 @@ return range; }, makeLink: function(range) { - var a, char, i, len, text, toggle, trim; + var a, char, len, text, trim; text = range.toString(); trim = function() { @@ -4547,20 +4547,10 @@ } } while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { - if (/[.,]/.test(char)) { + if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/)).length % 2) { trim(); continue; } - i = 0; - toggle = false; - while (i < len) { - if (/[()\[\]{}<>]/.test(text[i++])) { - toggle = !toggle; - } - } - if (toggle) { - trim(); - } break; } text = text.contains(':') ? text : (text.contains('@') ? 'mailto:' : 'http://') + text; diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index 0f935cea9..ea7c3c902 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -118,15 +118,9 @@ Linkify = # Clean hanging brackets, commas, periods while /[)\]}>.,]/.test char = text.charAt (len = text.length) - 1 - if /[.,]/.test char + if /[.,]/.test(char) or (text.match /[()\[\]{}<>]/).length % 2 trim() continue - i = 0 - toggle = false - while i < len - if /[()\[\]{}<>]/.test text[i++] then toggle = !toggle - if toggle - trim() break text = From b424adb93b7d4638e0c7222d1c9cfb60af635556 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 13 Aug 2013 19:26:43 -0700 Subject: [PATCH 3/4] Forgot to make regex global --- builds/4chan-X.user.js | 2 +- builds/crx/script.js | 2 +- src/Linkification/Linkify.coffee | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 9916f84e2..bef0d95a8 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4541,7 +4541,7 @@ } } while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { - if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/)).length % 2) { + if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/g)).length % 2) { trim(); continue; } diff --git a/builds/crx/script.js b/builds/crx/script.js index 1b4513959..910049a46 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4547,7 +4547,7 @@ } } while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { - if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/)).length % 2) { + if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/g)).length % 2) { trim(); continue; } diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index ea7c3c902..a5fa64f5c 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -118,7 +118,7 @@ Linkify = # Clean hanging brackets, commas, periods while /[)\]}>.,]/.test char = text.charAt (len = text.length) - 1 - if /[.,]/.test(char) or (text.match /[()\[\]{}<>]/).length % 2 + if /[.,]/.test(char) or (text.match /[()\[\]{}<>]/g).length % 2 trim() continue break From 67636c5b8911852b7d8abeb5e91805801086c093 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 13 Aug 2013 20:05:25 -0700 Subject: [PATCH 4/4] Small trim --- builds/4chan-X.user.js | 4 ++-- builds/crx/script.js | 4 ++-- src/Linkification/Linkify.coffee | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index bef0d95a8..541cb2a49 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4525,7 +4525,7 @@ return range; }, makeLink: function(range) { - var a, char, len, text, trim; + var a, char, text, trim; text = range.toString(); trim = function() { @@ -4540,7 +4540,7 @@ range.setStart(range.startContainer, range.startOffset + 1); } } - while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { + while (/[)\]}>.,]/.test(char = text.charAt(text.length - 1))) { if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/g)).length % 2) { trim(); continue; diff --git a/builds/crx/script.js b/builds/crx/script.js index 910049a46..c8a4bfa47 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4531,7 +4531,7 @@ return range; }, makeLink: function(range) { - var a, char, len, text, trim; + var a, char, text, trim; text = range.toString(); trim = function() { @@ -4546,7 +4546,7 @@ range.setStart(range.startContainer, range.startOffset + 1); } } - while (/[)\]}>.,]/.test(char = text.charAt((len = text.length) - 1))) { + while (/[)\]}>.,]/.test(char = text.charAt(text.length - 1))) { if (/[.,]/.test(char) || (text.match(/[()\[\]{}<>]/g)).length % 2) { trim(); continue; diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index a5fa64f5c..5593e6bb5 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -117,12 +117,13 @@ Linkify = range.setStart range.startContainer, range.startOffset + 1 # Clean hanging brackets, commas, periods - while /[)\]}>.,]/.test char = text.charAt (len = text.length) - 1 + while /[)\]}>.,]/.test char = text.charAt text.length - 1 if /[.,]/.test(char) or (text.match /[()\[\]{}<>]/g).length % 2 trim() continue break + # This is the only piece of code left based on Anthony Lieuallen's Linkify text = if text.contains ':' text