Debian Codegraph @

vendor/jfcherng/php-diff/src/Renderer/Html/Inline.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 * Inline HTML diff generator.
11 */
12 final class Inline extends AbstractHtml
13 {
14 /**
15 * {@inheritdoc}
16 */
17 public const INFO = [
18 'desc' => 'Inline',
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-inline',
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 (
55 $this->options['lineNumbers']
56 ?
57 '<th>' . $this->_('old_version') . '</th>' .
58 '<th>' . $this->_('new_version') . '</th>' .
59 '<th></th>' // diff symbol column
60 :
61 ''
62 ) .
63 '<th' . $colspan . '>' . $this->_('differences') . '</th>' .
64 '</tr>' .
65 '</thead>';
66 }
67
68 /**
69 * Renderer the table separate block.
70 */
71 protected function renderTableSeparateBlock(): string
72 {
73 $colspan = $this->options['lineNumbers'] ? '4' : '2';
74
75 return
76 '<tbody class="skipped">' .
77 '<tr>' .
78 '<td colspan="' . $colspan . '"></td>' .
79 '</tr>' .
80 '</tbody>';
81 }
82
83 /**
84 * Renderer table hunks.
85 *
86 * @param array[][] $hunks each hunk has many blocks
87 */
88 protected function renderTableHunks(array $hunks): string
89 {
90 $ret = '';
91
92 foreach ($hunks as $i => $hunk) {
93 if ($i > 0 && $this->options['separateBlock']) {
94 $ret .= $this->renderTableSeparateBlock();
95 }
96
97 foreach ($hunk as $block) {
98 $ret .= $this->renderTableBlock($block);
99 }
100 }
101
102 return $ret;
103 }
104
105 /**
106 * Renderer the table block.
107 *
108 * @param array $block the block
109 */
110 protected function renderTableBlock(array $block): string
111 {
112 switch ($block['tag']) {
113 case SequenceMatcher::OP_EQ:
114 $content = $this->renderTableBlockEqual($block);
115 break;
116 case SequenceMatcher::OP_INS:
117 $content = $this->renderTableBlockInsert($block);
118 break;
119 case SequenceMatcher::OP_DEL:
120 $content = $this->renderTableBlockDelete($block);
121 break;
122 case SequenceMatcher::OP_REP:
123 $content = $this->renderTableBlockReplace($block);
124 break;
125 default:
126 $content = '';
127 }
128
129 return '<tbody class="change change-' . self::TAG_CLASS_MAP[$block['tag']] . '">' . $content . '</tbody>';
130 }
131
132 /**
133 * Renderer the table block: equal.
134 *
135 * @param array $block the block
136 */
137 protected function renderTableBlockEqual(array $block): string
138 {
139 $ret = '';
140
141 // note that although we are in a OP_EQ situation,
142 // the old and the new may not be exactly the same
143 // because of ignoreCase, ignoreWhitespace, etc
144 foreach ($block['new']['lines'] as $no => $newLine) {
145 // we could only pick either the old or the new to show
146 // here we pick the new one to let the user know what it is now
147 $ret .= $this->renderTableRow(
148 'new',
149 SequenceMatcher::OP_EQ,
150 $newLine,
151 $block['old']['offset'] + $no + 1,
152 $block['new']['offset'] + $no + 1,
153 );
154 }
155
156 return $ret;
157 }
158
159 /**
160 * Renderer the table block: insert.
161 *
162 * @param array $block the block
163 */
164 protected function renderTableBlockInsert(array $block): string
165 {
166 $ret = '';
167
168 foreach ($block['new']['lines'] as $no => $newLine) {
169 $ret .= $this->renderTableRow(
170 'new',
171 SequenceMatcher::OP_INS,
172 $newLine,
173 null,
174 $block['new']['offset'] + $no + 1,
175 );
176 }
177
178 return $ret;
179 }
180
181 /**
182 * Renderer the table block: delete.
183 *
184 * @param array $block the block
185 */
186 protected function renderTableBlockDelete(array $block): string
187 {
188 $ret = '';
189
190 foreach ($block['old']['lines'] as $no => $oldLine) {
191 $ret .= $this->renderTableRow(
192 'old',
193 SequenceMatcher::OP_DEL,
194 $oldLine,
195 $block['old']['offset'] + $no + 1,
196 null,
197 );
198 }
199
200 return $ret;
201 }
202
203 /**
204 * Renderer the table block: replace.
205 *
206 * @param array $block the block
207 */
208 protected function renderTableBlockReplace(array $block): string
209 {
210 return
211 $this->renderTableBlockDelete($block) .
212 $this->renderTableBlockInsert($block);
213 }
214
215 /**
216 * Renderer a content row of the output table.
217 *
218 * @param string $tdClass the <td> class
219 * @param int $op the operation
220 * @param string $line the line
221 * @param null|int $oldLineNum the old line number
222 * @param null|int $newLineNum the new line number
223 */
224 protected function renderTableRow(
225 string $tdClass,
226 int $op,
227 string $line,
228 ?int $oldLineNum,
229 ?int $newLineNum
230 ): string {
231 return
232 '<tr data-type="' . self::SYMBOL_MAP[$op] . '">' .
233 (
234 $this->options['lineNumbers']
235 ? $this->renderLineNumberColumns($oldLineNum, $newLineNum)
236 : ''
237 ) .
238 '<th class="sign ' . self::TAG_CLASS_MAP[$op] . '">' . self::SYMBOL_MAP[$op] . '</th>' .
239 '<td class="' . $tdClass . '">' . $line . '</td>' .
240 '</tr>';
241 }
242
243 /**
244 * Renderer the line number columns.
245 *
246 * @param null|int $oldLineNum The old line number
247 * @param null|int $newLineNum The new line number
248 */
249 protected function renderLineNumberColumns(?int $oldLineNum, ?int $newLineNum): string
250 {
251 return
252 (
253 isset($oldLineNum)
254 ? '<th class="n-old">' . $oldLineNum . '</th>'
255 : '<th></th>'
256 ) .
257 (
258 isset($newLineNum)
259 ? '<th class="n-new">' . $newLineNum . '</th>'
260 : '<th></th>'
261 );
262 }
263 }