Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 7 commits
  • 2 files changed
  • 0 comments
  • 2 contributors
193  src/com/twitter/Autolink.java
@@ -2,22 +2,22 @@
@@ -26,12 +26,18 @@
@@ -43,12 +49,18 @@
@@ -62,7 +74,7 @@ private static CharSequence escapeHTML(String text) {
@@ -94,19 +106,61 @@ else if (c == '<')
@@ -114,32 +168,22 @@ public void linkToMentionAndList(Entity entity, String text, StringBuilder build
@@ -203,22 +247,18 @@ public void linkToURL(Entity entity, String text, StringBuilder builder) {
@@ -477,4 +517,49 @@ public void setNoFollow(boolean noFollow) {
98  tests/com/twitter/AutolinkTest.java
@@ -3,6 +3,8 @@
3 3
 
4 4
 import java.util.ArrayList;
5 5
 import java.util.List;
  6
+import java.util.Map;
  7
+import java.util.regex.Pattern;
6 8
 
7 9
 import com.twitter.Extractor.Entity;
8 10
 
@@ -81,6 +83,102 @@ public void testUsernameIncludeSymbol() {
81 83
     assertAutolink(expected, linker.autoLink(tweet));
82 84
   }
83 85
 
  86
+  public void testUrlClass() {
  87
+    linker.setNoFollow(false);
  88
+
  89
+    String tweet = "http://twitter.com";
  90
+    String expected = "<a href=\"http://twitter.com\">http://twitter.com</a>";
  91
+    assertAutolink(expected, linker.autoLink(tweet));
  92
+
  93
+    linker.setUrlClass("testClass");
  94
+    expected = "<a href=\"http://twitter.com\" class=\"testClass\">http://twitter.com</a>";
  95
+    assertAutolink(expected, linker.autoLink(tweet));
  96
+
  97
+    tweet = "#hash @tw";
  98
+    String result = linker.autoLink(tweet);
  99
+    assertTrue(result.contains("class=\"" + Autolink.DEFAULT_HASHTAG_CLASS + "\""));
  100
+    assertTrue(result.contains("class=\"" + Autolink.DEFAULT_USERNAME_CLASS + "\""));
  101
+    assertFalse(result.contains("class=\"testClass\""));
  102
+  }
  103
+
  104
+  public void testSymbolTag() {
  105
+    linker.setSymbolTag("s");
  106
+    linker.setTextWithSymbolTag("b");
  107
+    linker.setNoFollow(false);
  108
+
  109
+    String tweet = "#hash";
  110
+    String expected = "<a href=\"https://twitter.com/#!/search?q=%23hash\" title=\"#hash\" class=\"tweet-url hashtag\"><s>#</s><b>hash</b></a>";
  111
+    assertAutolink(expected, linker.autoLink(tweet));
  112
+
  113
+    tweet = "@mention";
  114
+    expected = "<s>@</s><a class=\"tweet-url username\" href=\"https://twitter.com/mention\"><b>mention</b></a>";
  115
+    assertAutolink(expected, linker.autoLink(tweet));
  116
+
  117
+    linker.setUsernameIncludeSymbol(true);
  118
+    expected = "<a class=\"tweet-url username\" href=\"https://twitter.com/mention\"><s>@</s><b>mention</b></a>";
  119
+    assertAutolink(expected, linker.autoLink(tweet));
  120
+  }
  121
+
  122
+  public void testUrlTarget() {
  123
+    linker.setUrlTarget("_blank");
  124
+
  125
+    String tweet = "http://test.com";
  126
+    String result = linker.autoLink(tweet);
  127
+    assertFalse("urlTarget shouldn't be applied to auto-linked hashtag", Pattern.matches(".*<a[^>]+hashtag[^>]+target[^>]+>.*", result));
  128
+    assertFalse("urlTarget shouldn't be applied to auto-linked mention", Pattern.matches(".*<a[^>]+username[^>]+target[^>]+>.*", result));
  129
+    assertTrue("urlTarget should be applied to auto-linked URL", Pattern.matches(".*<a[^>]+test.com[^>]+target=\"_blank\"[^>]*>.*", result));
  130
+    assertFalse("urlClass should not appear in HTML", result.toLowerCase().contains("urlclass"));
  131
+  }
  132
+
  133
+  public void testLinkAttributeModifier() {
  134
+    linker.setLinkAttributeModifier(new Autolink.LinkAttributeModifier() {
  135
+      public void modify(Entity entity, Map<String, String> attributes) {
  136
+        if (entity.type == Entity.Type.HASHTAG) {
  137
+          attributes.put("dummy-hash-attr", "test");
  138
+        }
  139
+      }
  140
+    });
  141
+
  142
+    String result = linker.autoLink("#hash @mention");
  143
+    assertTrue("HtmlAttributeModifier should be applied to hashtag", Pattern.matches(".*<a[^>]+hashtag[^>]+dummy-hash-attr=\"test\"[^>]*>.*", result));
  144
+    assertFalse("HtmlAttributeModifier should not be applied to mention", Pattern.matches(".*<a[^>]+username[^>]+dummy-hash-attr=\"test\"[^>]*>.*", result));
  145
+
  146
+    linker.setLinkAttributeModifier(new Autolink.LinkAttributeModifier() {
  147
+      public void modify(Entity entity, Map<String, String> attributes) {
  148
+        if (entity.type == Entity.Type.URL) {
  149
+          attributes.put("dummy-url-attr", entity.value);
  150
+        }
  151
+      }
  152
+    });
  153
+    result = linker.autoLink("@mention http://twitter.com/");
  154
+    assertFalse("HtmlAttributeModifier should not be applied to mention", Pattern.matches(".*<a[^>]+username[^>]+dummy-url-attr[^>]*>.*", result));
  155
+    assertTrue("htmlAttributeBlock should be applied to URL", Pattern.matches(".*<a[^>]+dummy-url-attr=\"http://twitter.com/\".*", result));
  156
+  }
  157
+
  158
+  public void testLinkTextModifier() {
  159
+    linker.setLinkTextModifier(new Autolink.LinkTextModifier() {
  160
+      public CharSequence modify(Entity entity, CharSequence text) {
  161
+        return entity.type == Entity.Type.HASHTAG ? "#replaced" : "pre_" + text + "_post";
  162
+      }
  163
+    });
  164
+
  165
+    String result = linker.autoLink("#hash @mention");
  166
+    assertTrue("LinkTextModifier should modify a hashtag link text", Pattern.matches(".*<a[^>]+>#replaced</a>.*", result));
  167
+    assertTrue("LinkTextModifier should modify a username link text", Pattern.matches(".*<a[^>]+>pre_mention_post</a>.*", result));
  168
+
  169
+    linker.setLinkTextModifier(new Autolink.LinkTextModifier() {
  170
+      public CharSequence modify(Entity entity, CharSequence text) {
  171
+        return "pre_" + text + "_post";
  172
+      }
  173
+    });
  174
+    linker.setSymbolTag("s");
  175
+    linker.setTextWithSymbolTag("b");
  176
+    linker.setUsernameIncludeSymbol(true);
  177
+    result = linker.autoLink("#hash @mention");
  178
+    assertTrue("LinkTextModifier should modify a hashtag link text", Pattern.matches(".*<a[^>]+>pre_<s>#</s><b>hash</b>_post</a>.*", result));
  179
+    assertTrue("LinkTextModifier should modify a username link text", Pattern.matches(".*<a[^>]+>pre_<s>@</s><b>mention</b>_post</a>.*", result));
  180
+  }
  181
+
84 182
   protected void assertAutolink(String expected, String linked) {
85 183
     assertEquals("Autolinked text should equal the input", expected, linked);
86 184
   }

No commit comments for this range

Something went wrong with that request. Please try again.