Permalink
Browse files

Add htmlAttributeBlock option

  • Loading branch information...
1 parent b7397e3 commit 2cdbd461ab9da83b7b7342abfbf20b491a43884f @keitaf keitaf committed Jun 5, 2012
Showing with 33 additions and 8 deletions.
  1. +11 −1 test/tests.js
  2. +22 −7 twitter-text.js
View
@@ -124,7 +124,7 @@ test("twttr.txt.autolink", function() {
// test urlClass
same(twttr.txt.autoLink("http://twitter.com"), "<a href=\"http://twitter.com\" rel=\"nofollow\">http://twitter.com</a>", "AutoLink without urlClass");
- same(twttr.txt.autoLink("http://twitter.com", {urlClass: "testClass"}), "<a href=\"http://twitter.com\" rel=\"nofollow\" class=\"testClass\">http://twitter.com</a>", "autoLink with urlClass");
+ same(twttr.txt.autoLink("http://twitter.com", {urlClass: "testClass"}), "<a href=\"http://twitter.com\" class=\"testClass\" rel=\"nofollow\">http://twitter.com</a>", "autoLink with urlClass");
ok(!twttr.txt.autoLink("#hash @tw", {urlClass: "testClass"}).match(/class=\"testClass\"/), "urlClass won't be used for hashtag and @mention auto-links");
// test urlTarget
@@ -134,6 +134,16 @@ test("twttr.txt.autolink", function() {
ok(autoLinkResult.match(/<a[^>]+test.com[^>]+target=\"_blank\"[^>]*>/), "urlTarget should be applied to auto-linked URL");
ok(!autoLinkResult.match(/urlClass/i), "urlClass should not appear in HTML");
+ // test htmlAttributeBlock
+ autoLinkResult = twttr.txt.autoLink("#hash @mention", {htmlAttributeBlock: function(entity, attributes) {if (entity.hashtag) attributes["dummy-hash-attr"] = "test";}});
+ ok(autoLinkResult.match(/<a[^>]+hashtag[^>]+dummy-hash-attr=\"test\"[^>]+>/), "htmlAttributeBlock should be applied to hashtag");
+ ok(!autoLinkResult.match(/<a[^>]+username[^>]+dummy-hash-attr=\"test\"[^>]+>/), "htmlAttributeBlock should not be applied to mention");
+ ok(!autoLinkResult.match(/html_attribute_block/i), "htmlAttributeBlock should not appear in HTML");
+
+ autoLinkResult = twttr.txt.autoLink("@mention http://twitter.com/", {htmlAttributeBlock: function(entity, attributes) {if (entity.url) attributes["dummy-url-attr"] = entity.url;}});
+ ok(!autoLinkResult.match(/<a[^>]+username[^>]+dummy-hash-attr=\"test\"[^>]+>/), "htmlAttributeBlock should not be applied to mention");
+ ok(autoLinkResult.match(/<a[^>]+dummy-url-attr=\"http:\/\/twitter.com\/\"/), "htmlAttributeBlock should be applied to URL");
+
// url entities
autoLinkResult = twttr.txt.autoLink("http://t.co/0JG5Mcq", {
invisibleTagAttrs: "style='font-size:0'",
View
@@ -397,11 +397,15 @@ if (typeof twttr === "undefined" || twttr === null) {
return r;
}
- twttr.txt.linkToTextWithSymbol = function(symbol, text, attributes, options) {
+ twttr.txt.linkToTextWithSymbol = function(entity, symbol, text, attributes, options) {
var taggedSymbol = options.symbolTag ? "<" + options.symbolTag + ">" + symbol + "</"+ options.symbolTag + ">" : symbol;
text = twttr.txt.htmlEscape(text);
var taggedText = options.textWithSymbolTag ? "<" + options.textWithSymbolTag + ">" + text + "</"+ options.textWithSymbolTag + ">" : text;
+ // if htmlAttributeBlock is specified, call it to modify the attributes
+ if (options.htmlAttributeBlock) {
+ options.htmlAttributeBlock(entity, attributes);
+ }
var d = {
tagAttr: twttr.txt.extractHtmlAttrsFromOptions(attributes) + (options.htmlAttrs || ""),
taggedSymbol: taggedSymbol,
@@ -423,7 +427,7 @@ if (typeof twttr === "undefined" || twttr === null) {
class: options.hashtagClass
};
- return twttr.txt.linkToTextWithSymbol(hash, hashtag, attrs, options);
+ return twttr.txt.linkToTextWithSymbol(entity, hash, hashtag, attrs, options);
};
twttr.txt.linkToCashtag = function(entity, text, options) {
@@ -434,7 +438,7 @@ if (typeof twttr === "undefined" || twttr === null) {
class: options.cashtagClass
};
- return twttr.txt.linkToTextWithSymbol("$", cashtag, attrs, options);
+ return twttr.txt.linkToTextWithSymbol(entity, "$", cashtag, attrs, options);
};
twttr.txt.linkToMentionAndList = function(entity, text, options) {
@@ -450,7 +454,7 @@ if (typeof twttr === "undefined" || twttr === null) {
attrs['data-screen-name'] = user;
}
- return twttr.txt.linkToTextWithSymbol(at, isList ? user + slashListname : user, attrs, options);
+ return twttr.txt.linkToTextWithSymbol(entity, at, isList ? user + slashListname : user, attrs, options);
};
twttr.txt.linkToUrl = function(entity, text, options) {
@@ -466,18 +470,29 @@ if (typeof twttr === "undefined" || twttr === null) {
linkText = twttr.txt.linkTextWithEntity(urlEntity, options);
}
+ var attrs = {};
+
// set class only if urlClass is specified.
if (options.urlClass) {
- options.htmlAttrs = (options.htmlAttrs || "") + " class=\"" + options.urlClass + "\"";
+ attrs["class"] = options.urlClass;
}
// set target only if urlTarget is specified.
if (options.urlTarget) {
- options.htmlAttrs = (options.htmlAttrs || "") + " target=\"" + options.urlTarget + "\"";
+ attrs.target = options.urlTarget;
+ }
+
+ if (!options.title && urlEntity.display_url) {
+ attrs.title = urlEntity.expanded_url;
+ }
+
+ // if htmlAttributeBlock is specified, call it to modify the attributes
+ if (options.htmlAttributeBlock) {
+ options.htmlAttributeBlock(entity, attrs);
}
var d = {
- htmlAttrs: options.htmlAttrs + (!options.title && urlEntity.display_url ? " title=\"" + urlEntity.expanded_url + "\"" : ""),
+ htmlAttrs: twttr.txt.extractHtmlAttrsFromOptions(attrs) + (options.htmlAttrs || ""),
url: twttr.txt.htmlEscape(url),
linkText: linkText
};

0 comments on commit 2cdbd46

Please sign in to comment.