Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Fix text-hiding technique to be compatible with IE7, and make invisib…
Browse files Browse the repository at this point in the history
…le_tag_attrs configurable.
  • Loading branch information
keita committed Mar 29, 2012
1 parent 55364e2 commit 6ca14a7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/com/twitter/Autolink.java
Expand Up @@ -24,6 +24,8 @@ public class Autolink {
public static final String DEFAULT_HASHTAG_URL_BASE = "https://twitter.com/#!/search?q=%23";
/** HTML attribute to add when noFollow is true (default) */
public static final String NO_FOLLOW_HTML_ATTRIBUTE = " rel=\"nofollow\"";
/** Default attribute for invisible span tag */
public static final String DEFAULT_INVISIBLE_TAG_ATTRS = "style='position:absolute;left:-999px;'";

protected String urlClass;
protected String listClass;
Expand All @@ -32,6 +34,7 @@ public class Autolink {
protected String usernameUrlBase;
protected String listUrlBase;
protected String hashtagUrlBase;
protected String invisibleTagAttrs;
protected boolean noFollow = true;
protected boolean usernameIncludeSymbol = false;

Expand Down Expand Up @@ -60,6 +63,7 @@ public Autolink() {
usernameUrlBase = DEFAULT_USERNAME_URL_BASE;
listUrlBase = DEFAULT_LIST_URL_BASE;
hashtagUrlBase = DEFAULT_HASHTAG_URL_BASE;
invisibleTagAttrs = DEFAULT_INVISIBLE_TAG_ATTRS;

extractor.setExtractURLWithoutProtocol(false);
}
Expand Down Expand Up @@ -175,7 +179,7 @@ public void linkToURL(Entity entity, String text, StringBuilder builder) {
String afterDisplayURL = entity.expandedURL.substring(diplayURLIndexInExpandedURL + displayURLSansEllipses.length());
String precedingEllipsis = entity.displayURL.startsWith("…") ? "…" : "";
String followingEllipsis = entity.displayURL.endsWith("…") ? "…" : "";
String invisibleSpan = "<span style='font-size:0; line-height:0'>";
String invisibleSpan = "<span " + invisibleTagAttrs + ">";

StringBuilder sb = new StringBuilder("<span class='tco-ellipsis'>");
sb.append(precedingEllipsis);
Expand All @@ -186,6 +190,8 @@ public void linkToURL(Entity entity, String text, StringBuilder builder) {
sb.append("<span class='tco-ellipsis'>").append(invisibleSpan).append("&nbsp;</span>").append(followingEllipsis).append("</span>");

linkText = sb;
} else {
linkText = entity.displayURL;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/com/twitter/AutolinkTest.java
Expand Up @@ -58,7 +58,7 @@ public void testURLEntities() {
List<Entity> entities = new ArrayList<Entity>();
entities.add(entity);
String tweet = "http://t.co/0JG5Mcq";
String expected = "<a href=\"http://t.co/0JG5Mcq\" rel=\"nofollow\"><span class='tco-ellipsis'><span style='font-size:0; line-height:0'>&nbsp;</span></span><span style='font-size:0; line-height:0'>http://</span><span class='js-display-url'>blog.twitter.com/2011/05/twitte</span><span style='font-size:0; line-height:0'>r-for-mac-update.html</span><span class='tco-ellipsis'><span style='font-size:0; line-height:0'>&nbsp;</span>…</span></a>";
String expected = "<a href=\"http://t.co/0JG5Mcq\" rel=\"nofollow\"><span class='tco-ellipsis'><span style='position:absolute;left:-999px;'>&nbsp;</span></span><span style='position:absolute;left:-999px;'>http://</span><span class='js-display-url'>blog.twitter.com/2011/05/twitte</span><span style='position:absolute;left:-999px;'>r-for-mac-update.html</span><span class='tco-ellipsis'><span style='position:absolute;left:-999px;'>&nbsp;</span>…</span></a>";

assertAutolink(expected, linker.autoLinkEntities(tweet, entities));
}
Expand Down

0 comments on commit 6ca14a7

Please sign in to comment.