From 2ea8441fd4a9b816240eea994b84700cc14e59e0 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 13 Aug 2013 04:25:32 -0700 Subject: [PATCH] Fix a few small issues, new Linkifier regex --- LICENSE | 2 +- builds/4chan-X.user.js | 22 +++++++++--------- builds/crx/script.js | 22 +++++++++--------- src/General/Config.coffee | 4 ---- src/Linkification/Linkify.coffee | 40 +++++++++++++++++++------------- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/LICENSE b/LICENSE index 468617e9f..a2ce0f756 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ /* -* 4chan X - Version 1.2.25 - 2013-08-12 +* 4chan X - Version 1.2.25 - 2013-08-13 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index e79507a81..bbddd4f10 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -19,7 +19,7 @@ // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAgMAAAAqbBEUAAAACVBMVEUAAGcAAABmzDNZt9VtAAAAAXRSTlMAQObYZgAAAHFJREFUKFOt0LENACEIBdBv4Qju4wgWanEj3D6OcIVMKaitYHEU/jwTCQj8W75kiVCSBvdQ5/AvfVHBin11BgdRq3ysBgfwBDRrj3MCIA+oAQaku/Q1cNctrAmyDl577tOThYt/Y1RBM4DgOHzM0HFTAyLukH/cmRnqAAAAAElFTkSuQmCC // ==/UserScript== /* -* 4chan X - Version 1.2.25 - 2013-08-12 +* 4chan X - Version 1.2.25 - 2013-08-13 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE @@ -142,7 +142,6 @@ }, 'Linkification': { 'Linkify': [true, 'Convert text into links where applicable.'], - 'Allow False Positives': [false, 'Linkify everything, allowing more false positives but reducing missed links'], 'Embedding': [true, 'Embed supported services.'], 'Auto-embed': [false, 'Auto-embed Linkify Embeds.'], 'Link Title': [true, 'Replace the link of a supported site with its actual title. Currently Supported: YouTube, Vimeo, SoundCloud, and Github gists'] @@ -4426,7 +4425,7 @@ if (g.VIEW === 'catalog' || !Conf['Linkify']) { return; } - this.regString = Conf['Allow False Positives'] ? /([-a-z]+:\/\/|[a-z]{3,}\.[-a-z0-9]+\.[a-z]|[-a-z0-9]+\.[a-z]|[\d]+\.[\d]+\.[\d]+\.[\d]+\/|[a-z]{3,}:[a-z0-9?]|[^\s@]+@[a-z0-9.-]+\.[a-z0-9])/i : /(((magnet|mailto)\:|(www\.)|(news|(ht|f)tp(s?))\:\/\/){1})/i; + this.regString = /(?:[a-z][-\w]+:([a-z\d%\/])|www\d{0,3}[.]|[-a-z\d.]+[.](com|net|org|jp|uk|ru|be|tv|xxx|edu|gov|cd|es|de|se|tk|dk|io|fm|fi)|[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}|[-\w\d.@]+@[a-z\d.-]+\.[a-z\d])/i; if (Conf['Comment Expansion']) { ExpandComment.callbacks.push(this.node); } @@ -4458,8 +4457,8 @@ space = /[\s'"]/; snapshot = $.X('.//br|.//text()', this.nodes.comment); i = 0; + links = []; while (node = snapshot.snapshotItem(i++)) { - links = []; data = node.data; if (node.parentElement.nodeName === "A" || !data) { continue; @@ -4475,7 +4474,7 @@ endNode = saved; length = saved.data.length; if (end = space.exec(saved.data)) { - length = end.index; + test.lastIndex = length = end.index; i--; break; } @@ -4487,22 +4486,23 @@ if (link = Linkify.regString.exec(text = range.toString())) { if (lIndex = link.index) { range.setStart(node, lIndex + index); + text = text.slice(0, lIndex); } links.push([range, text]); } break; } else { if (link = Linkify.regString.exec(result[0])) { - range = Linkify.makeRange(node, node, link.index, link.length); + range = Linkify.makeRange(node, node, index + link.index, length + link.index); links.push([range, link]); } } } - _ref = links.reverse(); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - range = _ref[_i]; - this.nodes.links.push(Linkify.makeLink(range, this)); - } + } + _ref = links.reverse(); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + range = _ref[_i]; + this.nodes.links.push(Linkify.makeLink(range, this)); } if (!(Conf['Embedding'] || Conf['Link Title'])) { return; diff --git a/builds/crx/script.js b/builds/crx/script.js index 4772e8adc..1f5f1120d 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript /* -* 4chan X - Version 1.2.25 - 2013-08-12 +* 4chan X - Version 1.2.25 - 2013-08-13 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE @@ -124,7 +124,6 @@ }, 'Linkification': { 'Linkify': [true, 'Convert text into links where applicable.'], - 'Allow False Positives': [false, 'Linkify everything, allowing more false positives but reducing missed links'], 'Embedding': [true, 'Embed supported services.'], 'Auto-embed': [false, 'Auto-embed Linkify Embeds.'], 'Link Title': [true, 'Replace the link of a supported site with its actual title. Currently Supported: YouTube, Vimeo, SoundCloud, and Github gists'] @@ -4432,7 +4431,7 @@ if (g.VIEW === 'catalog' || !Conf['Linkify']) { return; } - this.regString = Conf['Allow False Positives'] ? /([-a-z]+:\/\/|[a-z]{3,}\.[-a-z0-9]+\.[a-z]|[-a-z0-9]+\.[a-z]|[\d]+\.[\d]+\.[\d]+\.[\d]+\/|[a-z]{3,}:[a-z0-9?]|[^\s@]+@[a-z0-9.-]+\.[a-z0-9])/i : /(((magnet|mailto)\:|(www\.)|(news|(ht|f)tp(s?))\:\/\/){1})/i; + this.regString = /(?:[a-z][-\w]+:([a-z\d%\/])|www\d{0,3}[.]|[-a-z\d.]+[.](com|net|org|jp|uk|ru|be|tv|xxx|edu|gov|cd|es|de|se|tk|dk|io|fm|fi)|[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}|[-\w\d.@]+@[a-z\d.-]+\.[a-z\d])/i; if (Conf['Comment Expansion']) { ExpandComment.callbacks.push(this.node); } @@ -4464,8 +4463,8 @@ space = /[\s'"]/; snapshot = $.X('.//br|.//text()', this.nodes.comment); i = 0; + links = []; while (node = snapshot.snapshotItem(i++)) { - links = []; data = node.data; if (node.parentElement.nodeName === "A" || !data) { continue; @@ -4481,7 +4480,7 @@ endNode = saved; length = saved.data.length; if (end = space.exec(saved.data)) { - length = end.index; + test.lastIndex = length = end.index; i--; break; } @@ -4493,22 +4492,23 @@ if (link = Linkify.regString.exec(text = range.toString())) { if (lIndex = link.index) { range.setStart(node, lIndex + index); + text = text.slice(0, lIndex); } links.push([range, text]); } break; } else { if (link = Linkify.regString.exec(result[0])) { - range = Linkify.makeRange(node, node, link.index, link.length); + range = Linkify.makeRange(node, node, index + link.index, length + link.index); links.push([range, link]); } } } - _ref = links.reverse(); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - range = _ref[_i]; - this.nodes.links.push(Linkify.makeLink(range, this)); - } + } + _ref = links.reverse(); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + range = _ref[_i]; + this.nodes.links.push(Linkify.makeLink(range, this)); } if (!(Conf['Embedding'] || Conf['Link Title'])) { return; diff --git a/src/General/Config.coffee b/src/General/Config.coffee index 83c2b3df9..f1e11d05c 100644 --- a/src/General/Config.coffee +++ b/src/General/Config.coffee @@ -83,10 +83,6 @@ Config = true 'Convert text into links where applicable.' ] - 'Allow False Positives': [ - false - 'Linkify everything, allowing more false positives but reducing missed links' - ] 'Embedding': [ true 'Embed supported services.' diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index ef1684dce..35d722b47 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -2,22 +2,27 @@ Linkify = init: -> return if g.VIEW is 'catalog' or not Conf['Linkify'] - @regString = if Conf['Allow False Positives'] + @regString = ///( - [-a-z]+:// + # http, magnet, ftp, etc + ?:[a-z][-\w]+:( + [a-z\d%/] + ) | - [a-z]{3,}\.[-a-z0-9]+\.[a-z] + www\d{0,3}[.] | - [-a-z0-9]+\.[a-z] + # This should account for virtually all links posted without www or http: + # If it misses any, screw it. No, I will not add canv.as + [-a-z\d.]+[.]( + com|net|org|jp|uk|ru|be|tv|xxx|edu|gov|cd|es|de|se|tk|dk|io|fm|fi + ) | - [\d]+\.[\d]+\.[\d]+\.[\d]+/ + # IPv4 Addresses + [\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3} | - [a-z]{3,}:[a-z0-9?] - | - [^\s@]+@[a-z0-9.-]+\.[a-z0-9] + # E-mails + [-\w\d.@]+@[a-z\d.-]+\.[a-z\d] )///i - else - /(((magnet|mailto)\:|(www\.)|(news|(ht|f)tp(s?))\:\/\/){1})/i if Conf['Comment Expansion'] ExpandComment.callbacks.push @node @@ -45,14 +50,15 @@ Linkify = snapshot = $.X './/br|.//text()', @nodes.comment i = 0 + links = [] while node = snapshot.snapshotItem i++ - links = [] {data} = node continue if node.parentElement.nodeName is "A" or not data while result = test.exec data {index} = result endNode = node + # End of node, not necessarily end of space-delimited string if (length = index + result[0].length) is data.length while (saved = snapshot.snapshotItem i++) @@ -62,25 +68,27 @@ Linkify = {length} = saved.data if end = space.exec saved.data - length = end.index + # Set our snapshot and regex to start on this node at this position when the loop resumes + test.lastIndex = length = end.index i-- break - if length is endNode.data.length then test.lastIndex = 0 + test.lastIndex = 0 if length is endNode.data.length range = Linkify.makeRange node, endNode, index, length if link = Linkify.regString.exec text = range.toString() if lIndex = link.index range.setStart node, lIndex + index + text = text[...lIndex] links.push [range, text] break else if link = Linkify.regString.exec result[0] - range = Linkify.makeRange node, node, link.index, link.length + range = Linkify.makeRange node, node, index + link.index, length + link.index links.push [range, link] - for range in links.reverse() - @nodes.links.push Linkify.makeLink range, @ + for range in links.reverse() + @nodes.links.push Linkify.makeLink range, @ return unless Conf['Embedding'] or Conf['Link Title']