Skip to content

Commit 78a1658

Browse files
committed
Code Fixes, Optimization, cleanup, refactoring and commenting.
- Major fix: The HTML renderers are now able to collapse blocks of equal lines when using the 'context' option of the Diff class.
1 parent f5f11a3 commit 78a1658

File tree

12 files changed

+296
-200
lines changed

12 files changed

+296
-200
lines changed

example/a.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<pre>
1111
构建具有中国特色的医学人才培养体系
1212
</pre>
13+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
14+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1315
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1416
<pre>
1517
另外我覺得那個評價的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅

example/b.txt

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<pre>
1212
构建具有中国國的医学人才培养体系
1313
</pre>
14+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
15+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1416
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1517
<pre>
1618
另外我覺得那個評鑑的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅

example/example.php

+20-25
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// Options for generating the diff.
2020
$options = [
2121
//'ignoreWhitespace' => true,
22-
//'ignoreCase' => true,
22+
//'ignoreCase' => true,
23+
//'context' => 2, /* Remove // to see text collapse in action */
2324
];
2425

2526
// Initialize the diff class.
@@ -60,33 +61,27 @@
6061
?>
6162

6263
<h2>HTML Unified Diff</h2>
63-
<pre>
64-
<?php
65-
// Generate a unified diff.
66-
// \jblond\Diff\Renderer\Html
67-
$renderer = new HtmlUnified();
68-
echo $diff->render($renderer);
69-
?>
70-
</pre>
64+
<?php
65+
// Generate a unified diff.
66+
// \jblond\Diff\Renderer\Html
67+
$renderer = new HtmlUnified();
68+
echo "<pre>{$diff->render($renderer)}</pre>";
69+
?>
7170

7271
<h2>Text Unified Diff</h2>
73-
<pre>
74-
<?php
75-
// Generate a unified diff.
76-
// \jblond\Diff\Renderer\Text
77-
$renderer = new Unified();
78-
echo htmlspecialchars($diff->render($renderer));
79-
?>
80-
</pre>
72+
<?php
73+
// Generate a unified diff.
74+
// \jblond\Diff\Renderer\Text
75+
$renderer = new Unified();
76+
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
77+
?>
8178

8279
<h2>Text Context Diff</h2>
83-
<pre>
84-
<?php
85-
// Generate a context diff.
86-
// jblond\Diff\Renderer\Text\Context
87-
$renderer = new Context();
88-
echo htmlspecialchars($diff->render($renderer));
89-
?>
90-
</pre>
80+
<?php
81+
// Generate a context diff.
82+
// jblond\Diff\Renderer\Text\Context
83+
$renderer = new Context();
84+
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
85+
?>
9186
</body>
9287
</html>

lib/jblond/Diff.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Diff
5555

