Skip to content

Commit

Permalink
Highlighting - Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
csavelief committed Feb 5, 2024
1 parent b106ba3 commit 90fb537
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ strings.highlight = function (text, patterns) {
let highlightedSnippet = `${prefix.substring(
begin)}<mark style="border-radius:3px;background:${position.color}">${infix}</mark>${suffix.substring(0, end)}`;
const pages = prefix.split('\f' /* page separator */).map((page, index) => index);
const beginMark = highlightedSnippet.lastIndexOf('<mark');
const beginMark = highlightedSnippet.lastIndexOf('<m');
const endMark = highlightedSnippet.lastIndexOf('</mark>');

if (beginMark && (!endMark || beginMark > endMark)) {
Expand Down
52 changes: 51 additions & 1 deletion src/strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test('highlight', () => {
});
});

test('highlight_overlaps', () => {
test('highlight_overlaps_style', () => {

const text = "Le stationnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx stationner ici.";
const textHighlighted = 'Le <mark style="border-radius:3px;background:#fffec8">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style="border-radius:3px;background:#fffec8">statio</mark>nner ici.';
Expand All @@ -153,6 +153,56 @@ test('highlight_overlaps', () => {
highlightedSnippet: 'Le <mark style="border-radius:3px;background:#fffec8">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx ',
};

expect(strings.highlight(text, [pattern])).toEqual({
snippets: [snippet1, snippet2], text: textHighlighted
});
});

test('highlight_overlaps_begin_tag', () => {

const text = "Le stationnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx stationner ici.";
const textHighlighted = 'Le <mark style="border-radius:3px;background:#fffec8">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style="border-radius:3px;background:#fffec8">statio</mark>nner ici.';
const pattern = {
regexp: /statio/gims, color: "#fffec8"
};
const snippet1 = {
matchedText: "statio",
matchedPage: 1,
rawSnippet: 'x xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx stationner ici.',
highlightedSnippet: 'x xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style=\"border-radius:3px;background:#fffec8\">statio</mark>nner ici.',
};
const snippet2 = {
matchedText: "statio",
matchedPage: 1,
rawSnippet: 'Le stationnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mar',
highlightedSnippet: 'Le <mark style=\"border-radius:3px;background:#fffec8\">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx ',
};

expect(strings.highlight(text, [pattern])).toEqual({
snippets: [snippet1, snippet2], text: textHighlighted
});
});

test('highlight_overlaps_end_tag', () => {

const text = "Le stationnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx stationner ici.";
const textHighlighted = 'Le <mark style="border-radius:3px;background:#fffec8">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style="border-radius:3px;background:#fffec8">statio</mark>nner ici.';
const pattern = {
regexp: /statio/gims, color: "#fffec8"
};
const snippet1 = {
matchedText: "statio",
matchedPage: 1,
rawSnippet: 'x xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx stationner ici.',
highlightedSnippet: 'x xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style=\"border-radius:3px;background:#fffec8\">statio</mark>nner ici.',
};
const snippet2 = {
matchedText: "statio",
matchedPage: 1,
rawSnippet: 'Le stationnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx <mark style=\"border-radius:3px;background:#fffec8\">statio</m',
highlightedSnippet: 'Le <mark style=\"border-radius:3px;background:#fffec8\">statio</mark>nnement est interdit. Merci de ne pas xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx ',
};

expect(strings.highlight(text, [pattern])).toEqual({
snippets: [snippet1, snippet2], text: textHighlighted
});
Expand Down

0 comments on commit 90fb537

Please sign in to comment.