mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-06-27 02:25:47 +02:00
Escape punctuation signs in search query
It fixes #20516. The chars `*{}()[]\` are punctuation signs in regex, so they need to be escaped when searching for them literally.
This commit is contained in:
parent
f7f963ef97
commit
923a778d26
1
test/pdfs/.gitignore
vendored
1
test/pdfs/.gitignore
vendored
@ -765,3 +765,4 @@
|
|||||||
!two_paragraphs.pdf
|
!two_paragraphs.pdf
|
||||||
!paragraph_and_link.pdf
|
!paragraph_and_link.pdf
|
||||||
!issue20225.pdf
|
!issue20225.pdf
|
||||||
|
!issue20516.pdf
|
||||||
|
|||||||
BIN
test/pdfs/issue20516.pdf
Normal file
BIN
test/pdfs/issue20516.pdf
Normal file
Binary file not shown.
@ -1158,6 +1158,26 @@ describe("pdf_find_controller", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("performs a search with a group of punctuation signs to escape", async () => {
|
||||||
|
const { eventBus, pdfFindController } =
|
||||||
|
await initPdfFindController("issue20516.pdf");
|
||||||
|
|
||||||
|
await testSearch({
|
||||||
|
eventBus,
|
||||||
|
pdfFindController,
|
||||||
|
state: {
|
||||||
|
query: `("client")`,
|
||||||
|
},
|
||||||
|
matchesPerPage: [1],
|
||||||
|
selectedMatch: {
|
||||||
|
pageIndex: 0,
|
||||||
|
matchIndex: 0,
|
||||||
|
},
|
||||||
|
pageMatches: [[6]],
|
||||||
|
pageMatchesLength: [[10]],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("custom matcher", () => {
|
describe("custom matcher", () => {
|
||||||
it("calls to the matcher with the right arguments", async () => {
|
it("calls to the matcher with the right arguments", async () => {
|
||||||
const QUERY = "Foo bar";
|
const QUERY = "Foo bar";
|
||||||
|
|||||||
@ -77,8 +77,8 @@ const DIACRITICS_EXCEPTION = new Set([
|
|||||||
let DIACRITICS_EXCEPTION_STR; // Lazily initialized, see below.
|
let DIACRITICS_EXCEPTION_STR; // Lazily initialized, see below.
|
||||||
|
|
||||||
const DIACRITICS_REG_EXP = /\p{M}+/gu;
|
const DIACRITICS_REG_EXP = /\p{M}+/gu;
|
||||||
const SPECIAL_CHARS_REG_EXP =
|
const SPECIAL_PUNCTUATION_CHARACTERS = /[.?*{}()[\]\\]/g;
|
||||||
/([*+^${}()|[\]\\])|(\p{P}+)|(\s+)|(\p{M})|(\p{L})/gu;
|
const SPECIAL_CHARS_REG_EXP = /([+^$|])|(\p{P}+)|(\s+)|(\p{M})|(\p{L})/gu;
|
||||||
const NOT_DIACRITIC_FROM_END_REG_EXP = /([^\p{M}])\p{M}*$/u;
|
const NOT_DIACRITIC_FROM_END_REG_EXP = /([^\p{M}])\p{M}*$/u;
|
||||||
const NOT_DIACRITIC_FROM_START_REG_EXP = /^\p{M}*([^\p{M}])/u;
|
const NOT_DIACRITIC_FROM_START_REG_EXP = /^\p{M}*([^\p{M}])/u;
|
||||||
|
|
||||||
@ -739,7 +739,10 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
if (p2) {
|
if (p2) {
|
||||||
// Allow whitespaces around group of punctuation signs.
|
// Allow whitespaces around group of punctuation signs.
|
||||||
return addExtraWhitespaces(p2, p2.replaceAll(/[.?]/g, "\\$&"));
|
return addExtraWhitespaces(
|
||||||
|
p2,
|
||||||
|
p2.replaceAll(SPECIAL_PUNCTUATION_CHARACTERS, "\\$&")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user