Skip to content

Commit

Permalink
Merge pull request twitter-archive#53 from twitter/fix_urlentities
Browse files Browse the repository at this point in the history
Fix text-hiding technique to be compatible with IE7, and set linkText to...
  • Loading branch information
Keita Fujii committed Apr 3, 2012
2 parents 87e67c8 + 09cf779 commit 24c5136
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions test/tests.js
Expand Up @@ -125,6 +125,7 @@ test("twttr.txt.autolink", function() {

// url entities
var autoLinkResult = twttr.txt.autoLink("http://t.co/0JG5Mcq", {
invisibleTagAttrs: "style='font-size:0'",
urlEntities: [{
"url": "http://t.co/0JG5Mcq",
"display_url": "blog.twitter.com/2011/05/twitte…",
Expand All @@ -139,6 +140,7 @@ test("twttr.txt.autolink", function() {
ok(autoLinkResult.match(/>blog.twitter.com\/2011\/05\/twitte.*…</), 'Use display url from url entities');
ok(autoLinkResult.match(/r-for-mac-update.html</), 'Include the tail of expanded_url');
ok(autoLinkResult.match(/>http:\/\//), 'Include the head of expanded_url');
ok(autoLinkResult.match(/span style='font-size:0'/), 'Obey invisibleTagAttrs');

// Insert the HTML into the document and verify that, if copied and pasted, it would get the expanded_url.
var div = document.createElement('div');
Expand All @@ -149,6 +151,21 @@ test("twttr.txt.autolink", function() {
ok(range.toString().match(/\shttp:\/\/blog.twitter.com\/2011\/05\/twitter-for-mac-update.html\s…/), 'Selection copies expanded_url');
document.body.removeChild(div);

var picTwitter = twttr.txt.autoLink("http://t.co/0JG5Mcq", {
urlEntities: [{
"url": "http://t.co/0JG5Mcq",
"display_url": "pic.twitter.com/xyz",
"expanded_url": "http://twitter.com/foo/statuses/123/photo/1",
"indices": [
84,
103
]
}]
});
ok(picTwitter.match(/<a href="http:\/\/t.co\/0JG5Mcq"[^>]+>/), 'Use t.co URL as link target');
ok(picTwitter.match(/>pic.twitter.com\/xyz</), 'Use display url from url entities');
ok(!picTwitter.match(/foo\/statuses/), 'Don\'t include the tail of expanded_url');

// urls with invalid character
var invalidChars = ['\u202A', '\u202B', '\u202C', '\u202D', '\u202E'];
for (i = 0; i < invalidChars.length; i++) {
Expand Down
5 changes: 4 additions & 1 deletion twitter-text.js
Expand Up @@ -505,8 +505,10 @@ if (typeof twttr === "undefined" || twttr === null) {
// <span style='font-size:0'>&nbsp;</span>
// …
// </span>
v['invisible'] = "style='font-size:0; line-height:0'";
v['invisible'] = options.invisibleTagAttrs;
linkText = stringSupplant("<span class='tco-ellipsis'>#{precedingEllipsis}<span #{invisible}>&nbsp;</span></span><span #{invisible}>#{beforeDisplayUrl}</span><span class='js-display-url'>#{displayUrlSansEllipses}</span><span #{invisible}>#{afterDisplayUrl}</span><span class='tco-ellipsis'><span #{invisible}>&nbsp;</span>#{followingEllipsis}</span>", v);
} else {
linkText = displayUrl;
}
}

Expand Down Expand Up @@ -537,6 +539,7 @@ if (typeof twttr === "undefined" || twttr === null) {
options.listUrlBase = options.listUrlBase || "https://twitter.com/";
options.before = options.before || "";
options.htmlAttrs = twttr.txt.extractHtmlAttrsFromOptions(options);
options.invisibleTagAttrs = options.invisibleTagAttrs || "style='position:absolute;left:-9999px;'";

// remap url entities to hash
var urlEntities, i, len;
Expand Down

0 comments on commit 24c5136

Please sign in to comment.