diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 780029d6c..5ec262c5c 100755 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4525,20 +4525,50 @@ return range; }, makeLink: function(range) { - var a, check, text; + var a, char, len, text, toggle, trim; text = range.toString(); - if (/[(\[{<]/.test(text.charAt(0))) { - if (check = /[\)\]}>]/.test(text.charAt(text.length - 1))) { - if (!(range.endOffset < 1)) { - range.setEnd(range.endContainer, range.endOffset - 1); - } + trim = function() { + if (!(range.endOffset < 1)) { + range.setEnd(range.endContainer, range.endOffset - 1); } - text = check ? text.slice(1, -1) : text.slice(1); + return text = text.slice(0, -1); + }; + if (/[(\[{<]/.test(text.charAt(0))) { + if (/[\)\]}>]/.test(text.charAt(text.length - 1))) { + trim(); + } + 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 === ',') { + trim(); + continue; + } + len = text.length; + while (i < len) { + toggle = false; + switch (text[i++]) { + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case '<': + case '>': + toggle = !toggle; + } + } + if (toggle) { + trim(); + continue; + } + break; + } text = text.contains(':') ? text : (text.contains('@') ? 'mailto:' : 'http://') + text; a = $.el('a', { className: 'linkify', diff --git a/builds/crx/script.js b/builds/crx/script.js index 60ec0a5d2..f808b5d67 100755 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4531,20 +4531,50 @@ return range; }, makeLink: function(range) { - var a, check, text; + var a, char, len, text, toggle, trim; text = range.toString(); - if (/[(\[{<]/.test(text.charAt(0))) { - if (check = /[\)\]}>]/.test(text.charAt(text.length - 1))) { - if (!(range.endOffset < 1)) { - range.setEnd(range.endContainer, range.endOffset - 1); - } + trim = function() { + if (!(range.endOffset < 1)) { + range.setEnd(range.endContainer, range.endOffset - 1); } - text = check ? text.slice(1, -1) : text.slice(1); + return text = text.slice(0, -1); + }; + if (/[(\[{<]/.test(text.charAt(0))) { + if (/[\)\]}>]/.test(text.charAt(text.length - 1))) { + trim(); + } + 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 === ',') { + trim(); + continue; + } + len = text.length; + while (i < len) { + toggle = false; + switch (text[i++]) { + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case '<': + case '>': + toggle = !toggle; + } + } + if (toggle) { + trim(); + continue; + } + break; + } text = text.contains(':') ? text : (text.contains('@') ? 'mailto:' : 'http://') + text; a = $.el('a', { className: 'linkify', diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index c2819ac0e..6be4c648b 100755 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -105,18 +105,31 @@ Linkify = makeLink: (range) -> text = range.toString() + + trim = -> + range.setEnd range.endContainer, range.endOffset - 1 unless range.endOffset < 1 + text = text.slice 0, -1 - # Clean brackets + # Clean brackets, hanging commas if /[(\[{<]/.test text.charAt 0 - if check = /[\)\]}>]/.test text.charAt text.length - 1 - unless range.endOffset < 1 - range.setEnd range.endContainer, range.endOffset - 1 - text = if check - text.slice 1, -1 - else - text.slice 1 + trim() if /[\)\]}>]/.test text.charAt text.length - 1 + 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 ',' + trim() + continue + len = text.length + while i < len + toggle = false + switch text[i++] + when '(', ')', '[', ']', '{', '}', '<', '>' then toggle = !toggle + if toggle + trim() + continue + break text = if text.contains ':'