@@ -112,7 +112,7 @@ public function generateSkippedLines(): string
112
112
113
113
return <<<HTML
114
114
<tr>
115
- <th class=" $ headerClass" title=" { $ this ->lastDeleted } "> $ marker</th>
115
+ <th class=" $ headerClass" title=" $ this ->lastDeleted "> $ marker</th>
116
116
<td class="Skipped">…</td>
117
117
</tr>
118
118
HTML ;
@@ -136,7 +136,7 @@ public function generateLinesEqual(array $changes): string
136
136
137
137
$ html .= <<<HTML
138
138
<tr>
139
- <th class=" $ headerClass" title=" { $ this ->lastDeleted } "> $ fromLine</th>
139
+ <th class=" $ headerClass" title=" $ this ->lastDeleted "> $ fromLine</th>
140
140
<td> $ line</td>
141
141
</tr>
142
142
HTML ;
@@ -165,7 +165,7 @@ public function generateLinesInsert(array $changes): string
165
165
166
166
$ html .= <<<HTML
167
167
<tr>
168
- <th class=" $ headerClass" title=" { $ this ->lastDeleted } "> $ toLine</th>
168
+ <th class=" $ headerClass" title=" $ this ->lastDeleted "> $ toLine</th>
169
169
<td><ins> $ line</ins></td>
170
170
</tr>
171
171
HTML ;
@@ -208,41 +208,68 @@ public function generateLinesDelete(array $changes): string
208
208
*/
209
209
public function generateLinesReplace (array $ changes ): string
210
210
{
211
- $ html = '' ;
212
- $ headerClass = '' ;
213
-
214
- foreach ($ changes ['base ' ]['lines ' ] as $ lineNo => $ line ) {
215
- $ fromLine = $ changes ['base ' ]['offset ' ] + $ lineNo + 1 + $ this ->lineOffset ;
216
- if (!$ lineNo && $ this ->lastDeleted !== null ) {
217
- $ headerClass = 'ChangeDelete ' ;
211
+ $ html = '' ;
212
+ $ baseLineCount = count ($ changes ['base ' ]['lines ' ]);
213
+ $ changedLineCount = count ($ changes ['changed ' ]['lines ' ]);
214
+
215
+ if (count ($ changes ['base ' ]['lines ' ]) == $ changedLineCount ) {
216
+ // Lines of Version 1 are modified at version 2.
217
+ foreach ($ changes ['base ' ]['lines ' ] as $ lineNo => $ line ) {
218
+ $ fromLine = $ changes ['base ' ]['offset ' ] + $ lineNo + 1 + $ this ->lineOffset ;
219
+
220
+ // Capture line-parts which are added to the same line at version 2.
221
+ $ addedParts = [];
222
+ preg_match_all ('/\x0.*?\x1/ ' , $ changes ['changed ' ]['lines ' ][$ lineNo ], $ addedParts , PREG_PATTERN_ORDER );
223
+ array_unshift ($ addedParts [0 ], '' );
224
+
225
+ // Inline Replacement:
226
+ // Concatenate line-parts which are removed at version2 with line-parts which are added at version 2.
227
+ $ line = preg_replace_callback (
228
+ '/\x0.*?\x1/ ' ,
229
+ function ($ removedParts ) use ($ addedParts ) {
230
+ $ addedPart = str_replace (["\0" , "\1" ], $ this ->options ['insertMarkers ' ], next ($ addedParts [0 ]));
231
+ $ removedPart = str_replace (["\0" , "\1" ], $ this ->options ['deleteMarkers ' ], $ removedParts [0 ]);
232
+
233
+ return "$ removedPart$ addedPart " ;
234
+ },
235
+ $ line
236
+ );
237
+
238
+ $ html .= <<<HTML
239
+ <tr>
240
+ <th> $ fromLine</th>
241
+ <td> $ line</td>
242
+ </tr>
243
+ HTML ;
218
244
}
219
245
220
- // Capture added parts.
221
- $ addedParts = [];
222
- preg_match_all ('/\x0.*?\x1/ ' , $ changes ['changed ' ]['lines ' ][$ lineNo ], $ addedParts , PREG_PATTERN_ORDER );
223
- array_unshift ($ addedParts [0 ], '' );
246
+ return $ html ;
247
+ }
224
248
225
- // Concatenate removed parts with added parts.
226
- $ line = preg_replace_callback (
227
- '/\x0.*?\x1/ ' ,
228
- function ($ removedParts ) use ($ addedParts ) {
229
- $ addedPart = str_replace (["\0" , "\1" ], $ this ->options ['insertMarkers ' ], next ($ addedParts [0 ]));
230
- $ removedPart = str_replace (["\0" , "\1" ], $ this ->options ['deleteMarkers ' ], $ removedParts [0 ]);
249
+ // More or less lines at version 2. Block of version 1 is replaced by block of version 2.
250
+ $ title = '' ;
231
251
232
- return "$ removedPart$ addedPart " ;
233
- },
234
- $ line
235
- );
252
+ foreach ($ changes ['changed ' ]['lines ' ] as $ lineNo => $ line ) {
253
+ $ toLine = $ changes ['changed ' ]['offset ' ] + $ lineNo + 1 ;
236
254
237
- $ html .= <<<HTML
255
+ if (!$ lineNo ) {
256
+ $ title = "Lines replaced at {$ this ->options ['title1 ' ]}: \n" ;
257
+ foreach ($ changes ['base ' ]['lines ' ] as $ baseLineNo => $ baseLine ) {
258
+ $ title .= $ changes ['base ' ]['offset ' ] + $ baseLineNo + 1 . ": $ baseLine \n" ;
259
+ }
260
+ }
261
+
262
+ $ title = htmlentities ($ title );
263
+ $ html .= <<<HTML
238
264
<tr>
239
- <th class=" $ headerClass " title=" { $ this -> lastDeleted } "> $ fromLine </th>
240
- <td> $ line</td>
265
+ <th class="ChangeReplace " title=" $ title "> $ toLine </th>
266
+ <td class="ChangeReplace" > $ line</td>
241
267
</tr>
242
268
HTML ;
243
- $ this ->lastDeleted = null ;
244
269
}
245
270
271
+ $ this ->lineOffset = $ this ->lineOffset + $ changedLineCount - $ baseLineCount ;
272
+
246
273
return $ html ;
247
274
}
248
275
0 commit comments