Skip to content

Commit e9cc1b7

Browse files
tkirdatkirda-bisontomi937claude
authored
fix: don't push empty originalQuery onto badQueries (#872)
`isBadQuery` matches by prefix: `q.indexOf(bad) === 0`. An empty entry in `badQueries` matches every subsequent query, silently blocking all ajax requests after the first empty-query response. Reachable with `minChars: 0` plus an empty server response. Guard the push so an empty `originalQuery` never enters the list. Ports szabto's #845 fix forward to the 2.x TypeScript source. Closes #845. Co-authored-by: Tomas Kirda <tomas.kirda@bisoncommerce.com> Co-authored-by: szabto <szabotamas93@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0340799 commit e9cc1b7

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

dist/jquery.autocomplete.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ var _Autocomplete = class _Autocomplete {
583583
result.suggestions = this.verifySuggestionsFormat(result.suggestions);
584584
if (!options.noCache) {
585585
this.cachedResponse[cacheKey] = result;
586-
if (options.preventBadQueries && !result.suggestions.length) {
586+
if (options.preventBadQueries && !result.suggestions.length && originalQuery) {
587587
this.badQueries.push(originalQuery);
588588
}
589589
}

dist/jquery.autocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@
591591
result.suggestions = this.verifySuggestionsFormat(result.suggestions);
592592
if (!options.noCache) {
593593
this.cachedResponse[cacheKey] = result;
594-
if (options.preventBadQueries && !result.suggestions.length) {
594+
if (options.preventBadQueries && !result.suggestions.length && originalQuery) {
595595
this.badQueries.push(originalQuery);
596596
}
597597
}

dist/jquery.autocomplete.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Autocomplete.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,11 @@ export class Autocomplete {
645645

646646
if (!options.noCache) {
647647
this.cachedResponse[cacheKey] = result;
648-
if (options.preventBadQueries && !result.suggestions.length) {
648+
// Guard against pushing an empty `originalQuery`. `isBadQuery`
649+
// matches by prefix (`q.indexOf(bad) === 0`); an empty entry
650+
// would match every subsequent query and silently block all
651+
// ajax requests after the first empty-query response.
652+
if (options.preventBadQueries && !result.suggestions.length && originalQuery) {
649653
this.badQueries.push(originalQuery);
650654
}
651655
}

test/autocomplete.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,44 @@ describe("Autocomplete Async — preventBadQueries", () => {
362362
});
363363
});
364364

365+
describe("Autocomplete Async — empty originalQuery", () => {
366+
it("does not push an empty originalQuery onto badQueries", async () => {
367+
const input = $("<input />");
368+
const serviceUrl = "/autocomplete/empty-bad-query";
369+
let ajaxCount = 0;
370+
371+
input.autocomplete({
372+
serviceUrl,
373+
minChars: 0,
374+
preventBadQueries: true,
375+
});
376+
const instance = input.autocomplete();
377+
378+
$.mockjax({
379+
url: serviceUrl,
380+
responseTime: 1,
381+
response: function () {
382+
ajaxCount += 1;
383+
this.responseText = JSON.stringify({ suggestions: [] });
384+
},
385+
});
386+
387+
// First lookup: empty query with no results.
388+
input.val("");
389+
instance.onValueChange();
390+
await new Promise((r) => setTimeout(r, 30));
391+
392+
// Second lookup: real query — must not be short-circuited by an empty
393+
// prefix sitting in badQueries.
394+
input.val("Jam");
395+
instance.onValueChange();
396+
await new Promise((r) => setTimeout(r, 30));
397+
398+
expect(instance.badQueries).not.toContain("");
399+
expect(ajaxCount).toBe(2);
400+
});
401+
});
402+
365403
describe("Autocomplete", () => {
366404
afterEach(() => {
367405
$(".autocomplete-suggestions").hide();

0 commit comments

Comments
 (0)