Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#388 Added tests and support for url's to contain parentheses, unicod…

…e, and http in all caps
  • Loading branch information...
commit a6454bfd3165ffe317ceb91dcf970deacc64be1b 1 parent 2904ea0
@admsteck authored
View
42 JabbR.Test/TextTransformFacts.cs
@@ -133,6 +133,20 @@ public void UrlWithHttpIsTransformed()
}
[Fact]
+ public void UrlWithCapitalizedHttpIsTransformed()
+ {
+ //arrange
+ var message = "message HTTP://www.jabbr.net continues on";
+ HashSet<string> extractedUrls;
+
+ //act
+ var result = TextTransform.TransformAndExtractUrls(message, out extractedUrls);
+
+ //assert
+ Assert.Equal("message <a rel=\"nofollow external\" target=\"_blank\" href=\"HTTP://www.jabbr.net\" title=\"HTTP://www.jabbr.net\">HTTP://www.jabbr.net</a> continues on", result);
+ }
+
+ [Fact]
public void UrlWithHttpsIsTransformed()
{
//arrange
@@ -145,6 +159,34 @@ public void UrlWithHttpsIsTransformed()
//assert
Assert.Equal("message <a rel=\"nofollow external\" target=\"_blank\" href=\"https://www.jabbr.net\" title=\"https://www.jabbr.net\">https://www.jabbr.net</a> continues on", result);
}
+
+ [Fact]
+ public void UrlWithParenthesesIsTransformed()
+ {
+ //arrange
+ var message = "message http://www.jabbr.net/jab(br) continues on";
+ HashSet<string> extractedUrls;
+
+ //act
+ var result = TextTransform.TransformAndExtractUrls(message, out extractedUrls);
+
+ //assert
+ Assert.Equal("message <a rel=\"nofollow external\" target=\"_blank\" href=\"http://www.jabbr.net/jab(br)\" title=\"http://www.jabbr.net/jab(br)\">http://www.jabbr.net/jab(br)</a> continues on", result);
+ }
+
+ [Fact]
+ public void UrlWithUnicodeIsTransformed()
+ {
+ //arrange
+ var message = "message http://➡.ws/䨹 continues on";
+ HashSet<string> extractedUrls;
+
+ //act
+ var result = TextTransform.TransformAndExtractUrls(message, out extractedUrls);
+
+ //assert
+ Assert.Equal("message <a rel=\"nofollow external\" target=\"_blank\" href=\"http://➡.ws/䨹\" title=\"http://➡.ws/䨹\">http://➡.ws/䨹</a> continues on", result);
+ }
}
}
}
View
2  JabbR/Infrastructure/TextTransform.cs
@@ -44,7 +44,7 @@ private string ConvertTextWithNewLines(string message)
public static string TransformAndExtractUrls(string message, out HashSet<string> extractedUrls)
{
- const string urlPattern = @"((https?|ftp)://|www\.)[\w]+(.[\w]+)([\w\-\.\[\],@?^=%&amp;:/~\+#!]*[\w\-\@?^=%&amp;/~\+#\[\]])";
+ const string urlPattern = @"(?i)(?<s>(?:https?|ftp)://|www\.)?(?:\S+(?::\S*)?@)?(?:(?:[\w\p{S}][\w\p{S}@-]*[.:])+\w+)(?(s)/?|/)(?:(?:[^\s()<>.,\u0022'”]+|[.,\u0022'”][^\s()<>.,\u0022]|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))*)";
var urls = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
message = Regex.Replace(message, urlPattern, m =>
Please sign in to comment.
Something went wrong with that request. Please try again.