Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of https://github.com/jeremiahlee/twitter-text-js

… into tweet_length
  • Loading branch information...
commit 9a7dd4d3fca2c90e521305e33cde81c0740505b9 2 parents 2f0043e + 5edc117
@keitaf keitaf authored
Showing with 16 additions and 1 deletion.
  1. +16 −1 twitter-text.js
View
17 twitter-text.js
@@ -1019,6 +1019,20 @@ 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) {
+ 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;
+ }
+
+ return textLength;
+ };
// Check the text for any reason that it may not be valid as a Tweet. This is meant as a pre-validation
// before posting to api.twitter.com. There are several server-side reasons for Tweets to fail but this pre-validation
@@ -1034,7 +1048,8 @@ if (typeof twttr === "undefined" || twttr === null) {
return "empty";
}
- if (text.length > MAX_LENGTH) {
+ // Determine max length independent of URL length
+ if (twttr.txt.getLengthWithWrappedUrls(text) > MAX_LENGTH) {
return "too_long";
}
Please sign in to comment.
Something went wrong with that request. Please try again.