Skip to content

Commit 509fcc8

Browse files
Merge pull request #20518 from calixteman/issue20516
Escape punctuation signs in search query
2 parents bdf5005 + 923a778 commit 509fcc8

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,4 @@
765765
!two_paragraphs.pdf
766766
!paragraph_and_link.pdf
767767
!issue20225.pdf
768+
!issue20516.pdf

test/pdfs/issue20516.pdf

8.37 KB
Binary file not shown.

test/unit/pdf_find_controller_spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,26 @@ describe("pdf_find_controller", function () {
11581158
});
11591159
});
11601160

1161+
it("performs a search with a group of punctuation signs to escape", async () => {
1162+
const { eventBus, pdfFindController } =
1163+
await initPdfFindController("issue20516.pdf");
1164+
1165+
await testSearch({
1166+
eventBus,
1167+
pdfFindController,
1168+
state: {
1169+
query: `("client")`,
1170+
},
1171+
matchesPerPage: [1],
1172+
selectedMatch: {
1173+
pageIndex: 0,
1174+
matchIndex: 0,
1175+
},
1176+
pageMatches: [[6]],
1177+
pageMatchesLength: [[10]],
1178+
});
1179+
});
1180+
11611181
describe("custom matcher", () => {
11621182
it("calls to the matcher with the right arguments", async () => {
11631183
const QUERY = "Foo bar";

web/pdf_find_controller.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ const DIACRITICS_EXCEPTION = new Set([
7777
let DIACRITICS_EXCEPTION_STR; // Lazily initialized, see below.
7878

7979
const DIACRITICS_REG_EXP = /\p{M}+/gu;
80-
const SPECIAL_CHARS_REG_EXP =
81-
/([*+^${}()|[\]\\])|(\p{P}+)|(\s+)|(\p{M})|(\p{L})/gu;
80+
const SPECIAL_PUNCTUATION_CHARACTERS = /[.?*{}()[\]\\]/g;
81+
const SPECIAL_CHARS_REG_EXP = /([+^$|])|(\p{P}+)|(\s+)|(\p{M})|(\p{L})/gu;
8282
const NOT_DIACRITIC_FROM_END_REG_EXP = /([^\p{M}])\p{M}*$/u;
8383
const NOT_DIACRITIC_FROM_START_REG_EXP = /^\p{M}*([^\p{M}])/u;
8484

@@ -739,7 +739,10 @@ class PDFFindController {
739739
}
740740
if (p2) {
741741
// Allow whitespaces around group of punctuation signs.
742-
return addExtraWhitespaces(p2, p2.replaceAll(/[.?]/g, "\\$&"));
742+
return addExtraWhitespaces(
743+
p2,
744+
p2.replaceAll(SPECIAL_PUNCTUATION_CHARACTERS, "\\$&")
745+
);
743746
}
744747
if (p3) {
745748
// Replace spaces by \s+ to be sure to match any spaces.

0 commit comments

Comments
 (0)