Skip to content

Commit

Permalink
Fix search terms for " and "" (mozilla#3017)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli authored Nov 10, 2023
1 parent b2d4cbc commit 20de532
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions translate/src/modules/placeable/components/Highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ describe('<Highlight search>', () => {
expect(marks.at(0).hasClass('search'));
expect(marks.at(0).text()).toEqual('456');
});

it('does not break for lone " quotes', () => {
const wrapper = mountMarker('123456"', [], '"');
const marks = wrapper.find('mark');
expect(marks).toHaveLength(2);
expect(marks.at(1).text()).toEqual('"');
});

it('does not break for doubled "" quotes', () => {
const wrapper = mountMarker('123456""', [], '""');
const marks = wrapper.find('mark');
expect(marks).toHaveLength(2);
expect(marks.at(1).text()).toEqual('""');
});
});

describe('specific marker', () => {
Expand Down
4 changes: 2 additions & 2 deletions translate/src/modules/placeable/components/Highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ export function Highlight({
if (search) {
const searchTerms = search.match(/(?<!\\)"(?:\\"|[^"])+(?<!\\)"|\S+/g);
for (let term of searchTerms ?? []) {
if (term.startsWith('"') && term.endsWith('"')) {
if (term.startsWith('"') && term.length >= 3 && term.endsWith('"')) {
term = term.slice(1, -1);
}
let lcTerm = term.toLowerCase();
let pos = 0;
let next: number;
while ((next = lcSource.indexOf(lcTerm, pos)) !== -1) {
let i = marks.findIndex((m) => m.index + m.length >= next);
let i = marks.findIndex((m) => m.index + m.length > next);
if (i === -1) {
i = marks.length;
}
Expand Down

0 comments on commit 20de532

Please sign in to comment.