Repository: mozilla/pdf.js
File: web/pdf_find_controller.js (JavaScript)

Code before:
      }
    } else if (++this.#visitedPagesCount === this._linkService.pagesCount) {
      // For example, in GeckoView we want to have only the final update because
      // the Java side provides only one object to update the counts.
      this.#updateUIResultsCount();
    }
  }

  /**
   * @typedef {Object} FindMatch
   * @property {number} index - The start of the matched text in the page's
   *   string contents.
   * @property {number} length - The length of the matched text.
   */

  /**
   * @param {string | string[]} query - The search query.
   * @param {string} pageContent - The text content of the page to search in.
   * @param {number} pageIndex - The index of the page that is being processed.
   * @returns {FindMatch[] | undefined} An array of matches in the provided
   *   page.
   */
  match(query, pageContent, pageIndex) {
    if (!this.#state) {
      return undefined;
    }
    const hasDiacritics = this._hasDiacritics[pageIndex];

    let isUnicode = false;
    if (typeof query === "string") {
      [isUnicode, query] = this.#convertToRegExpString(query, hasDiacritics);
    } else {
      // Words are sorted in reverse order to be sure that "foobar" is matched
      // before "foo" in case the query is "foobar foo".
      query = query
        .sort()
        .reverse()
        .map(q => {
          const [isUnicodePart, queryPart] = this.#convertToRegExpString(
            q,
            hasDiacritics
          );
          isUnicode ||= isUnicodePart;
          return `(${queryPart})`;
        })
        .join("|");
    }
    if (!query) {
      // The query can be empty because some chars like diacritics could have
      // been stripped out.
      return undefined;

Review comment (suggestion):
Could we stop parsing slightly "earlier" in that case, and not call this method at all?

I've not tested it, but what about the following instead?
```diff
diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js
index 6e87c3149..17eebb22d 100644
--- a/web/pdf_find_controller.js
+++ b/web/pdf_find_controller.js
@@ -772,7 +772,7 @@ class PDFFindController {

   #calculateMatch(pageIndex) {
     const query = this.#query;
-    if (query.length === 0) {
+    if (!this.#state || query.length === 0) {
       return; // Do nothing: the matches should be wiped out already.
     }
     const pageContent = this._pageContents[pageIndex];
```

Code after:
    if (this.#updateMatchesCountOnProgress) {
      if (pageMatchesCount > 0) {
        this.#updateUIResultsCount();
      }
    } else if (++this.#visitedPagesCount === this._linkService.pagesCount) {
      // For example, in GeckoView we want to have only the final update because
      // the Java side provides only one object to update the counts.
      this.#updateUIResultsCount();
    }
  }

  /**
   * @typedef {Object} FindMatch
   * @property {number} index - The start of the matched text in the page's
   *   string contents.
   * @property {number} length - The length of the matched text.
   */

  /**
   * @param {string | string[]} query - The search query.
   * @param {string} pageContent - The text content of the page to search in.
   * @param {number} pageIndex - The index of the page that is being processed.
   * @returns {FindMatch[] | undefined} An array of matches in the provided
   *   page.
   */
  match(query, pageContent, pageIndex) {
    const hasDiacritics = this._hasDiacritics[pageIndex];

    let isUnicode = false;
    if (typeof query === "string") {
      [isUnicode, query] = this.#convertToRegExpString(query, hasDiacritics);
    } else {
      // Words are sorted in reverse order to be sure that "foobar" is matched
      // before "foo" in case the query is "foobar foo".
      query = query
        .sort()
        .reverse()
        .map(q => {
          const [isUnicodePart, queryPart] = this.#convertToRegExpString(
            q,
            hasDiacritics
          );
          isUnicode ||= isUnicodePart;
          return `(${queryPart})`;
        })
        .join("|");
    }
    if (!query) {
      // The query can be empty because some chars like diacritics could have
      // been stripped out.
      return undefined;