| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Renderer\Html; |
| 6 |
|
| 7 |
use Jfcherng\Diff\Differ; |
| 8 |
use Jfcherng\Diff\Factory\LineRendererFactory; |
| 9 |
use Jfcherng\Diff\Renderer\AbstractRenderer; |
| 10 |
use Jfcherng\Diff\Renderer\Html\LineRenderer\AbstractLineRenderer; |
| 11 |
use Jfcherng\Diff\Renderer\RendererConstant; |
| 12 |
use Jfcherng\Diff\SequenceMatcher; |
| 13 |
use Jfcherng\Utility\MbString; |
| 14 |
|
| 15 |
/** |
| 16 |
* Base renderer for rendering HTML-based diffs. |
| 17 |
*/ |
| 18 |
abstract class AbstractHtml extends AbstractRenderer |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var bool is this renderer pure text? |
| 22 |
*/ |
| 23 |
public const IS_TEXT_RENDERER = false; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string[] array of the different opcodes and how they are mapped to HTML classes |
| 27 |
* |
| 28 |
* @todo rename to OP_CLASS_MAP in v7 |
| 29 |
*/ |
| 30 |
public const TAG_CLASS_MAP = [ |
| 31 |
SequenceMatcher::OP_DEL => 'del', |
| 32 |
SequenceMatcher::OP_EQ => 'eq', |
| 33 |
SequenceMatcher::OP_INS => 'ins', |
| 34 |
SequenceMatcher::OP_REP => 'rep', |
| 35 |
]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Auto format the content in "changes" to be suitable for HTML output. |
| 39 |
* |
| 40 |
* This may not be a wanted behavior for some (custom) renderers |
| 41 |
* if they want to do this by themselves in a later stage. |
| 42 |
* |
| 43 |
* @var bool |
| 44 |
*/ |
| 45 |
public const AUTO_FORMAT_CHANGES = true; |
| 46 |
|
| 47 |
public function getResultForIdenticalsDefault(): string |
| 48 |
{ |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Render and return an array structure suitable for generating HTML |
| 54 |
* based differences. Generally called by subclasses that generate a |
| 55 |
* HTML based diff and return an array of the changes to show in the diff. |
| 56 |
* |
| 57 |
* @param Differ $differ the differ object |
| 58 |
* |
| 59 |
* @return array[][] generated changes, suitable for presentation in HTML |
| 60 |
*/ |
| 61 |
public function getChanges(Differ $differ): array |
| 62 |
{ |
| 63 |
$lineRenderer = LineRendererFactory::make( |
| 64 |
$this->options['detailLevel'], |
| 65 |
$differ->getOptions(), |
| 66 |
$this->options, |
| 67 |
); |
| 68 |
|
| 69 |
$old = $differ->getOld(); |
| 70 |
$new = $differ->getNew(); |
| 71 |
|
| 72 |
$changes = []; |
| 73 |
|
| 74 |
foreach ($differ->getGroupedOpcodes() as $hunk) { |
| 75 |
$change = []; |
| 76 |
|
| 77 |
foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) { |
| 78 |
$change[] = $this->getDefaultBlock($op, $i1, $j1); |
| 79 |
$block = &$change[\count($change) - 1]; |
| 80 |
|
| 81 |
// if there are same amount of lines replaced |
| 82 |
// we can render the inner detailed changes with corresponding lines |
| 83 |
// @todo or use LineRenderer to do the job regardless different line counts? |
| 84 |
if ($op === SequenceMatcher::OP_REP && $i2 - $i1 === $j2 - $j1) { |
| 85 |
for ($k = $i2 - $i1 - 1; $k >= 0; --$k) { |
| 86 |
$this->renderChangedExtent($lineRenderer, $old[$i1 + $k], $new[$j1 + $k]); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$block['old']['lines'] = \array_slice($old, $i1, $i2 - $i1); |
| 91 |
$block['new']['lines'] = \array_slice($new, $j1, $j2 - $j1); |
| 92 |
} |
| 93 |
unset($block); |
| 94 |
|
| 95 |
$changes[] = $change; |
| 96 |
} |
| 97 |
|
| 98 |
if (static::AUTO_FORMAT_CHANGES) { |
| 99 |
$this->formatChanges($changes); |
| 100 |
} |
| 101 |
|
| 102 |
return $changes; |
| 103 |
} |
| 104 |
|
| 105 |
protected function renderWorker(Differ $differ): string |
| 106 |
{ |
| 107 |
$rendered = $this->redererChanges($this->getChanges($differ)); |
| 108 |
|
| 109 |
return $this->cleanUpDummyHtmlClosures($rendered); |
| 110 |
} |
| 111 |
|
| 112 |
protected function renderArrayWorker(array $differArray): string |
| 113 |
{ |
| 114 |
$this->ensureChangesUseIntTag($differArray); |
| 115 |
|
| 116 |
$rendered = $this->redererChanges($differArray); |
| 117 |
|
| 118 |
return $this->cleanUpDummyHtmlClosures($rendered); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Render the array of changes. |
| 123 |
* |
| 124 |
* @param array[][] $changes the changes |
| 125 |
* |
| 126 |
* @todo rename typo to renderChanges() in v7 |
| 127 |
*/ |
| 128 |
abstract protected function redererChanges(array $changes): string; |
| 129 |
|
| 130 |
/** |
| 131 |
* Renderer the changed extent. |
| 132 |
* |
| 133 |
* @param AbstractLineRenderer $lineRenderer the line renderer |
| 134 |
* @param string $old the old line |
| 135 |
* @param string $new the new line |
| 136 |
*/ |
| 137 |
protected function renderChangedExtent(AbstractLineRenderer $lineRenderer, string &$old, string &$new): void |
| 138 |
{ |
| 139 |
static $mbOld, $mbNew; |
| 140 |
|
| 141 |
$mbOld ??= new MbString(); |
| 142 |
$mbNew ??= new MbString(); |
| 143 |
|
| 144 |
$mbOld->set($old); |
| 145 |
$mbNew->set($new); |
| 146 |
|
| 147 |
$lineRenderer->render($mbOld, $mbNew); |
| 148 |
|
| 149 |
$old = $mbOld->get(); |
| 150 |
$new = $mbNew->get(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get the default block. |
| 155 |
* |
| 156 |
* @param int $op the operation |
| 157 |
* @param int $i1 begin index of the diff of the old array |
| 158 |
* @param int $j1 begin index of the diff of the new array |
| 159 |
* |
| 160 |
* @return array the default block |
| 161 |
* |
| 162 |
* @todo rename tag to op in v7 |
| 163 |
*/ |
| 164 |
protected function getDefaultBlock(int $op, int $i1, int $j1): array |
| 165 |
{ |
| 166 |
return [ |
| 167 |
'tag' => $op, |
| 168 |
'old' => [ |
| 169 |
'offset' => $i1, |
| 170 |
'lines' => [], |
| 171 |
], |
| 172 |
'new' => [ |
| 173 |
'offset' => $j1, |
| 174 |
'lines' => [], |
| 175 |
], |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Make the content in "changes" suitable for HTML output. |
| 181 |
* |
| 182 |
* @param array[][] $changes the changes |
| 183 |
*/ |
| 184 |
final protected function formatChanges(array &$changes): void |
| 185 |
{ |
| 186 |
foreach ($changes as &$hunk) { |
| 187 |
foreach ($hunk as &$block) { |
| 188 |
$block['old']['lines'] = $this->formatLines($block['old']['lines']); |
| 189 |
$block['new']['lines'] = $this->formatLines($block['new']['lines']); |
| 190 |
|
| 191 |
/** @phan-suppress-next-line PhanTypeInvalidLeftOperandOfBitwiseOp */ |
| 192 |
if ($block['tag'] & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL)) { |
| 193 |
$block['old']['lines'] = str_replace( |
| 194 |
RendererConstant::HTML_CLOSURES, |
| 195 |
RendererConstant::HTML_CLOSURES_DEL, |
| 196 |
$block['old']['lines'], |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
/** @phan-suppress-next-line PhanTypeInvalidLeftOperandOfBitwiseOp */ |
| 201 |
if ($block['tag'] & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) { |
| 202 |
$block['new']['lines'] = str_replace( |
| 203 |
RendererConstant::HTML_CLOSURES, |
| 204 |
RendererConstant::HTML_CLOSURES_INS, |
| 205 |
$block['new']['lines'], |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Make a series of lines suitable for outputting in a HTML rendered diff. |
| 214 |
* |
| 215 |
* @param string[] $lines array of lines to format |
| 216 |
* |
| 217 |
* @return string[] array of the formatted lines |
| 218 |
*/ |
| 219 |
protected function formatLines(array $lines): array |
| 220 |
{ |
| 221 |
/** |
| 222 |
* To prevent from invoking the same function calls for several times, |
| 223 |
* we can glue lines into a string and call functions for one time. |
| 224 |
* After that, we split the string back into lines. |
| 225 |
*/ |
| 226 |
return explode( |
| 227 |
RendererConstant::IMPLODE_DELIMITER, |
| 228 |
$this->formatStringFromLines( |
| 229 |
implode( |
| 230 |
RendererConstant::IMPLODE_DELIMITER, |
| 231 |
$lines, |
| 232 |
), |
| 233 |
), |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Make a string suitable for outputting in a HTML rendered diff. |
| 239 |
* |
| 240 |
* This my involve replacing tab characters with spaces, making the HTML safe |
| 241 |
* for output, ensuring that double spaces are replaced with etc. |
| 242 |
* |
| 243 |
* @param string $string the string of imploded lines |
| 244 |
* |
| 245 |
* @return string the formatted string |
| 246 |
*/ |
| 247 |
protected function formatStringFromLines(string $string): string |
| 248 |
{ |
| 249 |
if (!$this->options['spaceToHtmlTag']) { |
| 250 |
$string = $this->expandTabs($string, $this->options['tabSize']); |
| 251 |
} |
| 252 |
|
| 253 |
$string = $this->htmlSafe($string); |
| 254 |
|
| 255 |
if ($this->options['spacesToNbsp']) { |
| 256 |
$string = $this->htmlFixSpaces($string); |
| 257 |
} |
| 258 |
|
| 259 |
if ($this->options['spaceToHtmlTag']) { |
| 260 |
$string = $this->htmlReplaceSpacesToHtmlTag($string); |
| 261 |
} |
| 262 |
|
| 263 |
return $string; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Replace tabs in a string with a number of spaces. |
| 268 |
* |
| 269 |
* @param string $string the input string which may contain tabs |
| 270 |
* @param int $tabSize one tab = how many spaces, a negative does nothing |
| 271 |
* @param bool $onlyLeadingTabs only expand leading tabs |
| 272 |
* |
| 273 |
* @return string the string with the tabs converted to spaces |
| 274 |
*/ |
| 275 |
protected function expandTabs(string $string, int $tabSize = 4, bool $onlyLeadingTabs = false): string |
| 276 |
{ |
| 277 |
if ($tabSize < 0) { |
| 278 |
return $string; |
| 279 |
} |
| 280 |
|
| 281 |
if ($onlyLeadingTabs) { |
| 282 |
return preg_replace_callback( |
| 283 |
"/^[ \t]{1,}/mS", // tabs and spaces may be mixed |
| 284 |
static fn (array $matches): string => str_replace("\t", str_repeat(' ', $tabSize), $matches[0]), |
| 285 |
$string, |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
return str_replace("\t", str_repeat(' ', $tabSize), $string); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Make a string containing HTML safe for output on a page. |
| 294 |
* |
| 295 |
* @param string $string the string |
| 296 |
* |
| 297 |
* @return string the string with the HTML characters replaced by entities |
| 298 |
*/ |
| 299 |
protected function htmlSafe(string $string): string |
| 300 |
{ |
| 301 |
return htmlspecialchars($string, \ENT_NOQUOTES, 'UTF-8'); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Replace a string containing spaces with a HTML representation having " ". |
| 306 |
* |
| 307 |
* @param string $string the string of spaces |
| 308 |
* |
| 309 |
* @return string the HTML representation of the string |
| 310 |
*/ |
| 311 |
protected function htmlFixSpaces(string $string): string |
| 312 |
{ |
| 313 |
return str_replace(' ', ' ', $string); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Replace spaces/tabs with HTML tags, which may be styled in frontend with CSS. |
| 318 |
* |
| 319 |
* @param string $string the string of spaces |
| 320 |
* |
| 321 |
* @return string the HTML representation of the string |
| 322 |
*/ |
| 323 |
protected function htmlReplaceSpacesToHtmlTag(string $string): string |
| 324 |
{ |
| 325 |
return strtr($string, [ |
| 326 |
' ' => '<span class="ch sp"> </span>', |
| 327 |
"\t" => "<span class=\"ch tab\">\t</span>", |
| 328 |
]); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Make sure the "changes" array uses int "tag". |
| 333 |
* |
| 334 |
* Internally, we would like always int form for better performance. |
| 335 |
* |
| 336 |
* @param array[][] $changes the changes |
| 337 |
*/ |
| 338 |
protected function ensureChangesUseIntTag(array &$changes): void |
| 339 |
{ |
| 340 |
// check if the tag is already int type |
| 341 |
if (\is_int($changes[0][0]['tag'] ?? null)) { |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
foreach ($changes as &$hunks) { |
| 346 |
foreach ($hunks as &$block) { |
| 347 |
$block['tag'] = SequenceMatcher::opStrToInt($block['tag']); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Clean up empty HTML closures in the given string. |
| 354 |
* |
| 355 |
* @param string $string the string |
| 356 |
*/ |
| 357 |
protected function cleanUpDummyHtmlClosures(string $string): string |
| 358 |
{ |
| 359 |
return str_replace( |
| 360 |
[ |
| 361 |
RendererConstant::HTML_CLOSURES_DEL[0] . RendererConstant::HTML_CLOSURES_DEL[1], |
| 362 |
RendererConstant::HTML_CLOSURES_INS[0] . RendererConstant::HTML_CLOSURES_INS[1], |
| 363 |
], |
| 364 |
'', |
| 365 |
$string, |
| 366 |
); |
| 367 |
} |
| 368 |
} |