Skip to content

Commit 5967e91

Browse files
committed
deduplicate render code
1 parent 8b68a5d commit 5967e91

File tree

3 files changed

+62
-93
lines changed

3 files changed

+62
-93
lines changed

lib/jblond/Diff/Renderer/Html/HtmlArray.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,54 @@ public function mbSubstrReplace(string $string, string $replacement, int $start,
115115
return join($smatches['0']);
116116
}
117117

118+
/**
119+
* @param string|array $changes
120+
* @param SideBySide|Inline $object
121+
* @return string
122+
*/
123+
public function renderHtml($changes, $object)
124+
{
125+
$html = '';
126+
if (empty($changes)) {
127+
return $html;
128+
}
129+
130+
$html .= $object->generateTableHeader();
131+
132+
foreach ($changes as $i => $blocks) {
133+
// If this is a separate block, we're condensing code so output ...,
134+
// indicating a significant portion of the code has been collapsed as
135+
// it is the same
136+
if ($i > 0) {
137+
$html .= $object->generateSkippedTable();
138+
}
139+
140+
foreach ($blocks as $change) {
141+
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
142+
switch ($change['tag']) {
143+
// Equal changes should be shown on both sides of the diff
144+
case 'equal':
145+
$html .= $object->generateTableRowsEqual($change);
146+
break;
147+
// Added lines only on the right side
148+
case 'insert':
149+
$html .= $object->generateTableRowsInsert($change);
150+
break;
151+
// Show deleted lines only on the left side
152+
case 'delete':
153+
$html .= $object->generateTableRowsDelete($change);
154+
break;
155+
// Show modified lines on both sides
156+
case 'replace':
157+
$html .= $object->generateTableRowsReplace($change);
158+
break;
159+
}
160+
$html .= '</tbody>';
161+
}
162+
}
163+
$html .= '</table>';
164+
return $html;
165+
}
118166
/**
119167
* Render and return an array structure suitable for generating HTML
120168
* based differences. Generally called by subclasses that generate a

lib/jblond/Diff/Renderer/Html/Inline.php

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,7 @@ class Inline extends HtmlArray
5757
public function render() : string
5858
{
5959
$changes = parent::render();
60-
$html = '';
61-
if (empty($changes)) {
62-
return $html;
63-
}
64-
65-
$html .= $this->generateTableHeader();
66-
67-
foreach ($changes as $i => $blocks) {
68-
// If this is a separate block, we're condensing code so output ...,
69-
// indicating a significant portion of the code has been collapsed as
70-
// it is the same
71-
if ($i > 0) {
72-
$html .= $this->generateSkippedTable();
73-
}
74-
75-
foreach ($blocks as $change) {
76-
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
77-
switch ($change['tag']) {
78-
// Equal changes should be shown on both sides of the diff
79-
case 'equal':
80-
$html .= $this->generateTableRowsEqual($change);
81-
break;
82-
// Added lines only on the right side
83-
case 'insert':
84-
$html .= $this->generateTableRowsInsert($change);
85-
break;
86-
// Show deleted lines only on the left side
87-
case 'delete':
88-
$html .= $this->generateTableRowsDelete($change);
89-
break;
90-
// Show modified lines on both sides
91-
case 'replace':
92-
$html .= $this->generateTableRowsReplace($change);
93-
break;
94-
}
95-
$html .= '</tbody>';
96-
}
97-
}
98-
$html .= '</table>';
99-
return $html;
60+
return parent::renderHtml($changes, $this);
10061
}
10162

10263

@@ -106,7 +67,7 @@ public function render() : string
10667
*
10768
* @return string Html code representation of the table's header.
10869
*/
109-
private function generateTableHeader() : string
70+
public function generateTableHeader() : string
11071
{
11172
$html = '<table class="Differences DifferencesInline">';
11273
$html .= '<thead>';
@@ -124,7 +85,7 @@ private function generateTableHeader() : string
12485
*
12586
* @return string Html code representing empty table body.
12687
*/
127-
private function generateSkippedTable() : string
88+
public function generateSkippedTable() : string
12889
{
12990
$html = '<tbody class="Skipped">';
13091
$html .= '<th>&hellip;</th>';
@@ -140,7 +101,7 @@ private function generateSkippedTable() : string
140101
* @param array &$change Array with data about changes.
141102
* @return string Html code representing one or more rows of text with no difference.
142103
*/
143-
private function generateTableRowsEqual(array &$change) : string
104+
public function generateTableRowsEqual(array &$change) : string
144105
{
145106
$html = "";
146107
foreach ($change['base']['lines'] as $no => $line) {
@@ -161,7 +122,7 @@ private function generateTableRowsEqual(array &$change) : string
161122
* @param array &$change Array with data about changes.
162123
* @return string Html code representing one or more rows of added text.
163124
*/
164-
private function generateTableRowsInsert(array &$change) : string
125+
public function generateTableRowsInsert(array &$change) : string
165126
{
166127
$html = "";
167128
foreach ($change['changed']['lines'] as $no => $line) {
@@ -181,7 +142,7 @@ private function generateTableRowsInsert(array &$change) : string
181142
* @param array &$change Array with data about changes.
182143
* @return string Html code representing one or more rows of removed text.
183144
*/
184-
private function generateTableRowsDelete(array &$change) : string
145+
public function generateTableRowsDelete(array &$change) : string
185146
{
186147
$html = "";
187148
foreach ($change['base']['lines'] as $no => $line) {
@@ -201,7 +162,7 @@ private function generateTableRowsDelete(array &$change) : string
201162
* @param array &$change Array with data about changes.
202163
* @return string Html code representing one or more rows of modified.
203164
*/
204-
private function generateTableRowsReplace(array &$change) : string
165+
public function generateTableRowsReplace(array &$change) : string
205166
{
206167
$html = "";
207168

lib/jblond/Diff/Renderer/Html/SideBySide.php

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,7 @@ class SideBySide extends HtmlArray
5757
public function render() : string
5858
{
5959
$changes = parent::render();
60-
61-
$html = '';
62-
if (empty($changes)) {
63-
return $html;
64-
}
65-
66-
$html .= $this->generateTableHeader();
67-
68-
foreach ($changes as $i => $blocks) {
69-
// If this is a separate block, we're condensing code so output ...,
70-
// indicating a significant portion of the code has been collapsed as
71-
// it is the same
72-
if ($i > 0) {
73-
$html .= $this->generateSkippedTable();
74-
}
75-
76-
foreach ($blocks as $change) {
77-
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
78-
switch ($change['tag']) {
79-
// Equal changes should be shown on both sides of the diff
80-
case 'equal':
81-
$html .= $this->generateTableRowsEqual($change);
82-
break;
83-
// Added lines only on the right side
84-
case 'insert':
85-
$html .= $this->generateTableRowsInsert($change);
86-
break;
87-
// Show deleted lines only on the left side
88-
case 'delete':
89-
$html .= $this->generateTableRowsDelete($change);
90-
break;
91-
// Show modified lines on both sides
92-
case 'replace':
93-
$html .= $this->generateTableRowsReplace($change);
94-
break;
95-
}
96-
$html .= '</tbody>';
97-
}
98-
}
99-
$html .= '</table>';
100-
return $html;
60+
return parent::renderHtml($changes, $this);
10161
}
10262

10363
/**
@@ -106,7 +66,7 @@ public function render() : string
10666
*
10767
* @return string Html code representation of the table's header.
10868
*/
109-
private function generateTableHeader() : string
69+
public function generateTableHeader() : string
11070
{
11171
$html = '<table class="Differences DifferencesSideBySide">';
11272
$html .= '<thead>';
@@ -123,7 +83,7 @@ private function generateTableHeader() : string
12383
*
12484
* @return string Html code representing empty table body.
12585
*/
126-
private function generateSkippedTable() : string
86+
public function generateSkippedTable() : string
12787
{
12888
$html = '<tbody class="Skipped">';
12989
$html .= '<th>&hellip;</th><td>&#xA0;</td>';
@@ -138,7 +98,7 @@ private function generateSkippedTable() : string
13898
* @param array &$change Array with data about changes.
13999
* @return string Html code representing one or more rows of text with no difference.
140100
*/
141-
private function generateTableRowsEqual(array &$change) : string
101+
public function generateTableRowsEqual(array &$change) : string
142102
{
143103
$html = "";
144104
foreach ($change['base']['lines'] as $no => $line) {
@@ -160,7 +120,7 @@ private function generateTableRowsEqual(array &$change) : string
160120
* @param array &$change Array with data about changes.
161121
* @return string Html code representing one or more rows of added text.
162122
*/
163-
private function generateTableRowsInsert(array &$change) : string
123+
public function generateTableRowsInsert(array &$change) : string
164124
{
165125
$html = "";
166126
foreach ($change['changed']['lines'] as $no => $line) {
@@ -181,7 +141,7 @@ private function generateTableRowsInsert(array &$change) : string
181141
* @param array &$change Array with data about changes.
182142
* @return string Html code representing one or more rows of removed text.
183143
*/
184-
private function generateTableRowsDelete(array &$change) : string
144+
public function generateTableRowsDelete(array &$change) : string
185145
{
186146
$html = "";
187147
foreach ($change['base']['lines'] as $no => $line) {
@@ -202,7 +162,7 @@ private function generateTableRowsDelete(array &$change) : string
202162
* @param array &$change Array with data about changes.
203163
* @return string Html code representing one or more rows of modified.
204164
*/
205-
private function generateTableRowsReplace(array &$change) : string
165+
public function generateTableRowsReplace(array &$change) : string
206166
{
207167
$html = "";
208168

0 commit comments

Comments
 (0)