Skip to content

Commit e170ca5

Browse files
authored
Highlight placeables in diff view (mozilla#4007)
Fixes mozilla#2714 When toggling the diff view in the history panel, placeables were not being highlighted. This happened because TranslationDiff rendered raw text slices from diff match patch bypassing the highlight component that marks up placeables. This wraps each diff slice with highlight so that the placeables are marked whether or not the diff view is active
1 parent b035ab6 commit e170ca5

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

translate/src/modules/diff/components/TranslationDiff.test.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
22

33
import { TranslationDiff } from './TranslationDiff';
4+
import { MockLocalizationProvider } from '~/test/utils';
5+
import { mount } from 'enzyme';
46
import { render } from '@testing-library/react';
57

68
describe('<TranslationDiff>', () => {
@@ -23,4 +25,16 @@ describe('<TranslationDiff>', () => {
2325
expect(container.querySelector('del')).toBeNull();
2426
expect(container).toHaveTextContent(/^abcdef$/);
2527
});
28+
29+
it('highlights placeables in diff slices', () => {
30+
const wrapper = mount(
31+
<MockLocalizationProvider>
32+
<TranslationDiff
33+
base={'Delavci { -brand-short-name } posodobljeno'}
34+
target={'Delavci { -brand-short-name } posodobljeno'}
35+
/>
36+
</MockLocalizationProvider>,
37+
);
38+
expect(wrapper.find('mark.placeable').length).toBeGreaterThan(0);
39+
});
2640
});

translate/src/modules/diff/components/TranslationDiff.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { diff_match_patch, DIFF_INSERT, DIFF_DELETE } from 'diff-match-patch';
22
import React from 'react';
3-
3+
import { Highlight } from '~/modules/placeable/components/Highlight';
44
import './TranslationDiff.css';
55

66
const dmp = new diff_match_patch();
@@ -27,13 +27,21 @@ export function TranslationDiff({ base, target }: Props): React.ReactElement {
2727
{diff.map(([type, slice], index) => {
2828
switch (type) {
2929
case DIFF_INSERT:
30-
return <ins key={index}>{slice}</ins>;
30+
return (
31+
<ins key={index}>
32+
<Highlight>{slice}</Highlight>
33+
</ins>
34+
);
3135

3236
case DIFF_DELETE:
33-
return <del key={index}>{slice}</del>;
37+
return (
38+
<del key={index}>
39+
<Highlight>{slice}</Highlight>
40+
</del>
41+
);
3442

3543
default:
36-
return slice;
44+
return <Highlight key={index}>{slice}</Highlight>;
3745
}
3846
})}
3947
</>

0 commit comments

Comments
 (0)