Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add getTweetLength()

  • Loading branch information...
commit 3f3d5e69261c267a820b55c4c0830fc680f6608c 1 parent 9a7dd4d
@keitaf keitaf authored
Showing with 24 additions and 7 deletions.
  1. +4 −0 test/conformance.html
  2. +20 −7 twitter-text.js
View
4 test/conformance.html
@@ -113,6 +113,10 @@
return function(test) {
return twttr.txt.isValidUrl(test.text, true, false);
};
+ case "lengths":
+ return function(test) {
+ return twttr.txt.getTweetLength(test.text);
+ };
}
}
View
27 twitter-text.js
@@ -1019,18 +1019,31 @@ if (typeof twttr === "undefined" || twttr === null) {
fromCode(0x202D),
fromCode(0x202E)
];
-
+
// Returns the length of Tweet text with consideration to t.co URL replacement
- twttr.txt.getLengthWithWrappedUrls = function(text) {
+ twttr.txt.getTweetLength = function(text, options) {
+ if (!options) {
+ options = {
+ short_url_length: 20,
+ short_url_length_https: 21
+ };
+ }
var textLength = text.length;
var urlsWithIndices = twttr.txt.extractUrlsWithIndices(text);
-
+
for (var i = 0; i < urlsWithIndices.length; i++) {
// Subtract the length of the original URL
- // Add 20 characters to account for a t.co URL
- textLength += urlsWithIndices[i].indices[0] - urlsWithIndices[i].indices[1] + 20;
+ textLength += urlsWithIndices[i].indices[0] - urlsWithIndices[i].indices[1];
+
+ // Add 21 characters for URL starting with https://
+ // Otherwise add 20 characters
+ if (urlsWithIndices[i].url.toLowerCase().match(/^https:\/\//)) {
+ textLength += options.short_url_length_https;
+ } else {
+ textLength += options.short_url_length;
+ }
}
-
+
return textLength;
};
@@ -1049,7 +1062,7 @@ if (typeof twttr === "undefined" || twttr === null) {
}
// Determine max length independent of URL length
- if (twttr.txt.getLengthWithWrappedUrls(text) > MAX_LENGTH) {
+ if (twttr.txt.getTweetLength(text) > MAX_LENGTH) {
return "too_long";
}
Please sign in to comment.
Something went wrong with that request. Please try again.