fix(find): respect spaces when query has no whitespace

This commit is contained in:
Shree 2025-10-14 18:13:40 +05:30
parent 30fdf16071
commit 2bcfd01784

View File

@ -708,6 +708,11 @@ class PDFFindController {
#convertToRegExpString(query, hasDiacritics) { #convertToRegExpString(query, hasDiacritics) {
const { matchDiacritics } = this.#state; const { matchDiacritics } = this.#state;
let isUnicode = false; let isUnicode = false;
const rawQuery = this._rawQuery ?? this.#state?.query;
const queryHasWhitespace =
typeof rawQuery === "string"
? /\s/.test(rawQuery)
: Array.isArray(rawQuery) && rawQuery.some(q => /\s/.test(q));
query = query.replaceAll( query = query.replaceAll(
SPECIAL_CHARS_REG_EXP, SPECIAL_CHARS_REG_EXP,
( (
@ -718,16 +723,16 @@ class PDFFindController {
p4 /* diacritics */, p4 /* diacritics */,
p5 /* letters */ p5 /* letters */
) => { ) => {
// We don't need to use a \s for whitespaces since all the different // We don't need \s because all whitespace is normalized to a single " ".
// kind of whitespaces are replaced by a single " ".
if (p1) { if (p1) {
// Escape characters like *+?... to not interfere with regexp syntax. // Escaped metacharacters like . * + ? ...
return `[ ]*\\${p1}[ ]*`; // Allow spaces around them ONLY if the user typed spaces.
return queryHasWhitespace ? `[ ]*\\${p1}[ ]*` : `\\${p1}`;
} }
if (p2) { if (p2) {
// Allow whitespaces around punctuation signs. // Punctuation: allow optional spaces ONLY if the user typed spaces.
return `[ ]*${p2}[ ]*`; return queryHasWhitespace ? `[ ]*${p2}[ ]*` : `${p2}`;
} }
if (p3) { if (p3) {
// Replace spaces by \s+ to be sure to match any spaces. // Replace spaces by \s+ to be sure to match any spaces.
@ -906,7 +911,6 @@ class PDFFindController {
.then( .then(
textContent => { textContent => {
const strBuf = []; const strBuf = [];
for (const textItem of textContent.items) { for (const textItem of textContent.items) {
strBuf.push(textItem.str); strBuf.push(textItem.str);
if (textItem.hasEOL) { if (textItem.hasEOL) {