Skip to content

Commit 5b673fd

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 176b647 + 56d7a5f commit 5b673fd

File tree

3 files changed

+329
-164
lines changed

3 files changed

+329
-164
lines changed

lib/Diff/Renderer/Html/Array.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function mb_substr_replace($string, $replacement, $start, $length=NULL) {
110110
* based differences. Generally called by subclasses that generate a
111111
* HTML based diff and return an array of the changes to show in the diff.
112112
*
113-
* @return array An array of the generated chances, suitable for presentation in HTML.
113+
* @return array An array of the generated changes, suitable for presentation in HTML.
114114
*/
115115
public function render()
116116
{
@@ -154,17 +154,7 @@ public function render()
154154
}
155155

156156
if($tag != $lastTag) {
157-
$blocks[] = array(
158-
'tag' => $tag,
159-
'base' => array(
160-
'offset' => $i1,
161-
'lines' => array()
162-
),
163-
'changed' => array(
164-
'offset' => $j1,
165-
'lines' => array()
166-
)
167-
);
157+
$blocks[] = $this->getDefaultArray($tag, $i1, $j1);
168158
$lastBlock = count($blocks)-1;
169159
}
170160

@@ -297,4 +287,18 @@ private function htmlSafe($string)
297287
{
298288
return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
299289
}
290+
291+
private function getDefaultArray($tag, $i1, $j1){
292+
return array(
293+
'tag' => $tag,
294+
'base' => array(
295+
'offset' => $i1,
296+
'lines' => array()
297+
),
298+
'changed' => array(
299+
'offset' => $j1,
300+
'lines' => array()
301+
)
302+
);
303+
}
300304
}

lib/Diff/Renderer/Html/Inline.php

+147-66
Original file line numberDiff line numberDiff line change
@@ -61,86 +61,167 @@ public function render()
6161
return $html;
6262
}
6363