5656
/**
5757
* @var array Associative array containing the options that will be applied for generating the diff.
58-
* The key-value pairs are set at the contructor of this class.
58+
* The key-value pairs are set at the constructor of this class.
5959
* @see Diff::setOptions()
6060
*/
6161
private $options = [];
@@ -65,7 +65,7 @@ class Diff
6565
*
6666
* The first two parameters define the data to compare to eachother.
6767
* The values can be of type string or array.
68-
* If the type is string, it's splitted into array elements by line-end characters.
68+
* If the type is string, it's split into array elements by line-end characters.
6969
*
7070
* Options for comparison can be set by using the third parameter. The format of this value is expected to be a
7171
* associative array where each key-value pair represents an option and its value (E.g. ['context' => 3], ...).
@@ -214,14 +214,14 @@ public function getArgumentType($var): int
214214
*/
215215
public function getGroupedOpcodes(): array
216216
{
217-
if (!is_null($this->groupedCodes)) {
217+
if ($this->groupedCodes !== null) {
218218
//Return the cached results.
219219
return $this->groupedCodes;
220220
}
221221

222-
//Get and cahche the grouped op-codes.
222+
//Get and cache the grouped op-codes.
223223
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options, null);
224-
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']);
224+
$this->groupedCodes = $sequenceMatcher->getGroupedOpCodes($this->options['context']);
225225

226226
return $this->groupedCodes;
227227
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ public function renderHtml(array $changes, object $htmlRenderer): string
6060

6161
foreach ($changes as $i => $blocks) {
6262
if ($i > 0) {
63-
// If this is a separate block, we're condensing code so output ...,
64-
// indicating a significant portion of the code has been collapsed as
65-
// it is the same.
66-
//TODO: When is $i > 0 ?
67-
$html .= '<span class="Skipped"><br>&hellip;<br></span>';
63+
// If this is a separate block, we're condensing code to output …,
64+
// indicating a significant portion of the code has been collapsed as it did not change.
65+
$html .= $htmlRenderer->generateTableRowsSkipped();
6866
}
6967

7068
foreach ($blocks as $change) {

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

+46-30
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*
1010
* PHP version 7.2 or greater
1111
*
12-
* @package jblond\Diff\Renderer\Html
13-
* @author Chris Boulton <[email protected]>
12+
* @package jblond\Diff\Renderer\Html
13+
* @author Chris Boulton <[email protected]>
1414
* @copyright (c) 2009 Chris Boulton
15-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
16-
* @version 1.15
17-
* @link https://github.com/JBlond/php-diff
15+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
16+
* @version 1.15
17+
* @link https://github.com/JBlond/php-diff
1818
*/
1919
class Inline extends HtmlArray
2020
{
@@ -50,20 +50,36 @@ public function generateTableHeader(): string
5050
HTML;
5151
}
5252

53+
/**
54+
* Generates a string representation of table rows showing lines are skipped.
55+
*
56+
* @return string HTML code representation of a table's header.
57+
*/
58+
public function generateTableRowsSkipped(): string
59+
{
60+
return <<<HTML
61+
<tr>
62+
<th>&hellip;</th>
63+
<th>&hellip;</th>
64+
<td class="Left Skipped">&hellip;</td>
65+
</tr>
66+
HTML;
67+
}
68+
5369
/**
5470
* Generates a string representation of table rows showing text with no difference.
5571
*
56-
* @param array $change Contains the op-codes about the changes between two blocks.
72+
* @param array $changes Contains the op-codes about the changes between two blocks.
5773
*
5874
* @return string HTML code representing table rows showing text with no difference.
5975
*/
60-
public function generateTableRowsEqual(array $change): string
76+
public function generateTableRowsEqual(array $changes): string
6177
{
6278
$html = '';
6379

64-
foreach ($change['base']['lines'] as $no => $line) {
65-
$fromLine = $change['base']['offset'] + $no + 1;
66-
$toLine = $change['changed']['offset'] + $no + 1;
80+
foreach ($changes['base']['lines'] as $lineNo => $line) {
81+
$fromLine = $changes['base']['offset'] + $lineNo + 1;
82+
$toLine = $changes['changed']['offset'] + $lineNo + 1;
6783

6884
$html .= <<<HTML
6985
<tr>
@@ -80,24 +96,24 @@ public function generateTableRowsEqual(array $change): string
8096
/**
8197
* Generates a string representation of table rows showing added text.
8298
*
83-
* @param array $change Contains the op-codes about the changes between two blocks of text.
99+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
84100
*
85101
* @return string HTML code representing table rows showing with added text.
86102
*/
87-
public function generateTableRowsInsert(array $change): string
103+
public function generateTableRowsInsert(array $changes): string
88104
{
89105
$html = '';
90106

91-
foreach ($change['changed']['lines'] as $no => $line) {
92-
$toLine = $change['changed']['offset'] + $no + 1;
107+
foreach ($changes['changed']['lines'] as $lineNo => $line) {
108+
$toLine = $changes['changed']['offset'] + $lineNo + 1;
93109

94110
$html .= <<<HTML
95111
<tr>
96-
<th>&#xA0;</th>
112+
<th>&nbsp;</th>
97113
<th>$toLine</th>
98114
<td class="Right">
99115
<ins>$line</ins>
100-
&#xA0;
116+
&nbsp;
101117
</td>
102118
</tr>
103119
HTML;
@@ -109,24 +125,24 @@ public function generateTableRowsInsert(array $change): string
109125
/**
110126
* Generates a string representation of table rows showing removed text.
111127
*
112-
* @param array $change Contains the op-codes about the changes between two blocks of text.
128+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
113129
*
114130
* @return string HTML code representing table rows showing removed text.
115131
*/
116-
public function generateTableRowsDelete(array $change): string
132+
public function generateTableRowsDelete(array $changes): string
117133
{
118134
$html = '';
119135

120-
foreach ($change['base']['lines'] as $no => $line) {
121-
$fromLine = $change['base']['offset'] + $no + 1;
136+
foreach ($changes['base']['lines'] as $lineNo => $line) {
137+
$fromLine = $changes['base']['offset'] + $lineNo + 1;
122138

123139
$html .= <<<HTML
124140
<tr>
125141
<th>$fromLine</th>
126-
<th>&#xA0;</th>
142+
<th>&nbsp;</th>
127143
<td class="Left">
128144
<del>$line</del>
129-
&#xA0;
145+
&nbsp;
130146
</td>
131147
</tr>
132148
HTML;
@@ -138,34 +154,34 @@ public function generateTableRowsDelete(array $change): string
138154
/**
139155
* Generates a string representation of table rows showing partially modified text.
140156
*
141-
* @param array $change Contains the op-codes about the changes between two blocks of text.
157+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
142158
*
143159
* @return string Html code representing table rows showing modified text.
144160
*/
145-
public function generateTableRowsReplace(array &$change): string
161+
public function generateTableRowsReplace(array $changes): string
146162
{
147163
$html = '';
148164

149-
foreach ($change['base']['lines'] as $no => $line) {
150-
$fromLine = $change['base']['offset'] + $no + 1;
165+
foreach ($changes['base']['lines'] as $lineNo => $line) {
166+
$fromLine = $changes['base']['offset'] + $lineNo + 1;
151167

152168
$html .= <<<HTML
153169
<tr>
154170
<th>$fromLine</th>
155-
<th>&#xA0;</th>
171+
<th>&nbsp;</th>
156172
<td class="Left">
157173
<span>$line</span>
158174
</td>
159175
</tr>
160176
HTML;
161177
}
162178

163-
foreach ($change['changed']['lines'] as $no => $line) {
164-
$toLine = $change['changed']['offset'] + $no + 1;
179+
foreach ($changes['changed']['lines'] as $lineNo => $line) {
180+
$toLine = $changes['changed']['offset'] + $lineNo + 1;
165181

166182
$html .= <<<HTML
167183
<tr>
168-
<th>&#xA0;</th>
184+
<th>&nbsp;</th>
169185
<th>$toLine</th>
170186
<td class="Right">
171187
<span>$line</span>

0 commit comments

Comments
 (0)