Skip to content

Commit 1960b0d

Browse files
committed
Guard against search cursors with a query of ""
They'd exhibit the same infinite traversal behavior mentioned in #1155
1 parent 8eca1bc commit 1960b0d

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

addon/search/searchcursor.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@
4040
var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
4141
var target = query.split("\n");
4242
// Different methods for single-line and multi-line queries
43-
if (target.length == 1)
44-
this.matches = function(reverse, pos) {
45-
var line = fold(cm.getLine(pos.line)), len = query.length, match;
46-
if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
47-
: (match = line.indexOf(query, pos.ch)) != -1)
48-
return {from: {line: pos.line, ch: match},
49-
to: {line: pos.line, ch: match + len}};
50-
};
51-
else
43+
if (target.length == 1) {
44+
if (!query.length) {
45+
// Empty string would match anything and never progress, so
46+
// we define it to match nothing instead.
47+
this.matches = function() {};
48+
} else {
49+
this.matches = function(reverse, pos) {
50+
var line = fold(cm.getLine(pos.line)), len = query.length, match;
51+
if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
52+
: (match = line.indexOf(query, pos.ch)) != -1)
53+
return {from: {line: pos.line, ch: match},
54+
to: {line: pos.line, ch: match + len}};
55+
};
56+
}
57+
} else {
5258
this.matches = function(reverse, pos) {
5359
var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(cm.getLine(ln));
5460
var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
@@ -70,6 +76,7 @@
7076
return {from: reverse ? end : start, to: reverse ? start : end};
7177
}
7278
};
79+
}
7380
}
7481
}
7582

0 commit comments

Comments
 (0)