Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug fix: extract correct indices from text with duplicate URLs. #40

Merged
merged 1 commit into from

2 participants

@keitaf
Collaborator

This fixes a bug reported in twitter/twitter-text-conformance#30

@hontent hontent merged commit db82fa5 into punct_before_url
@caniszczyk caniszczyk deleted the fix_extract_url branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 6 deletions.
  1. +7 −6 twitter-text.js
View
13 twitter-text.js
@@ -579,16 +579,17 @@ if (!window.twttr) {
var urls = [],
position = 0;
- text.replace(twttr.txt.regexen.extractUrl, function(match, all, before, url, protocol, domain, port, path, query) {
- var startPosition = text.indexOf(url, position),
- endPosition = startPosition + url.length;
+ while (twttr.txt.regexen.extractUrl.exec(text)) {
+ var before = RegExp.$2, url = RegExp.$3, protocol = RegExp.$4, domain = RegExp.$5, path = RegExp.$7;
+ var endPosition = twttr.txt.regexen.extractUrl.lastIndex,
+ startPosition = endPosition - url.length;
// if protocol is missing and domain contains non-ASCII characters,
// extract ASCII-only domains.
if (!protocol) {
if (!options.extractUrlsWithoutProtocol
|| before.match(twttr.txt.regexen.invalidUrlWithoutProtocolPrecedingChars)) {
- return;
+ continue;
}
var lastUrl = null,
lastUrlInvalidMatch = false,
@@ -608,7 +609,7 @@ if (!window.twttr) {
// no ASCII-only domain found. Skip the entire URL.
if (lastUrl == null) {
- return;
+ continue;
}
// lastUrl only contains domain. Need to add path and query if they exist.
@@ -630,7 +631,7 @@ if (!window.twttr) {
indices: [startPosition, endPosition]
});
}
- });
+ }
return urls;
};
Something went wrong with that request. Please try again.