DiffRendererHtmlInline.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\components;
  8. /**
  9. * Renders diff to HTML. Output adjusted to be copy-paste friendly.
  10. *
  11. * @author Alexander Makarov <sam@rmcreative.ru>
  12. * @since 2.0
  13. */
  14. class DiffRendererHtmlInline extends \Diff_Renderer_Html_Array
  15. {
  16. /**
  17. * Render a and return diff with changes between the two sequences
  18. * displayed inline (under each other)
  19. *
  20. * @return string The generated inline diff.
  21. */
  22. public function render()
  23. {
  24. $changes = parent::render();
  25. $html = '';
  26. if (empty($changes)) {
  27. return $html;
  28. }
  29. $html .= <<<HTML
  30. <table class="Differences DifferencesInline">
  31. <thead>
  32. <tr>
  33. <th>Old</th>
  34. <th>New</th>
  35. <th>Differences</th>
  36. </tr>
  37. </thead>
  38. HTML;
  39. foreach ($changes as $i => $blocks) {
  40. // If this is a separate block, we're condensing code so output ...,
  41. // indicating a significant portion of the code has been collapsed as
  42. // it is the same
  43. if ($i > 0) {
  44. $html .= <<<HTML
  45. <tbody class="Skipped">
  46. <th data-line-number="&hellip;"></th>
  47. <th data-line-number="&hellip;"></th>
  48. <td>&nbsp;</td>
  49. </tbody>
  50. HTML;
  51. }
  52. foreach ($blocks as $change) {
  53. $tag = ucfirst($change['tag']);
  54. $html .= <<<HTML
  55. <tbody class="Change{$tag}">
  56. HTML;
  57. // Equal changes should be shown on both sides of the diff
  58. if ($change['tag'] === 'equal') {
  59. foreach ($change['base']['lines'] as $no => $line) {
  60. $fromLine = $change['base']['offset'] + $no + 1;
  61. $toLine = $change['changed']['offset'] + $no + 1;
  62. $html .= <<<HTML
  63. <tr>
  64. <th data-line-number="{$fromLine}"></th>
  65. <th data-line-number="{$toLine}"></th>
  66. <td class="Left">{$line}</td>
  67. </tr>
  68. HTML;
  69. }
  70. }
  71. // Added lines only on the right side
  72. elseif ($change['tag'] === 'insert') {
  73. foreach ($change['changed']['lines'] as $no => $line) {
  74. $toLine = $change['changed']['offset'] + $no + 1;
  75. $html .= <<<HTML
  76. <tr>
  77. <th data-line-number="&nbsp;"></th>
  78. <th data-line-number="{$toLine}"></th>
  79. <td class="Right"><ins>{$line}</ins>&nbsp;</td>
  80. </tr>
  81. HTML;
  82. }
  83. }
  84. // Show deleted lines only on the left side
  85. elseif ($change['tag'] === 'delete') {
  86. foreach ($change['base']['lines'] as $no => $line) {
  87. $fromLine = $change['base']['offset'] + $no + 1;
  88. $html .= <<<HTML
  89. <tr>
  90. <th data-line-number="{$fromLine}"></th>
  91. <th data-line-number="&nbsp;"></th>
  92. <td class="Left"><del>{$line}</del>&nbsp;</td>
  93. </tr>
  94. HTML;
  95. }
  96. }
  97. // Show modified lines on both sides
  98. elseif ($change['tag'] === 'replace') {
  99. foreach ($change['base']['lines'] as $no => $line) {
  100. $fromLine = $change['base']['offset'] + $no + 1;
  101. $html .= <<<HTML
  102. <tr>
  103. <th data-line-number="{$fromLine}"></th>
  104. <th data-line-number="&nbsp;"></th>
  105. <td class="Left"><span>{$line}</span></td>
  106. </tr>
  107. HTML;
  108. }
  109. foreach ($change['changed']['lines'] as $no => $line) {
  110. $toLine = $change['changed']['offset'] + $no + 1;
  111. $html .= <<<HTML
  112. <tr>
  113. <th data-line-number="{$toLine}"></th>
  114. <th data-line-number="&nbsp;"></th>
  115. <td class="Right"><span>{$line}</span></td>
  116. </tr>
  117. HTML;
  118. }
  119. }
  120. $html .= <<<HTML
  121. </tbody>
  122. HTML;
  123. }
  124. }
  125. $html .= <<<HTML
  126. </table>
  127. HTML;
  128. return $html;
  129. }
  130. }