From 16b0a7fe47951ff8a7545e325da2d8a48566637e Mon Sep 17 00:00:00 2001 From: baha-bouali Date: Sat, 5 Sep 2026 15:59:11 +0100 Subject: [PATCH] add regex testing for non-alpha characters --- .../placeable/components/Highlight.test.jsx | 27 +++++++++++++++++++ .../placeable/components/Highlight.tsx | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/translate/src/modules/placeable/components/Highlight.test.jsx b/translate/src/modules/placeable/components/Highlight.test.jsx index 82feedad71..1d9f1e16d4 100644 --- a/translate/src/modules/placeable/components/Highlight.test.jsx +++ b/translate/src/modules/placeable/components/Highlight.test.jsx @@ -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 = { diff --git a/translate/src/modules/placeable/components/Highlight.tsx b/translate/src/modules/placeable/components/Highlight.tsx index 258f6b966b..22f7db722e 100644 --- a/translate/src/modules/placeable/components/Highlight.tsx +++ b/translate/src/modules/placeable/components/Highlight.tsx @@ -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,