Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/viewer/search/search-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import { normalizeSearchText, prepareSearchText } from "./search-text.js";

const segments = new Intl.Segmenter(undefined, { granularity: "grapheme" });

function continuesHyphenatedWord(previous, current) {
return Boolean(previous?.hasEOL &&
/[\p{L}][-\u00AD\u2010]$/u.test(previous.str) &&
/^\p{L}/u.test(current?.str ?? "") &&
!continuesWrappedUrl(previous, current));
}

// Keep source offsets through normalization, including ligatures and collapsed
// whitespace, so counting and painting always refer to the same PDF text.
export function findSearchMatches(items, query) {
const needle = prepareSearchText(normalizeSearchText(query), normalizeSearchText(query));
if (!needle) return [];
const url = /^https?:\/\/\S+$/u.test(normalizeSearchText(query));
const preserveHyphens = /[-\u00AD\u2010]/u.test(needle);
let text = "";
const locations = [];
const removedHyphens = [];
function append(value, location) {
for (const char of value) {
if (/[\s\u200B-\u200D\u2060\uFEFF]/u.test(char)) {
Expand All @@ -24,7 +33,15 @@ export function findSearchMatches(items, query) {
}
}
items.forEach((item, itemIndex) => {
if (itemIndex && !continuesWrappedUrl(items[itemIndex - 1], item)) append(" ", null);
const previous = items[itemIndex - 1];
const wrappedWord = !url && itemIndex > 0 && continuesHyphenatedWord(previous, item);
if (wrappedWord && !preserveHyphens && /[-\u00AD\u2010]$/u.test(text)) {
const location = locations.pop();
text = text.slice(0, -1);
removedHyphens.push({ offset: text.length, location });
} else if (itemIndex && !wrappedWord && !continuesWrappedUrl(previous, item)) {
append(" ", null);
}
for (const { segment, index } of segments.segment(item.str)) {
append(segment.normalize("NFKC").toLocaleLowerCase(), {
itemIndex, start: index, end: index + segment.length,
Expand All @@ -40,6 +57,13 @@ export function findSearchMatches(items, query) {
if (previous?.itemIndex === location.itemIndex) previous.end = location.end;
else ranges.push({ ...location });
}
// A search spanning a soft line-break should also highlight the printed
// hyphen, without changing the PDF's selectable or copied text.
for (const hyphen of removedHyphens) {
if (offset >= hyphen.offset || offset + needle.length <= hyphen.offset) continue;
const sourceRange = ranges.find(range => range.itemIndex === hyphen.location.itemIndex);
if (sourceRange) sourceRange.end = Math.max(sourceRange.end, hyphen.location.end);
}
matches.push({ offset, ordinal: matches.length, ranges });
}
return matches;
Expand Down
51 changes: 51 additions & 0 deletions tests/search-matches.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,54 @@ test("separate normalized matches can refer to the same original ligature", () =
assert.equal(matches.length, 2);
assert.deepEqual(matches[0].ranges, matches[1].ranges);
});

test("finds a word split by a printed hyphen and highlights both pieces", () => {
const items = [
{ str: "Review the account.", hasEOL: true },
{ str: "Please review the ac-", hasEOL: true },
{ str: "count statements.", hasEOL: true },
];
const matches = findSearchMatches(items, "account");

assert.equal(matches.length, 2);
assert.deepEqual(matches[1].ranges, [
{ itemIndex: 1, start: 18, end: 21 },
{ itemIndex: 2, start: 0, end: 5 },
]);
assert.deepEqual(matches[1].ranges.map(range =>
items[range.itemIndex].str.slice(range.start, range.end)), ["ac-", "count"]);
});

test("preserves literal hyphen searches across a printed line break", () => {
const items = [{ str: "risk-", hasEOL: true }, { str: "based" }];
assert.equal(findSearchMatches(items, "risk-based").length, 1);
assert.equal(findSearchMatches(items, "riskbased").length, 1);
});

test("does not join genuine in-line hyphens or unrelated text fragments", () => {
assert.equal(findSearchMatches([{ str: "ac-count" }], "account").length, 0);
assert.equal(findSearchMatches([
{ str: "ac-", hasEOL: false }, { str: "count" },
], "account").length, 0);
assert.equal(findSearchMatches([
{ str: "one", hasEOL: true }, { str: "another" },
], "oneanother").length, 0);
});

test("keeps URL punctuation intact when a URL is wrapped after a hyphen", () => {
const items = [
{ str: "https://example.com/ac-", hasEOL: true },
{ str: "count.pdf" },
];
assert.equal(findSearchMatches(items, "https://example.com/ac-count.pdf").length, 1);
assert.equal(findSearchMatches(items, "https://example.com/account.pdf").length, 0);
});

test("recognizes discretionary soft hyphens while preserving their source offsets", () => {
const items = [{ str: "recom\u00AD", hasEOL: true }, { str: "mend" }];
const [match] = findSearchMatches(items, "recommend");
assert.deepEqual(match.ranges, [
{ itemIndex: 0, start: 0, end: 6 },
{ itemIndex: 1, start: 0, end: 4 },
]);
});
Loading