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
27 changes: 27 additions & 0 deletions translate/src/modules/placeable/components/Highlight.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ describe('mark terms', () => {
expect(container.querySelector('mark')).toBeNull();
});

it('does not mark a term followed by _', () => {
const string = 'foo_bar text';
const terms = {
terms: [{ text: 'foo' }],
};

const { container } = mountMarker(string, terms);

expect(container.querySelector('mark')).toBeNull();
});

it.each(['[', '\\', ']', '^', '`'])(
'does not extend a term match past %s',
(char) => {
const string = `foo${char}bar text`;
const terms = {
terms: [{ text: 'foo' }],
};

const { container } = mountMarker(string, terms);
const marks = container.querySelectorAll('mark.term');

expect(marks).toHaveLength(1);
expect(marks[0].textContent).toBe('foo');
},
);

it('marks longer terms first', () => {
const string = 'This is a translation tool.';
const terms = {
Expand Down
2 changes: 1 addition & 1 deletion translate/src/modules/placeable/components/Highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function Highlight({
.map((t) => t.text)
.sort((a, b) => (a.length < b.length ? 1 : -1));
for (const term of sourceTerms) {
const re = new RegExp(`\\b${escapeRegExp(term)}[a-zA-z]*\\b`, 'gi');
const re = new RegExp(`\\b${escapeRegExp(term)}[a-zA-Z]*\\b`, 'gi');
for (const match of source.matchAll(re)) {
marks.push({
index: match.index ?? -1,
Expand Down