diff --git a/test/unit/pdf_find_controller_spec.js b/test/unit/pdf_find_controller_spec.js index 271a758f0..bdb6ffd6e 100644 --- a/test/unit/pdf_find_controller_spec.js +++ b/test/unit/pdf_find_controller_spec.js @@ -13,11 +13,11 @@ * limitations under the License. */ +import { FindState, PDFFindController } from "../../web/pdf_find_controller.js"; import { buildGetDocumentParams } from "./test_utils.js"; import { EventBus } from "../../web/event_utils.js"; import { getDocument } from "../../src/display/api.js"; import { isNodeJS } from "../../src/shared/is_node.js"; -import { PDFFindController } from "../../web/pdf_find_controller.js"; import { SimpleLinkService } from "../../web/pdf_link_service.js"; const tracemonkeyFileName = "tracemonkey.pdf"; @@ -156,6 +156,38 @@ function testSearch({ }); } +function testEmptySearch({ eventBus, pdfFindController, state }) { + return new Promise(function (resolve) { + const eventState = Object.assign( + Object.create(null), + { + source: this, + type: "", + query: null, + caseSensitive: false, + entireWord: false, + phraseSearch: true, + findPrevious: false, + matchDiacritics: false, + }, + state + ); + eventBus.dispatch("find", eventState); + + eventBus.on( + "updatefindcontrolstate", + function onUpdatefindcontrolstate(evt) { + if (evt.state !== FindState.NOT_FOUND) { + return; + } + eventBus.off("updatefindcontrolstate", onUpdatefindcontrolstate); + expect(evt.matchesCount.total).toBe(0); + resolve(); + } + ); + }); +} + describe("pdf_find_controller", function () { it("performs a normal search", async function () { const { eventBus, pdfFindController } = await initPdfFindController(); @@ -689,4 +721,16 @@ describe("pdf_find_controller", function () { pageMatchesLength: [[5]], }); }); + + it("performs a search with a single diacritic", async function () { + const { eventBus, pdfFindController } = await initPdfFindController(); + + await testEmptySearch({ + eventBus, + pdfFindController, + state: { + query: "\u064E", + }, + }); + }); }); diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 3dab4fa89..0ea48f124 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -591,9 +591,13 @@ class PDFFindController { } #calculateRegExpMatch(query, entireWord, pageIndex, pageContent) { - const matches = [], - matchesLength = []; - + const matches = (this._pageMatches[pageIndex] = []); + const matchesLength = (this._pageMatchesLength[pageIndex] = []); + if (!query) { + // The query can be empty because some chars like diacritics could have + // been stripped out. + return; + } const diffs = this._pageDiffs[pageIndex]; let match; while ((match = query.exec(pageContent)) !== null) { @@ -615,8 +619,6 @@ class PDFFindController { matchesLength.push(matchLen); } } - this._pageMatches[pageIndex] = matches; - this._pageMatchesLength[pageIndex] = matchesLength; } #convertToRegExpString(query, hasDiacritics) { @@ -691,7 +693,7 @@ class PDFFindController { #calculateMatch(pageIndex) { let query = this.#query; - if (query.length === 0) { + if (!query) { // Do nothing: the matches should be wiped out already. return; } @@ -724,7 +726,7 @@ class PDFFindController { } const flags = `g${isUnicode ? "u" : ""}${caseSensitive ? "" : "i"}`; - query = new RegExp(query, flags); + query = query ? new RegExp(query, flags) : null; this.#calculateRegExpMatch(query, entireWord, pageIndex, pageContent); @@ -854,7 +856,7 @@ class PDFFindController { } // If there's no query there's no point in searching. - if (this.#query === "") { + if (!this.#query) { this.#updateUIState(FindState.FOUND); return; }