Skip to content
Browse files

Repair regex of mention and hashtag functions

Unquote the regex strings inside the `mention` and `hashtag` functions.
Add test cases to test results of said functions. Remove superfluous
A-Z charcter classes from regex as case-insensitive modifier is used.

To ensure the tested tweet contains a mention and a hashtag, I also
found it necessary to temporarily replace the `$.twitter` function
with a mocked version that returns a pre-defined result. I have never
used QUnit before, so I am not sure if there is a better way to go about
this. It seems to work as intended. The tests originally failed. After
unquoting the regex strings, the tests passed.
  • Loading branch information...
1 parent e4486b0 commit 0b5f223471b550be01646f8f66bea16b1aa7fae8 @rexmac rexmac committed Feb 23, 2012
Showing with 27 additions and 2 deletions.
  1. +2 −2 src/jquery.twitter.js
  2. +3 −0 test/jquery.twitter.html
  3. +22 −0 test/jquery.twitter_test.js
View
4 src/jquery.twitter.js
@@ -13,12 +13,12 @@ var linkify = linkify || function() {};
var
mention = function( str ) {
- return str.replace("/[@]+[A-Za-z0-9-_]+/ig", function( username ) {
+ return str.replace(/[@]+[a-z0-9-_]+/ig, function( username ) {
return username.link("http://twitter.com/"+ username.replace("@","") );
});
},
hashtags = function( str ) {
- return str.replace("/[#]+[A-Za-z0-9-_]+/ig", function( tag ) {
+ return str.replace(/[#]+[a-z0-9-_]+/ig, function( tag ) {
return tag.link("http://search.twitter.com/search?q="+tag.replace("#","%23"));
});
};
View
3 test/jquery.twitter.html
@@ -36,6 +36,9 @@ <h2 id="qunit-userAgent"></h2>
<hr class="space">
<h5>#testlist5</h5>
<div id="testlist5" ></div>
+ <hr class="space">
+ <h5>#testlist6</h5>
+ <div id="testlist6" ></div>
</div>
</div>
</body>
View
22 test/jquery.twitter_test.js
@@ -36,6 +36,24 @@
avatar : false
});
+ // Temporarily replace $.twitter with a mock version that returns a pre-defined result
+ $.oTwitter = $.twitter;
+ $.twitter = function( options, callback ) {
+ var tweets = {
+ "results" : [{
+ "from_user": "mediatemple",
+ "text": "This text contains a @mention and a #hashtag"
+ }]
+ };
+ callback( tweets, {}, null );
+ };
+
+ $("#testlist6").twitter({ from: "mediatemple" });
+
+ // Replace original $.twitter function
+ $.twitter = $.oTwitter;
+ delete $.oTwitter;
+
module("$.twitter()");
test("Test the async", function() {
@@ -121,6 +139,10 @@
ok($("#testlist1").children().find("span").length, "Make sure the tweet is there" );
+ ok( $("#testlist6").children().find('a[href="http://twitter.com/mention"]').length, "Make sure @mentions are linked" );
+
+ ok( $("#testlist6").children().find('a[href="http://search.twitter.com/search?q=%23hashtag"]').length, "Make sure #hashtags are linked" );
+
});
test("Test a few of the cases for the object style signature", function() {

0 comments on commit 0b5f223

Please sign in to comment.
Something went wrong with that request. Please try again.