Debian Codegraph @

vendor/jfcherng/php-diff/src/Renderer/Html/SideBySide.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Diff\Renderer\Html;
6
7 use Jfcherng\Diff\SequenceMatcher;
8
9 /**
10 * Side by Side HTML diff generator.
11 */
12 final class SideBySide extends AbstractHtml
13 {
14 /**
15 * {@inheritdoc}
16 */
17 public const INFO = [
18 'desc' => 'Side by side',
19 'type' => 'Html',
20 ];
21
22 protected function redererChanges(array $changes): string
23 {
24 if (empty($changes)) {
25 return $this->getResultForIdenticals();
26 }
27
28 $wrapperClasses = [
29 ...$this->options['wrapperClasses'],
30 'diff', 'diff-html', 'diff-side-by-side',
31 ];
32
33 return
34 '<table class="' . implode(' ', $wrapperClasses) . '">' .
35 $this->renderTableHeader() .
36 $this->renderTableHunks($changes) .
37 '</table>';
38 }
39
40 /**
41 * Renderer the table header.
42 */
43 protected function renderTableHeader(): string
44 {
45 if (!$this->options['showHeader']) {
46 return '';
47 }
48
49 $colspan = $this->options['lineNumbers'] ? ' colspan="2"' : '';
50
51 return
52 '<thead>' .
53 '<tr>' .
54 '<th' . $colspan . '>' . $this->_('old_version') . '</th>' .
55 '<th' . $colspan . '>' . $this->_('new_version') . '</th>' .
56 '</tr>' .
57 '</thead>';
58 }
59
60 /**
61 * Renderer the table separate block.
62 */
63 protected function renderTableSeparateBlock(): string
64 {
65 $colspan = $this->options['lineNumbers'] ? '4' : '2';
66
67 return
68 '<tbody class="skipped">' .
69 '<tr>' .
70 '<td colspan="' . $colspan . '"></td>' .
71 '</tr>' .
72 '</tbody>';
73 }
74
75 /**
76 * Renderer table hunks.
77 *
78 * @param array[][] $hunks each hunk has many blocks
79 */
80 protected function renderTableHunks(array $hunks): string
81 {
82 $ret = '';
83
84 foreach ($hunks as $i => $hunk) {
85 if ($i > 0 && $this->options['separateBlock']) {
86 $ret .= $this->renderTableSeparateBlock();
87 }
88
89 foreach ($hunk as $block) {
90 $ret .= $this->renderTableBlock($block);
91 }
92 }
93
94 return $ret;
95 }
96
97 /**
98 * Renderer the table block.
99 *
100 * @param array $block the block
101 */
102 protected function renderTableBlock(array $block): string
103 {
104 switch ($block['tag']) {
105 case SequenceMatcher::OP_EQ:
106 $content = $this->renderTableBlockEqual($block);
107 break;
108 case SequenceMatcher::OP_INS:
109 $content = $this->renderTableBlockInsert($block);
110 break;
111 case SequenceMatcher::OP_DEL:
112 $content = $this->renderTableBlockDelete($block);
113 break;
114 case SequenceMatcher::OP_REP:
115 $content = $this->renderTableBlockReplace($block);
116 break;
117 default:
118 $content = '';
119 }
120
121 return '<tbody class="change change-' . self::TAG_CLASS_MAP[$block['tag']] . '">' . $content . '</tbody>';
122 }
123
124 /**
125 * Renderer the table block: equal.
126 *
127 * @param array $block the block
128 */
129 protected function renderTableBlockEqual(array $block): string
130 {
131 $ret = '';
132
133 $rowCount = \count($block['new']['lines']);
134
135 for ($no = 0; $no < $rowCount; ++$no) {
136 $ret .= $this->renderTableRow(
137 $block['old']['lines'][$no],
138 $block['new']['lines'][$no],
139 $block['old']['offset'] + $no + 1,
140 $block['new']['offset'] + $no + 1,
141 );
142 }
143
144 return $ret;
145 }
146
147 /**
148 * Renderer the table block: insert.
149 *
150 * @param array $block the block
151 */
152 protected function renderTableBlockInsert(array $block): string
153 {
154 $ret = '';
155
156 foreach ($block['new']['lines'] as $no => $newLine) {
157 $ret .= $this->renderTableRow(
158 null,
159 $newLine,
160 null,
161 $block['new']['offset'] + $no + 1,
162 );
163 }
164
165 return $ret;
166 }
167
168 /**
169 * Renderer the table block: delete.
170 *
171 * @param array $block the block
172 */
173 protected function renderTableBlockDelete(array $block): string
174 {
175 $ret = '';
176
177 foreach ($block['old']['lines'] as $no => $oldLine) {
178 $ret .= $this->renderTableRow(
179 $oldLine,
180 null,
181 $block['old']['offset'] + $no + 1,
182 null,
183 );
184 }
185
186 return $ret;
187 }
188
189 /**
190 * Renderer the table block: replace.
191 *
192 * @param array $block the block
193 */
194 protected function renderTableBlockReplace(array $block): string
195 {
196 $ret = '';
197
198 $lineCountMax = max(\count($block['old']['lines']), \count($block['new']['lines']));
199
200 for ($no = 0; $no < $lineCountMax; ++$no) {
201 if (isset($block['old']['lines'][$no])) {
202 $oldLineNum = $block['old']['offset'] + $no + 1;
203 $oldLine = $block['old']['lines'][$no];
204 } else {
205 $oldLineNum = $oldLine = null;
206 }
207
208 if (isset($block['new']['lines'][$no])) {
209 $newLineNum = $block['new']['offset'] + $no + 1;
210 $newLine = $block['new']['lines'][$no];
211 } else {
212 $newLineNum = $newLine = null;
213 }
214
215 $ret .= $this->renderTableRow($oldLine, $newLine, $oldLineNum, $newLineNum);
216 }
217
218 return $ret;
219 }
220
221 /**
222 * Renderer a content row of the output table.
223 *
224 * @param null|string $oldLine the old line
225 * @param null|string $newLine the new line
226 * @param null|int $oldLineNum the old line number
227 * @param null|int $newLineNum the new line number
228 */
229 protected function renderTableRow(
230 ?string $oldLine,
231 ?string $newLine,
232 ?int $oldLineNum,
233 ?int $newLineNum
234 ): string {
235 return
236 '<tr>' .
237 (
238 $this->options['lineNumbers']
239 ? $this->renderLineNumberColumn('old', $oldLineNum)
240 : ''
241 ) .
242 $this->renderLineContentColumn('old', $oldLine) .
243 (
244 $this->options['lineNumbers']
245 ? $this->renderLineNumberColumn('new', $newLineNum)
246 : ''
247 ) .
248 $this->renderLineContentColumn('new', $newLine) .
249 '</tr>';
250 }
251
252 /**
253 * Renderer the line number column.
254 *
255 * @param string $type the diff type
256 * @param null|int $lineNum the line number
257 */
258 protected function renderLineNumberColumn(string $type, ?int $lineNum): string
259 {
260 return isset($lineNum)
261 ? '<th class="n-' . $type . '">' . $lineNum . '</th>'
262 : '<th></th>';
263 }
264
265 /**
266 * Renderer the line content column.
267 *
268 * @param string $type the diff type
269 * @param null|string $content the line content
270 */
271 protected function renderLineContentColumn(string $type, ?string $content): string
272 {
273 return
274 '<td class="' . $type . (isset($content) ? '' : ' none') . '">' .
275 $content .
276 '</td>';
277 }
278 }