64-
$html .= '<table class="Differences DifferencesInline">';
65-
$html .= '<thead>';
66-
$html .= '<tr>';
67-
$html .= '<th>Old</th>';
68-
$html .= '<th>New</th>';
69-
$html .= '<th>Differences</th>';
70-
$html .= '</tr>';
71-
$html .= '</thead>';
64+
$html .= $this->generateTableHeader();
65+
7266
foreach($changes as $i => $blocks) {
7367
// If this is a separate block, we're condensing code so output ...,
7468
// indicating a significant portion of the code has been collapsed as
7569
// it is the same
7670
if($i > 0) {
77-
$html .= '<tbody class="Skipped">';
78-
$html .= '<th>&hellip;</th>';
79-
$html .= '<th>&hellip;</th>';
80-
$html .= '<td>&#xA0;</td>';
81-
$html .= '</tbody>';
71+
$html .= $this->generateSkippedTable();
8272
}
8373

8474
foreach($blocks as $change) {
8575
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
86-
// Equal changes should be shown on both sides of the diff
87-
if($change['tag'] == 'equal') {
88-
foreach($change['base']['lines'] as $no => $line) {
89-
$fromLine = $change['base']['offset'] + $no + 1;
90-
$toLine = $change['changed']['offset'] + $no + 1;
91-
$html .= '<tr>';
92-
$html .= '<th>'.$fromLine.'</th>';
93-
$html .= '<th>'.$toLine.'</th>';
94-
$html .= '<td class="Left">'.$line.'</td>';
95-
$html .= '</tr>';
96-
}
97-
}
98-
// Added lines only on the right side
99-
else if($change['tag'] == 'insert') {
100-
foreach($change['changed']['lines'] as $no => $line) {
101-
$toLine = $change['changed']['offset'] + $no + 1;
102-
$html .= '<tr>';
103-
$html .= '<th>&#xA0;</th>';
104-
$html .= '<th>'.$toLine.'</th>';
105-
$html .= '<td class="Right"><ins>'.$line.'</ins>&#xA0;</td>';
106-
$html .= '</tr>';
107-
}
108-
}
109-
// Show deleted lines only on the left side
110-
else if($change['tag'] == 'delete') {
111-
foreach($change['base']['lines'] as $no => $line) {
112-
$fromLine = $change['base']['offset'] + $no + 1;
113-
$html .= '<tr>';
114-
$html .= '<th>'.$fromLine.'</th>';
115-
$html .= '<th>&#xA0;</th>';
116-
$html .= '<td class="Left"><del>'.$line.'</del>&#xA0;</td>';
117-
$html .= '</tr>';
118-
}
119-
}
120-
// Show modified lines on both sides
121-
else if($change['tag'] == 'replace') {
122-
foreach($change['base']['lines'] as $no => $line) {
123-
$fromLine = $change['base']['offset'] + $no + 1;
124-
$html .= '<tr>';
125-
$html .= '<th>'.$fromLine.'</th>';
126-
$html .= '<th>&#xA0;</th>';
127-
$html .= '<td class="Left"><span>'.$line.'</span></td>';
128-
$html .= '</tr>';
129-
}
130-
131-
foreach($change['changed']['lines'] as $no => $line) {
132-
$toLine = $change['changed']['offset'] + $no + 1;
133-
$html .= '<tr>';
134-
$html .= '<th>&#xA0;</th>';
135-
$html .= '<th>'.$toLine.'</th>';
136-
$html .= '<td class="Right"><span>'.$line.'</span></td>';
137-
$html .= '</tr>';
138-
}
76+
switch ($change['tag']){
77+
// Equal changes should be shown on both sides of the diff
78+
case 'equal':
79+
$html .= $this->generateTableRowsEqual($change);
80+
break;
81+
// Added lines only on the right side
82+
case 'insert':
83+
$html .= $this->generateTableRowsInsert($change);
84+
break;
85+
// Show deleted lines only on the left side
86+
case 'delete':
87+
$html .= $this->generateTableRowsDelete($change);
88+
break;
89+
// Show modified lines on both sides
90+
case 'replace':
91+
$html .= $this->generateTableRowsReplace($change);
92+
break;
13993
}
14094
$html .= '</tbody>';
14195
}
14296
}
14397
$html .= '</table>';
14498
return $html;
14599
}
100+
101+
102+
/**
103+
* Generates a string representation of a predefined table and its head with
104+
* titles from options.
105+
*
106+
* @return string Html code representation of the table's header.
107+
*/
108+
private function generateTableHeader()
109+
{
110+
$html = '<table class="Differences DifferencesInline">';
111+
$html .= '<thead>';
112+
$html .= '<tr>';
113+
$html .= '<th>Old</th>';
114+
$html .= '<th>New</th>';
115+
$html .= '<th>Differences</th>';
116+
$html .= '</tr>';
117+
$html .= '</thead>';
118+
return $html;
119+
}
120+
121+
/**
122+
* Generates a string representation of empty table body.
123+
*
124+
* @return string Html code representing empty table body.
125+
*/
126+
private function generateSkippedTable()
127+
{
128+
$html = '<tbody class="Skipped">';
129+
$html .= '<th>&hellip;</th>';
130+
$html .= '<th>&hellip;</th>';
131+
$html .= '<td>&#xA0;</td>';
132+
$html .= '</tbody>';
133+
return $html;
134+
}
135+
136+
/**
137+
* Generates a string representation of one or more rows of a table of lines of text with no difference.
138+
*
139+
* @param array &$change Array with data about changes.
140+
* @return string Html code representing one or more rows of text with no difference.
141+
*/
142+
private function generateTableRowsEqual(&$change)
143+
{
144+
$html = "";
145+
foreach($change['base']['lines'] as $no => $line) {
146+
$fromLine = $change['base']['offset'] + $no + 1;
147+
$toLine = $change['changed']['offset'] + $no + 1;
148+
$html .= '<tr>';
149+
$html .= '<th>'.$fromLine.'</th>';
150+
$html .= '<th>'.$toLine.'</th>';
151+
$html .= '<td class="Left">'.$line.'</td>';
152+
$html .= '</tr>';
153+
}
154+
return $html;
155+
}
156+
157+
/**
158+
* Generates a string representation of one or more rows of a table of lines, where new text was added.
159+
*
160+
* @param array &$change Array with data about changes.
161+
* @return string Html code representing one or more rows of added text.
162+
*/
163+
private function generateTableRowsInsert(&$change)
164+
{
165+
$html = "";
166+
foreach($change['changed']['lines'] as $no => $line) {
167+
$toLine = $change['changed']['offset'] + $no + 1;
168+
$html .= '<tr>';
169+
$html .= '<th>&#xA0;</th>';
170+
$html .= '<th>'.$toLine.'</th>';
171+
$html .= '<td class="Right"><ins>'.$line.'</ins>&#xA0;</td>';
172+
$html .= '</tr>';
173+
}
174+
return $html;
175+
}
176+
177+
/**
178+
* Generates a string representation of one or more rows of a table of lines, where text was removed.
179+
*
180+
* @param array &$change Array with data about changes.
181+
* @return string Html code representing one or more rows of removed text.
182+
*/
183+
private function generateTableRowsDelete(&$change)
184+
{
185+
$html = "";
186+
foreach($change['base']['lines'] as $no => $line) {
187+
$fromLine = $change['base']['offset'] + $no + 1;
188+
$html .= '<tr>';
189+
$html .= '<th>'.$fromLine.'</th>';
190+
$html .= '<th>&#xA0;</th>';
191+
$html .= '<td class="Left"><del>'.$line.'</del>&#xA0;</td>';
192+
$html .= '</tr>';
193+
}
194+
return $html;
195+
}
196+
197+
/**
198+
* Generates a string representation of one or more rows of a table of lines, where text was partially modified.
199+
*
200+
* @param array &$change Array with data about changes.
201+
* @return string Html code representing one or more rows of modified.
202+
*/
203+
private function generateTableRowsReplace(&$change)
204+
{
205+
$html = "";
206+
207+
foreach($change['base']['lines'] as $no => $line) {
208+
$fromLine = $change['base']['offset'] + $no + 1;
209+
$html .= '<tr>';
210+
$html .= '<th>'.$fromLine.'</th>';
211+
$html .= '<th>&#xA0;</th>';
212+
$html .= '<td class="Left"><span>'.$line.'</span></td>';
213+
$html .= '</tr>';
214+
}
215+
216+
foreach($change['changed']['lines'] as $no => $line) {
217+
$toLine = $change['changed']['offset'] + $no + 1;
218+
$html .= '<tr>';
219+
$html .= '<th>&#xA0;</th>';
220+
$html .= '<th>'.$toLine.'</th>';
221+
$html .= '<td class="Right"><span>'.$line.'</span></td>';
222+
$html .= '</tr>';
223+
}
224+
225+
return $html;
226+
}
146227
}

0 commit comments

Comments
 (0)