OSDN Git Service

Twitter.GetTextLengthRemainメソッドからURLを抽出する処理をExtractUrlsメソッドに分割
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 17 Oct 2015 10:23:34 +0000 (19:23 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 19 Oct 2015 10:20:59 +0000 (19:20 +0900)
OpenTween.Tests/TwitterTest.cs
OpenTween/Twitter.cs

index a3df71b..8905894 100644 (file)
@@ -383,5 +383,26 @@ namespace OpenTween
                 Assert.Equal(133, twitter.GetTextLengthRemain("🔥🐔🔥 焼き鳥"));
             }
         }
+
+        [Fact]
+        public void ExtractUrls_Test()
+        {
+            Assert.Equal(new[] { "http://example.com/" }, Twitter.ExtractUrls("http://example.com/"));
+            Assert.Equal(new[] { "http://example.com/hogehoge" }, Twitter.ExtractUrls("http://example.com/hogehoge"));
+            Assert.Equal(new[] { "http://example.com/" }, Twitter.ExtractUrls("hogehoge http://example.com/"));
+
+            Assert.Equal(new[] { "https://example.com/" }, Twitter.ExtractUrls("https://example.com/"));
+            Assert.Equal(new[] { "https://example.com/hogehoge" }, Twitter.ExtractUrls("https://example.com/hogehoge"));
+            Assert.Equal(new[] { "https://example.com/" }, Twitter.ExtractUrls("hogehoge https://example.com/"));
+
+            Assert.Equal(new[] { "example.com" }, Twitter.ExtractUrls("example.com"));
+            Assert.Equal(new[] { "example.com/hogehoge" }, Twitter.ExtractUrls("example.com/hogehoge"));
+            Assert.Equal(new[] { "example.com" }, Twitter.ExtractUrls("hogehoge example.com"));
+
+            // スキーム (http://) を省略かつ末尾が ccTLD の場合は t.co に短縮されない
+            Assert.Empty(Twitter.ExtractUrls("example.jp"));
+            // ただし、末尾にパスが続く場合は t.co に短縮される
+            Assert.Equal(new[] { "example.jp/hogehoge" }, Twitter.ExtractUrls("example.jp/hogehoge"));
+        }
     }
 }
index ba8403b..179f2e5 100644 (file)
@@ -3148,7 +3148,25 @@ namespace OpenTween
                     pos++;
             }
 
-            var urlMatches = Regex.Matches(postText, Twitter.rgUrl, RegexOptions.IgnoreCase).Cast<Match>();
+            var urls = ExtractUrls(postText);
+            foreach (var url in urls)
+            {
+                var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
+                    ? this.Configuration.ShortUrlLengthHttps
+                    : this.Configuration.ShortUrlLength;
+
+                textLength += shortUrlLength - url.Length;
+            }
+
+            if (isDm)
+                return this.Configuration.DmTextCharacterLimit - textLength;
+            else
+                return 140 - textLength;
+        }
+
+        public static IEnumerable<string> ExtractUrls(string text)
+        {
+            var urlMatches = Regex.Matches(text, Twitter.rgUrl, RegexOptions.IgnoreCase).Cast<Match>();
             foreach (var m in urlMatches)
             {
                 var before = m.Groups["before"].Value;
@@ -3183,23 +3201,14 @@ namespace OpenTween
 
                     if (validUrl)
                     {
-                        textLength += this.Configuration.ShortUrlLength - url.Length;
+                        yield return url;
                     }
                 }
                 else
                 {
-                    var shortUrlLength = protocol == "https://"
-                        ? this.Configuration.ShortUrlLengthHttps
-                        : this.Configuration.ShortUrlLength;
-
-                    textLength += shortUrlLength - url.Length;
+                    yield return url;
                 }
             }
-
-            if (isDm)
-                return this.Configuration.DmTextCharacterLimit - textLength;
-            else
-                return 140 - textLength;
         }
 
 #region "UserStream"