Skip to content

Commit a6222c7

Browse files
committed
fix: use semantic elements for diff elements (fixes #23)
1 parent 34c4d80 commit a6222c7

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/index.tsx

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {computeLineInformation, DiffInformation, DiffMethod, DiffType, LineInfor
66
import computeStyles, {ReactDiffViewerStyles, ReactDiffViewerStylesOverride,} from './styles';
77
import {ReactElement} from "react";
88
import {computeHiddenBlocks} from "./compute-hidden-blocks";
9+
import IntrinsicElements = React.JSX.IntrinsicElements;
910

1011
const m = require('memoize-one');
1112

@@ -174,17 +175,31 @@ class DiffViewer extends React.Component<
174175
renderer?: (chunk: string) => JSX.Element,
175176
): ReactElement[] => {
176177
return diffArray.map((wordDiff, i): JSX.Element => {
177-
return (
178-
<span
178+
const content = renderer ? renderer(wordDiff.value as string) : wordDiff.value
179+
180+
return wordDiff.type === DiffType.ADDED ?
181+
<ins
179182
key={i}
180183
className={cn(this.styles.wordDiff, {
181184
[this.styles.wordAdded]: wordDiff.type === DiffType.ADDED,
185+
})}
186+
>
187+
{content}
188+
</ins>
189+
: wordDiff.type === DiffType.REMOVED ? <del
190+
key={i}
191+
className={cn(this.styles.wordDiff, {
182192
[this.styles.wordRemoved]: wordDiff.type === DiffType.REMOVED,
183193
})}
184194
>
185-
{renderer ? renderer(wordDiff.value as string) : wordDiff.value}
195+
{content}
196+
</del> : <span
197+
key={i}
198+
className={cn(this.styles.wordDiff)}
199+
>
200+
{content}
186201
</span>
187-
);
202+
;
188203
});
189204
};
190205

@@ -218,16 +233,24 @@ class DiffViewer extends React.Component<
218233
const removed = type === DiffType.REMOVED;
219234
const changed = type === DiffType.CHANGED;
220235
let content;
221-
if (Array.isArray(value)) {
236+
const hasWordDiff = Array.isArray(value)
237+
if (hasWordDiff) {
222238
content = this.renderWordDiff(value, this.props.renderContent);
223239
} else if (this.props.renderContent) {
224240
content = this.props.renderContent(value);
225241
} else {
226242
content = value;
227243
}
228244

245+
let ElementType: keyof IntrinsicElements = 'div'
246+
if (added && !hasWordDiff) {
247+
ElementType = 'ins'
248+
} else if (removed && !hasWordDiff) {
249+
ElementType = 'del'
250+
}
251+
229252
return (
230-
<div className={this.styles.line} role={'row'}>
253+
<ElementType className={this.styles.line} role={'row'} title={added && !hasWordDiff ? "Added line" : removed && !hasWordDiff ? "Removed line" : undefined}>
231254
{!this.props.hideLineNumbers && (
232255
<div
233256
onClick={
@@ -297,7 +320,7 @@ class DiffViewer extends React.Component<
297320
>
298321
<pre className={this.styles.contentText}>{content}</pre>
299322
</div>
300-
</div>
323+
</ElementType>
301324
);
302325
};
303326

@@ -589,17 +612,19 @@ class DiffViewer extends React.Component<
589612
[this.styles.splitView]: splitView,
590613
})}
591614
>
592-
<div className={this.styles.column} role={'table'}>
615+
<div className={this.styles.column} role={'table'} title={`Diff information for ${leftTitle}`}>
593616
<div
594617
className={cn(this.styles.titleBlock, this.styles.column)}
618+
role={'columnheader'}
595619
>
596620
<pre className={this.styles.contentText}>{leftTitle}</pre>
597621
</div>
598622
{nodes.leftLines}
599623
</div>
600-
{nodes.rightLines.length > 0 ? <div className={this.styles.column} role={'table'}>
624+
{nodes.rightLines.length > 0 ? <div className={this.styles.column} role={'table'} title={`Diff information for ${rightTitle}`}>
601625
<div
602626
className={cn(this.styles.titleBlock, this.styles.column)}
627+
role={'columnheader'}
603628
>
604629
<pre className={this.styles.contentText}>{rightTitle}</pre>
605630
</div>

src/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ export default (
383383
label: 'line',
384384
height: '1.6em',
385385
display: 'flex',
386+
textDecoration: 'none'
386387
});
387388

388389
const column = css({

0 commit comments

Comments
 (0)