private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { string text = args.NewValue as string; if (!string.IsNullOrEmpty(text)) { TweetTextBlock textblock = (TweetTextBlock)obj; textblock.Inlines.Clear(); textblock.Inlines.Add(" "); string[] words = Regex.Split(text, @"([ \(\)\{\}\[\]])"); string possibleUserName = words[0].ToString(); if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "@")) { textblock = FormatName(textblock, possibleUserName); words.SetValue("", 0); } foreach (string word in words) { // clickable hyperlinks if (UrlShorteningService.IsUrl(word)) { try { Hyperlink link = new Hyperlink(); link.NavigateUri = new Uri(word); link.Inlines.Add(word); link.Click += new RoutedEventHandler(link_Click); link.ToolTip = "Open link in the default browser"; textblock.Inlines.Add(link); } catch { //TODO:What are we catching here? Why? Log it? textblock.Inlines.Add(word); } } // clickable @name else if (word.StartsWith("@")) { textblock = FormatName(textblock, word); } // clickable #hashtag else if (word.StartsWith("#")) { string hashtag = String.Empty; Match foundHashtag = Regex.Match(word, @"#(\w+)(?<suffix>.*)"); if (foundHashtag.Success) { hashtag = foundHashtag.Groups[1].Captures[0].Value; Hyperlink tag = new Hyperlink(); tag.Inlines.Add(hashtag); string hashtagUrl = "http://search.twitter.com/search?q=%23{0}"; // The main application has access to the Settings class, where a // user-defined hashtagUrl can be stored. This hardcoded one that // is used to set the NavigateUri is just a default behavior that // will be used if the click event is not handled for some reason. tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag)); tag.ToolTip = "Show statuses that include this hashtag"; tag.Tag = hashtag; tag.Click += new RoutedEventHandler(hashtag_Click); textblock.Inlines.Add("#"); textblock.Inlines.Add(tag); textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value); } } else { textblock.Inlines.Add(word); } } textblock.Inlines.Add(" "); } }
public void Addresses_that_have_commas_should_be_urls() { Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/x,123")); }
public void Addresses_that_dont_end_in_a_filename_extension_should_be_urls() { Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/x")); }
public void Web_site_root_addresses_should_be_urls() { Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/")); }
public void Things_that_start_with_feed_should_not_be_urls() { Assert.IsFalse(UrlShorteningService.IsUrl("feed://www.foo.com/blah/blah.aspx")); }
public void Things_that_start_with_https_should_not_be_urls() { Assert.IsTrue(UrlShorteningService.IsUrl("https://www.foo.com/blah/blah.aspx")); }