Skip to content

Commit

Permalink
fix: support overlapping highlights in HighlightUtils.ts (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar and louis-bompart authored May 10, 2024
1 parent c769a3b commit 7876f7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/utils/HighlightUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,19 @@ export class HighlightUtils {
break;
}

highlighted += _.escape(content.slice(last, start));
highlighted += `<span class="${cssClass}"`;
if (highlight.dataHighlightGroup) {
highlighted += ` data-highlight-group="${highlight.dataHighlightGroup.toString()}"`;
}
if (highlight.dataHighlightGroupTerm) {
highlighted += ` data-highlight-group-term="${highlight.dataHighlightGroupTerm}"`;
if (last <= start) {
highlighted += _.escape(content.slice(last, start));
highlighted += `<span class="${cssClass}"`;
if (highlight.dataHighlightGroup) {
highlighted += ` data-highlight-group="${highlight.dataHighlightGroup.toString()}"`;
}
if (highlight.dataHighlightGroupTerm) {
highlighted += ` data-highlight-group-term="${highlight.dataHighlightGroupTerm}"`;
}
highlighted += '>';
highlighted += _.escape(content.slice(start, end));
highlighted += '</span>';
}
highlighted += '>';
highlighted += _.escape(content.slice(start, end));
highlighted += '</span>';

last = end;
}
if (last != maxIndex) {
Expand Down
11 changes: 11 additions & 0 deletions unitTests/utils/HighlightUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export function HighlightUtilsTest() {
const localPath = 'C:\\Programmes\\Ces\\config\\sources\\salesforce';

describe('highlightString', function () {
it('should handle highlights that overlap', function () {
let highlights: IHighlight[] = [
{ offset: 9, length: 19 },
{ offset: 16, length: 12 },
{ offset: 18, length: 15 }
];
let expectedHighlight =
'Lorem ips<span class="coveo-highlight">um dolor sit amet, </span>ctetur adipisicing elit, sed do eiusmod tempor incididunt ut';
expect(HighlightUtils.highlightString(lorem, highlights, null, 'coveo-highlight')).toBe(expectedHighlight);
});

it('should wrap the passed highlights with tags using the specified class name', function () {
let highlights: IHighlight[] = [
{ offset: 3, length: 5 },
Expand Down

0 comments on commit 7876f7f

Please sign in to comment.