Skip to content

Commit 37bd345

Browse files
committed
Fix problem caused by in OP_EQ, it still may be $old !== $new
Two different line could be marked as the same, because of ignoreCase, ignoreWhitespace, etc. Signed-off-by: Jack Cherng <[email protected]>
1 parent a821017 commit 37bd345

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,13 @@ public function getChanges(): array
7777
$lastTag = $tag;
7878

7979
if ($tag === SequenceMatcher::OP_EQ) {
80-
/**
81-
* @todo Technically this is wrong.
82-
* The old and the new may not be exactly the same
83-
* because of ignoreCase and ignoreWhitespace.
84-
*/
85-
if (!empty($lines = \array_slice($old, $i1, $i2 - $i1))) {
86-
$formattedLines = $this->formatLines($lines);
87-
88-
$blocks[$lastBlock]['base']['lines'] += $formattedLines;
89-
$blocks[$lastBlock]['changed']['lines'] += $formattedLines;
90-
}
80+
// note that although we are in a OP_EQ situation,
81+
// the old and the new may not be exactly the same
82+
// because of ignoreCase, ignoreWhitespace, etc
83+
$lines = \array_slice($old, $i1, $i2 - $i1);
84+
$blocks[$lastBlock]['base']['lines'] += $this->formatLines($lines);
85+
$lines = \array_slice($new, $j1, $j2 - $j1);
86+
$blocks[$lastBlock]['changed']['lines'] += $this->formatLines($lines);
9187

9288
continue;
9389
}

src/Renderer/Html/Inline.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ protected function renderTableEqual(array $change): string
124124
{
125125
$html = '';
126126

127-
foreach ($change['base']['lines'] as $no => $line) {
127+
// note that although we are in a OP_EQ situation,
128+
// the old and the new may not be exactly the same
129+
// because of ignoreCase, ignoreWhitespace, etc
130+
foreach ($change['base']['lines'] as $no => $oldLine) {
131+
// hmm... but this is a inline template
132+
// we could only pick a line from the base or the changed to show
128133
$oldLineNum = $change['base']['offset'] + $no + 1;
129134
$newLineNum = $change['changed']['offset'] + $no + 1;
130135

@@ -133,7 +138,7 @@ protected function renderTableEqual(array $change): string
133138
'<th class="f-num">' . $oldLineNum . '</th>' .
134139
'<th class="t-num">' . $newLineNum . '</th>' .
135140
'<th class="sign"></th>' .
136-
'<td class="old">' . $line . '</td>' .
141+
'<td class="old">' . $oldLine . '</td>' .
137142
'</tr>';
138143
}
139144

src/Renderer/Html/SideBySide.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,21 @@ protected function renderTableEqual(array $change): string
122122
{
123123
$html = '';
124124

125-
foreach ($change['base']['lines'] as $no => $line) {
125+
// note that although we are in a OP_EQ situation,
126+
// the old and the new may not be exactly the same
127+
// because of ignoreCase, ignoreWhitespace, etc
128+
foreach ($change['base']['lines'] as $no => $oldLine) {
129+
$newLine = $change['changed']['lines'][$no];
130+
126131
$oldLineNum = $change['base']['offset'] + $no + 1;
127-
$newLine = $change['changed']['offset'] + $no + 1;
132+
$newLineNum = $change['changed']['offset'] + $no + 1;
128133

129134
$html .=
130135
'<tr>' .
131136
'<th class="f-num">' . $oldLineNum . '</th>' .
132-
'<td class="old">' . $line . '</td>' .
133-
'<th class="t-num">' . $newLine . '</th>' .
134-
'<td class="new">' . $line . '</td>' .
137+
'<td class="old">' . $oldLine . '</td>' .
138+
'<th class="t-num">' . $newLineNum . '</th>' .
139+
'<td class="new">' . $newLine . '</td>' .
135140
'</tr>';
136141
}
137142

0 commit comments

Comments
 (0)