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

Commit

Permalink
@tw's changes to allow pre/post hooks for usernames and hashtags
Browse files Browse the repository at this point in the history
  • Loading branch information
J.P. Cummins committed Apr 19, 2011
1 parent 5c47e44 commit c81d7a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ test("twttr.txt.splitTags", function() {
for (var i = 0; i < tests.length; i++) {
same(twttr.txt.splitTags(tests[i][0]), tests[i][1], tests[i][2] || tests[i][0]);
}
});

test("twttr.txt.autolink", function() {
// Username Overrides
ok(twttr.txt.autoLink("@tw", { before: "!" }).match(/!@<a[^>]+>tw<\/a>/), "Override before");
ok(twttr.txt.autoLink("@tw", { at: "!" }).match(/!<a[^>]+>tw<\/a>/), "Override at");
ok(twttr.txt.autoLink("@tw", { preChunk: "<b>" }).match(/@<a[^>]+><b>tw<\/a>/), "Override preChunk");
ok(twttr.txt.autoLink("@tw", { postChunk: "</b>" }).match(/@<a[^>]+>tw<\/b><\/a>/), "Override postChunk");

// Hashtag Overrides
ok(twttr.txt.autoLink("#hi", { before: "!" }).match(/!<a[^>]+>#hi<\/a>/), "Override before");
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");
});
10 changes: 7 additions & 3 deletions twitter-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ if (!window.twttr) {
user: twttr.txt.htmlEscape(user),
slashListname: twttr.txt.htmlEscape(slashListname),
extraHtml: extraHtml,
chunk: twttr.txt.htmlEscape(chunk)
preChunk: "",
chunk: twttr.txt.htmlEscape(chunk),
postChunk: ""
};
for (var k in options) {
if (options.hasOwnProperty(k)) {
Expand All @@ -221,7 +223,7 @@ if (!window.twttr) {
// this is a screen name
d.chunk = twttr.txt.htmlEscape(user);
d.dataScreenName = !options.suppressDataScreenName ? stringSupplant("data-screen-name=\"#{chunk}\" ", d) : "";
return stringSupplant("#{before}#{at}<a class=\"#{urlClass} #{usernameClass}\" #{dataScreenName}href=\"#{usernameUrlBase}#{chunk}\"#{extraHtml}>#{chunk}</a>", d);
return stringSupplant("#{before}#{at}<a class=\"#{urlClass} #{usernameClass}\" #{dataScreenName}href=\"#{usernameUrlBase}#{chunk}\"#{extraHtml}>#{preChunk}#{chunk}#{postChunk}</a>", d);
}
}
});
Expand All @@ -244,7 +246,9 @@ if (!window.twttr) {
var d = {
before: before,
hash: twttr.txt.htmlEscape(hash),
preText: "",
text: twttr.txt.htmlEscape(text),
postText: "",
extraHtml: extraHtml
};

Expand All @@ -254,7 +258,7 @@ if (!window.twttr) {
}
}

return stringSupplant("#{before}<a href=\"#{hashtagUrlBase}#{text}\" title=\"##{text}\" class=\"#{urlClass} #{hashtagClass}\"#{extraHtml}>#{hash}#{text}</a>", d);
return stringSupplant("#{before}<a href=\"#{hashtagUrlBase}#{text}\" title=\"##{text}\" class=\"#{urlClass} #{hashtagClass}\"#{extraHtml}>#{hash}#{preText}#{text}#{postText}</a>", d);
});
};

Expand Down

0 comments on commit c81d7a0

Please sign in to comment.