Avoid an infinite loop when searching for a single diacritic
This commit is contained in:
parent
1e3e2defe4
commit
69c88477a9
@ -13,11 +13,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { FindState, PDFFindController } from "../../web/pdf_find_controller.js";
|
||||||
import { buildGetDocumentParams } from "./test_utils.js";
|
import { buildGetDocumentParams } from "./test_utils.js";
|
||||||
import { EventBus } from "../../web/event_utils.js";
|
import { EventBus } from "../../web/event_utils.js";
|
||||||
import { getDocument } from "../../src/display/api.js";
|
import { getDocument } from "../../src/display/api.js";
|
||||||
import { isNodeJS } from "../../src/shared/is_node.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";
|
import { SimpleLinkService } from "../../web/pdf_link_service.js";
|
||||||
|
|
||||||
const tracemonkeyFileName = "tracemonkey.pdf";
|
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 () {
|
describe("pdf_find_controller", function () {
|
||||||
it("performs a normal search", async function () {
|
it("performs a normal search", async function () {
|
||||||
const { eventBus, pdfFindController } = await initPdfFindController();
|
const { eventBus, pdfFindController } = await initPdfFindController();
|
||||||
@ -689,4 +721,16 @@ describe("pdf_find_controller", function () {
|
|||||||
pageMatchesLength: [[5]],
|
pageMatchesLength: [[5]],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("performs a search with a single diacritic", async function () {
|
||||||
|
const { eventBus, pdfFindController } = await initPdfFindController();
|
||||||
|
|
||||||
|
await testEmptySearch({
|
||||||
|
eventBus,
|
||||||
|
pdfFindController,
|
||||||
|
state: {
|
||||||
|
query: "\u064E",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -591,9 +591,13 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#calculateRegExpMatch(query, entireWord, pageIndex, pageContent) {
|
#calculateRegExpMatch(query, entireWord, pageIndex, pageContent) {
|
||||||
const matches = [],
|
const matches = (this._pageMatches[pageIndex] = []);
|
||||||
matchesLength = [];
|
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];
|
const diffs = this._pageDiffs[pageIndex];
|
||||||
let match;
|
let match;
|
||||||
while ((match = query.exec(pageContent)) !== null) {
|
while ((match = query.exec(pageContent)) !== null) {
|
||||||
@ -615,8 +619,6 @@ class PDFFindController {
|
|||||||
matchesLength.push(matchLen);
|
matchesLength.push(matchLen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._pageMatches[pageIndex] = matches;
|
|
||||||
this._pageMatchesLength[pageIndex] = matchesLength;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#convertToRegExpString(query, hasDiacritics) {
|
#convertToRegExpString(query, hasDiacritics) {
|
||||||
@ -691,7 +693,7 @@ class PDFFindController {
|
|||||||
|
|
||||||
#calculateMatch(pageIndex) {
|
#calculateMatch(pageIndex) {
|
||||||
let query = this.#query;
|
let query = this.#query;
|
||||||
if (query.length === 0) {
|
if (!query) {
|
||||||
// Do nothing: the matches should be wiped out already.
|
// Do nothing: the matches should be wiped out already.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -724,7 +726,7 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const flags = `g${isUnicode ? "u" : ""}${caseSensitive ? "" : "i"}`;
|
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);
|
this.#calculateRegExpMatch(query, entireWord, pageIndex, pageContent);
|
||||||
|
|
||||||
@ -854,7 +856,7 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If there's no query there's no point in searching.
|
// If there's no query there's no point in searching.
|
||||||
if (this.#query === "") {
|
if (!this.#query) {
|
||||||
this.#updateUIState(FindState.FOUND);
|
this.#updateUIState(FindState.FOUND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user