Skip to content

Commit

Permalink
strings - While highlighting text, ensure the raw text length is equa…
Browse files Browse the repository at this point in the history
…l to the preprocessed text length.
  • Loading branch information
csavelief committed Nov 15, 2023
1 parent 976252f commit f3ae94b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
5 changes: 4 additions & 1 deletion dist/cjs/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cjs/main.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion dist/esm/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main.min.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ strings.highlight = function (text, patterns) {

let highlightedText = text;
text = strings.removeDiacritics(text, true);

if (text.length !== highlightedText.length) {
highlightedText = text;
}
const highlights = patterns.flatMap(pattern => {

const matcher = pattern.regexp;
Expand Down Expand Up @@ -291,7 +295,6 @@ strings.highlight = function (text, patterns) {
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);

// matchedText, rawSnippet, highlightedSnippet, page
return new strings.Highlight(infix, pages.length, rawSnippet, highlightedSnippet);
});
return new strings.HighlightedText(highlightedText, highlights);
Expand Down
24 changes: 23 additions & 1 deletion src/strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,26 @@ test('highlight', () => {
expect(strings.highlight(text, [pattern])).toEqual({
snippets: [snippet], text: textHighlighted
});
})

const text2 = 'Motif n°1: aabbcc.\fMotif n°2: aabbcc.';
const textHighlighted2 = 'Motif n°1: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.\fMotif n°2: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.';
const pattern2 = {
regexp: /aabbcc/gims, color: "#fffec8"
};
const snippet2 = {
matchedText: "aabbcc",
matchedPage: 1,
rawSnippet: 'Motif n°1: aabbcc.\fMotif n°2: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.',
highlightedSnippet: 'Motif n°1: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.\fMotif n°2: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.',
};
const snippet3 = {
matchedText: "aabbcc",
matchedPage: 2,
rawSnippet: 'Motif n°1: aabbcc.\fMotif n°2: aabbcc.',
highlightedSnippet: 'Motif n°1: aabbcc.\fMotif n°2: <mark style="border-radius:3px;background:#fffec8">aabbcc</mark>.',
};

expect(strings.highlight(text2, [pattern2])).toEqual({
snippets: [snippet3, snippet2], text: textHighlighted2
});
});

0 comments on commit f3ae94b

Please sign in to comment.