Skip to content

Commit

Permalink
Merge pull request twitter-archive#8 from howardr/master
Browse files Browse the repository at this point in the history
Use display url in autoLinkUrlsCustom if urlEntities array is provided as an option
  • Loading branch information
mzsanford committed Aug 16, 2011
2 parents 3e4b850 + 2f95fb9 commit 54547a9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
16 changes: 15 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ test("twttr.txt.autolink", function() {
ok(twttr.txt.autoLink("#hi", { hash: "!" }).match(/<a[^>]+>!hi<\/a>/), "Override hash");
ok(twttr.txt.autoLink("#hi", { preText: "<b>" }).match(/<a[^>]+>#<b>hi<\/a>/), "Override preText");
ok(twttr.txt.autoLink("#hi", { postText: "</b>" }).match(/<a[^>]+>#hi<\/b><\/a>/), "Override postText");
});

// url entities
ok(twttr.txt.autoLink("http://t.co/0JG5Mcq", {
urlEntities: [{
"url": "http://t.co/0JG5Mcq",
"display_url": "blog.twitter.com/2011/05/twitte…",
"expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",
"indices": [
84,
103
]
}]
}).match(/<a href="http:\/\/t.co\/0JG5Mcq"[^>]+>blog.twitter.com\/2011\/05\/twitte…<\/a>/), 'Use display url from url entities');

});
2 changes: 1 addition & 1 deletion test/twitter-text-conformance
Submodule twitter-text-conformance updated from b186e8 to 55be80
17 changes: 16 additions & 1 deletion twitter-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ if (!window.twttr) {
delete options.urlClass;
}

// remap url entities to hash
var urlEntities, i, len;
if(options.urlEntities) {
urlEntities = {};
for(i = 0, len = options.urlEntities.length; i < len; i++) {
urlEntities[options.urlEntities[i].url] = options.urlEntities[i];
}
}

delete options.suppressNoFollow;
delete options.suppressDataScreenName;
delete options.listClass;
Expand All @@ -429,8 +438,14 @@ if (!window.twttr) {
htmlAttrs: htmlAttrs,
url: twttr.txt.htmlEscape(url)
};
if(urlEntities && urlEntities[url] && urlEntities[url].display_url) {
d.displayUrl = twttr.txt.htmlEscape(urlEntities[url].display_url);
}
else {
d.displayUrl = d.url;
}

return stringSupplant("#{before}<a href=\"#{url}\"#{htmlAttrs}>#{url}</a>", d);
return stringSupplant("#{before}<a href=\"#{url}\"#{htmlAttrs}>#{displayUrl}</a>", d);
} else {
return all;
}
Expand Down

0 comments on commit 54547a9

Please sign in to comment.