Skip to content

Commit 36799b9

Browse files
committedFeb 24, 2020
SideBySide shows a stripe for nonexistent lines
This makes nonexistent lines more distinguishable so we no longer let inserted/deleted/equal lines be across column spans. This should be more consistent to most diff softwares. Signed-off-by: Jack Cherng <[email protected]>
1 parent 6b7e36b commit 36799b9

File tree

6 files changed

+35
-60
lines changed

6 files changed

+35
-60
lines changed
 

‎README.md

-9
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,6 @@ $result = $htmlRenderer->renderArray(json_decode($jsonResult, true));
170170
![Side By Side](https://raw.githubusercontent.com/jfcherng/php-diff/v6/example/images/side-by-side-renderer.png)
171171

172172

173-
### Renderer: Side By Side (no line numbers)
174-
175-
```php
176-
<?php $rendererOptions = ['lineNumbers' => false];
177-
```
178-
179-
![Side By Side](https://raw.githubusercontent.com/jfcherng/php-diff/v6/example/images/side-by-side-renderer-line-numbers-false.png)
180-
181-
182173
### Renderer: Unified
183174

184175
```diff

‎example/diff-table.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
}
7979
.diff-wrapper.diff.diff-html .change.change-ins .old, .diff-wrapper.diff.diff-html .change.change-del .new {
8080
cursor: not-allowed;
81-
background: #ebebeb;
81+
background: repeating-linear-gradient(-45deg, #ebebeb, #ebebeb 5px, #e0e0e0 5px, #e0e0e0 10px);
8282
}
8383
.diff-wrapper.diff.diff-html .change .old {
8484
background: #fbe1e1;

‎example/diff-table.scss

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// color for the nonexistent block
3030
// for example, there are a deleted line that has no corresponding one
3131
$bg-color-nonexistent-block: mix($bg-color, $table-sidebar-color, 60%);
32+
$bg-color-nonexistent-block-alternative: mix($bg-color, $table-sidebar-color, 40%);
3233

3334
background-color: $bg-color;
3435
border-collapse: collapse;
@@ -121,7 +122,13 @@
121122
&.change-ins .old,
122123
&.change-del .new {
123124
cursor: not-allowed;
124-
background: $bg-color-nonexistent-block;
125+
background: repeating-linear-gradient(
126+
-45deg,
127+
$bg-color-nonexistent-block,
128+
$bg-color-nonexistent-block 5px,
129+
$bg-color-nonexistent-block-alternative 5px,
130+
$bg-color-nonexistent-block-alternative 10px,
131+
);
125132
}
126133

127134
.old {
Binary file not shown.
194 Bytes
Loading

‎src/Renderer/Html/SideBySide.php

+26-49
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,18 @@ protected function renderTableBlockEqual(array $block): string
126126
// the old and the new may not be exactly the same
127127
// because of ignoreCase, ignoreWhitespace, etc
128128
foreach ($block['new']['lines'] as $no => $newLine) {
129-
if ($this->options['lineNumbers']) {
130-
$oldLine = $block['old']['lines'][$no];
129+
$oldLine = $block['old']['lines'][$no];
131130

132-
$oldLineNum = $block['old']['offset'] + $no + 1;
133-
$newLineNum = $block['new']['offset'] + $no + 1;
131+
$oldLineNum = $block['old']['offset'] + $no + 1;
132+
$newLineNum = $block['new']['offset'] + $no + 1;
134133

135-
$html .=
136-
'<tr>' .
137-
$this->renderLineNumberColumn('old', $oldLineNum) .
138-
'<td class="old">' . $oldLine . '</td>' .
139-
$this->renderLineNumberColumn('new', $newLineNum) .
140-
'<td class="new">' . $newLine . '</td>' .
141-
'</tr>';
142-
} else {
143-
// hmm... but there is only space for one line
144-
// we could only pick either the old or the new to show
145-
$html .=
146-
'<tr>' .
147-
'<td class="new" colspan="2">' . $newLine . '</td>' .
148-
'</tr>';
149-
}
134+
$html .=
135+
'<tr>' .
136+
$this->renderLineNumberColumn('old', $oldLineNum) .
137+
'<td class="old">' . $oldLine . '</td>' .
138+
$this->renderLineNumberColumn('new', $newLineNum) .
139+
'<td class="new">' . $newLine . '</td>' .
140+
'</tr>';
150141
}
151142

152143
return $html;
@@ -162,22 +153,15 @@ protected function renderTableBlockInsert(array $block): string
162153
$html = '';
163154

164155
foreach ($block['new']['lines'] as $no => $newLine) {
165-
if ($this->options['lineNumbers']) {
166-
$newLineNum = $block['new']['offset'] + $no + 1;
156+
$newLineNum = $block['new']['offset'] + $no + 1;
167157

168-
$html .=
169-
'<tr>' .
170-
$this->renderLineNumberColumn('', null) .
171-
'<td class="old"></td>' .
172-
$this->renderLineNumberColumn('new', $newLineNum) .
173-
'<td class="new">' . $newLine . '</td>' .
174-
'</tr>';
175-
} else {
176-
$html .=
177-
'<tr>' .
178-
'<td class="new" colspan="2">' . $newLine . '</td>' .
179-
'</tr>';
180-
}
158+
$html .=
159+
'<tr>' .
160+
$this->renderLineNumberColumn('', null) .
161+
'<td class="old"></td>' .
162+
$this->renderLineNumberColumn('new', $newLineNum) .
163+
'<td class="new">' . $newLine . '</td>' .
164+
'</tr>';
181165
}
182166

183167
return $html;
@@ -193,22 +177,15 @@ protected function renderTableBlockDelete(array $block): string
193177
$html = '';
194178

195179
foreach ($block['old']['lines'] as $no => $oldLine) {
196-
if ($this->options['lineNumbers']) {
197-
$oldLineNum = $block['old']['offset'] + $no + 1;
180+
$oldLineNum = $block['old']['offset'] + $no + 1;
198181

199-
$html .=
200-
'<tr>' .
201-
$this->renderLineNumberColumn('old', $oldLineNum) .
202-
'<td class="old">' . $oldLine . '</td>' .
203-
$this->renderLineNumberColumn('', null) .
204-
'<td class="new"></td>' .
205-
'</tr>';
206-
} else {
207-
$html .=
208-
'<tr>' .
209-
'<td class="old" colspan="2">' . $oldLine . '</td>' .
210-
'</tr>';
211-
}
182+
$html .=
183+
'<tr>' .
184+
$this->renderLineNumberColumn('old', $oldLineNum) .
185+
'<td class="old">' . $oldLine . '</td>' .
186+
$this->renderLineNumberColumn('', null) .
187+
'<td class="new"></td>' .
188+
'</tr>';
212189
}
213190

214191
return $html;

0 commit comments

Comments
 (0)