Skip to content

Commit

Permalink
using display url when using autoLink if urlEntities array is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
howardr committed Aug 12, 2011
1 parent 3e4b850 commit 327f36e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion test/tests.js
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[^>]+>blog.twitter.com\/2011\/05\/twitte…<\/a>/), 'Use display url from url entities');

});
11 changes: 10 additions & 1 deletion twitter-text.js
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 @@ -427,7 +436,7 @@ if (!window.twttr) {
var d = {
before: before,
htmlAttrs: htmlAttrs,
url: twttr.txt.htmlEscape(url)
url: twttr.txt.htmlEscape((urlEntities && (url in urlEntities)) ? urlEntities[url].display_url : url )
};

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

0 comments on commit 327f36e

Please sign in to comment.