1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Component; |
6
|
|
|
|
7
|
|
|
use Diff_Renderer_Html_Array; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Renders diff to HTML. Output adjusted to be copy-paste friendly. |
11
|
|
|
* |
12
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
13
|
|
|
*/ |
14
|
|
|
final class DiffRendererHtmlInline extends Diff_Renderer_Html_Array |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Render a and return diff with changes between the two sequences |
18
|
|
|
* displayed inline (under each other) |
19
|
|
|
* |
20
|
|
|
* @psalm-suppress ImplementedReturnTypeMismatch |
21
|
|
|
* |
22
|
|
|
* @return string The generated inline diff. |
23
|
|
|
*/ |
24
|
|
|
public function render(): string |
25
|
|
|
{ |
26
|
|
|
$changes = parent::render(); |
27
|
|
|
$html = ''; |
28
|
|
|
if (empty($changes)) { |
29
|
|
|
return $html; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$html .= <<<HTML |
33
|
|
|
<table class="Differences DifferencesInline"> |
34
|
|
|
<thead> |
35
|
|
|
<tr> |
36
|
|
|
<th>Old</th> |
37
|
|
|
<th>New</th> |
38
|
|
|
<th>Differences</th> |
39
|
|
|
</tr> |
40
|
|
|
</thead> |
41
|
|
|
HTML; |
42
|
|
|
foreach ($changes as $i => $blocks) { |
43
|
|
|
// If this is a separate block, we're condensing code so output ..., |
44
|
|
|
// indicating a significant portion of the code has been collapsed as |
45
|
|
|
// it is the same |
46
|
|
|
if ($i > 0) { |
47
|
|
|
$html .= <<<HTML |
48
|
|
|
<tbody class="Skipped"> |
49
|
|
|
<th data-line-number="…"></th> |
50
|
|
|
<th data-line-number="…"></th> |
51
|
|
|
<td> </td> |
52
|
|
|
</tbody> |
53
|
|
|
HTML; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($blocks as $change) { |
57
|
|
|
$tag = ucfirst($change['tag']); |
58
|
|
|
$html .= <<<HTML |
59
|
|
|
<tbody class="Change{$tag}"> |
60
|
|
|
HTML; |
61
|
|
|
// Equal changes should be shown on both sides of the diff |
62
|
|
|
if ($change['tag'] === 'equal') { |
63
|
|
|
foreach ($change['base']['lines'] as $no => $line) { |
64
|
|
|
$fromLine = $change['base']['offset'] + $no + 1; |
65
|
|
|
$toLine = $change['changed']['offset'] + $no + 1; |
66
|
|
|
$html .= <<<HTML |
67
|
|
|
<tr> |
68
|
|
|
<th data-line-number="{$fromLine}"></th> |
69
|
|
|
<th data-line-number="{$toLine}"></th> |
70
|
|
|
<td class="Left">{$line}</td> |
71
|
|
|
</tr> |
72
|
|
|
HTML; |
73
|
|
|
} |
74
|
|
|
} // Added lines only on the right side |
75
|
|
|
elseif ($change['tag'] === 'insert') { |
76
|
|
|
foreach ($change['changed']['lines'] as $no => $line) { |
77
|
|
|
$toLine = $change['changed']['offset'] + $no + 1; |
78
|
|
|
$html .= <<<HTML |
79
|
|
|
<tr> |
80
|
|
|
<th data-line-number=" "></th> |
81
|
|
|
<th data-line-number="{$toLine}"></th> |
82
|
|
|
<td class="Right"><ins>{$line}</ins> </td> |
83
|
|
|
</tr> |
84
|
|
|
HTML; |
85
|
|
|
} |
86
|
|
|
} // Show deleted lines only on the left side |
87
|
|
|
elseif ($change['tag'] === 'delete') { |
88
|
|
|
foreach ($change['base']['lines'] as $no => $line) { |
89
|
|
|
$fromLine = $change['base']['offset'] + $no + 1; |
90
|
|
|
$html .= <<<HTML |
91
|
|
|
<tr> |
92
|
|
|
<th data-line-number="{$fromLine}"></th> |
93
|
|
|
<th data-line-number=" "></th> |
94
|
|
|
<td class="Left"><del>{$line}</del> </td> |
95
|
|
|
</tr> |
96
|
|
|
HTML; |
97
|
|
|
} |
98
|
|
|
} // Show modified lines on both sides |
99
|
|
|
elseif ($change['tag'] === 'replace') { |
100
|
|
|
foreach ($change['base']['lines'] as $no => $line) { |
101
|
|
|
$fromLine = $change['base']['offset'] + $no + 1; |
102
|
|
|
$html .= <<<HTML |
103
|
|
|
<tr> |
104
|
|
|
<th data-line-number="{$fromLine}"></th> |
105
|
|
|
<th data-line-number=" "></th> |
106
|
|
|
<td class="Left"><span>{$line}</span></td> |
107
|
|
|
</tr> |
108
|
|
|
HTML; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($change['changed']['lines'] as $no => $line) { |
112
|
|
|
$toLine = $change['changed']['offset'] + $no + 1; |
113
|
|
|
$html .= <<<HTML |
114
|
|
|
<tr> |
115
|
|
|
<th data-line-number="{$toLine}"></th> |
116
|
|
|
<th data-line-number=" "></th> |
117
|
|
|
<td class="Right"><span>{$line}</span></td> |
118
|
|
|
</tr> |
119
|
|
|
HTML; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
$html .= <<<HTML |
123
|
|
|
</tbody> |
124
|
|
|
HTML; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$html .= <<<HTML |
128
|
|
|
</table> |
129
|
|
|
HTML; |
130
|
|
|
|
131
|
|
|
return $html; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|