fixed the formatTweet to create links to urls. popcorn-port
authorhamidouk
Mon, 21 Nov 2011 16:20:16 +0100
branchpopcorn-port
changeset 297 ed4a459c9290
parent 296 10ecc65d945c
child 298 eccdc619ede3
fixed the formatTweet to create links to urls.
src/js/utils.js
unittests/tests/utils.js
--- a/src/js/utils.js	Mon Nov 21 15:45:47 2011 +0100
+++ b/src/js/utils.js	Mon Nov 21 16:20:16 2011 +0100
@@ -51,12 +51,15 @@
 
 /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */
 IriSP.formatTweet = function(tweet) {
-  var rNickname = /@(\w+)/; // matches a @handle
-  var rHashtag = /#(\w+)/;  // matches a hashtag
-  var i1 = tweet.replace(rNickname, "<a href='http://twitter.com/$1'>@$1</a>");
-  var i2 = i1.replace(rHashtag, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>");
+  var rNickname = /@(\w+)/gi; // matches a @handle
+  var rHashtag = /#(\w+)/gi;  // matches a hashtag
+  var rUrl = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi; // copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480
   
-  return i2;
+  var i1 = tweet.replace(rUrl, "<a href='$1'>$1</a>");
+  var i2 = i1.replace(rNickname, "<a href='http://twitter.com/$1'>@$1</a>");
+  var i3 = i2.replace(rHashtag, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>");
+
+  return i3;
 };
 
 /* for ie compatibility
--- a/unittests/tests/utils.js	Mon Nov 21 15:45:47 2011 +0100
+++ b/unittests/tests/utils.js	Mon Nov 21 16:20:16 2011 +0100
@@ -39,8 +39,8 @@
   });
   
   test("test function to format a tweet", function() {
-    var input = "@handle #hashtag ";
-    var output = "<a href='http://twitter.com/handle'>@handle</a> <a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a> ";
+    var input = "@handle @bundle #hashtag http://t.co/11111";
+    var output = "<a href='http://twitter.com/handle'>@handle</a> <a href='http://twitter.com/bundle'>@bundle</a> <a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a> <a href='http://t.co/11111'>http://t.co/11111</a>";
     equal(IriSP.formatTweet(input), output, "the correct output is given");
   });
 }
\ No newline at end of file