Skip to content

Commit f7271f5

Browse files
committed
Detect partially selected URI more correctly even if there are any shorter versions before it
1 parent 0f7eae0 commit f7271f5

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

webextensions/background/uriMatcher.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@ var URIMatcher = {
1515
if (match.length == 0)
1616
return null;
1717

18+
const longestResultAt = new Map();
1819
for (let maybeURI of match) {
1920
maybeURI = this.sanitizeURIString(maybeURI);
2021
const uriRange = await this.findTextRange({
2122
text: maybeURI,
2223
range: params.cursor,
2324
tabId: params.tabId
2425
});
25-
if (!uriRange)
26+
if (!uriRange) {
2627
continue;
27-
return {
28+
}
29+
const positionKey = `${uriRange.startTextNodePos}:${uriRange.startOffset}`;
30+
const result = {
2831
text: maybeURI,
2932
range: uriRange,
3033
uri: this.fixupURI(maybeURI, params.baseURI)
3134
};
35+
const longestResult = longestResultAt.get(positionKey);
36+
if (!longestResult ||
37+
(longestResult.text.length <= maybeURI.length &&
38+
maybeURI.startsWith(longestResult.text))) {
39+
longestResultAt.set(positionKey, result);
40+
}
41+
}
42+
if (longestResultAt.size > 0) {
43+
return [...longestResultAt.values()][0];
3244
}
3345
log(' => no match');
3446
}

0 commit comments

Comments
 (0)