You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.0KB

  1. <?php
  2. /**
  3. * Inline HTML diff generator for PHP DiffLib.
  4. *
  5. * PHP version 5
  6. *
  7. * Copyright (c) 2009 Chris Boulton <chris.boulton@interspire.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the Chris Boulton nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package DiffLib
  36. * @author Chris Boulton <chris.boulton@interspire.com>
  37. * @copyright (c) 2009 Chris Boulton
  38. * @license New BSD License http://www.opensource.org/licenses/bsd-license.php
  39. * @version 1.1
  40. * @link http://github.com/chrisboulton/php-diff
  41. */
  42. require_once dirname(__FILE__).'/Array.php';
  43. class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array
  44. {
  45. /**
  46. * Render a and return diff with changes between the two sequences
  47. * displayed inline (under each other)
  48. *
  49. * @return string The generated inline diff.
  50. */
  51. public function render()
  52. {
  53. $changes = parent::render();
  54. $html = '';
  55. if(empty($changes)) {
  56. return $html;
  57. }
  58. $html .= '<table class="Differences DifferencesInline">';
  59. $html .= '<thead>';
  60. $html .= '<tr>';
  61. $html .= '<th>Old</th>';
  62. $html .= '<th>New</th>';
  63. $html .= '<th>Differences</th>';
  64. $html .= '</tr>';
  65. $html .= '</thead>';
  66. foreach($changes as $i => $blocks) {
  67. // If this is a separate block, we're condensing code so output ...,
  68. // indicating a significant portion of the code has been collapsed as
  69. // it is the same
  70. if($i > 0) {
  71. $html .= '<tbody class="Skipped">';
  72. $html .= '<th>&hellip;</th>';
  73. $html .= '<th>&hellip;</th>';
  74. $html .= '<td>&nbsp;</td>';
  75. $html .= '</tbody>';
  76. }
  77. foreach($blocks as $change) {
  78. $html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
  79. // Equal changes should be shown on both sides of the diff
  80. if($change['tag'] == 'equal') {
  81. foreach($change['base']['lines'] as $no => $line) {
  82. $fromLine = $change['base']['offset'] + $no + 1;
  83. $toLine = $change['changed']['offset'] + $no + 1;
  84. $html .= '<tr>';
  85. $html .= '<th>'.$fromLine.'</th>';
  86. $html .= '<th>'.$toLine.'</th>';
  87. $html .= '<td class="Left">'.$line.'</td>';
  88. $html .= '</tr>';
  89. }
  90. }
  91. // Added lines only on the right side
  92. else if($change['tag'] == 'insert') {
  93. foreach($change['changed']['lines'] as $no => $line) {
  94. $toLine = $change['changed']['offset'] + $no + 1;
  95. $html .= '<tr>';
  96. $html .= '<th>&nbsp;</th>';
  97. $html .= '<th>'.$toLine.'</th>';
  98. $html .= '<td class="Right"><ins>'.$line.'</ins>&nbsp;</td>';
  99. $html .= '</tr>';
  100. }
  101. }
  102. // Show deleted lines only on the left side
  103. else if($change['tag'] == 'delete') {
  104. foreach($change['base']['lines'] as $no => $line) {
  105. $fromLine = $change['base']['offset'] + $no + 1;
  106. $html .= '<tr>';
  107. $html .= '<th>'.$fromLine.'</th>';
  108. $html .= '<th>&nbsp;</th>';
  109. $html .= '<td class="Left"><del>'.$line.'</del>&nbsp;</td>';
  110. $html .= '</tr>';
  111. }
  112. }
  113. // Show modified lines on both sides
  114. else if($change['tag'] == 'replace') {
  115. foreach($change['base']['lines'] as $no => $line) {
  116. $fromLine = $change['base']['offset'] + $no + 1;
  117. $html .= '<tr>';
  118. $html .= '<th>'.$fromLine.'</th>';
  119. $html .= '<th>&nbsp;</th>';
  120. $html .= '<td class="Left"><span>'.$line.'</span></td>';
  121. $html .= '</tr>';
  122. }
  123. foreach($change['changed']['lines'] as $no => $line) {
  124. $toLine = $change['changed']['offset'] + $no + 1;
  125. $html .= '<tr>';
  126. $html .= '<th>&nbsp;</th>';
  127. $html .= '<th>'.$toLine.'</th>';
  128. $html .= '<td class="Right"><span>'.$line.'</span></td>';
  129. $html .= '</tr>';
  130. }
  131. }
  132. $html .= '</tbody>';
  133. }
  134. }
  135. $html .= '</table>';
  136. return $html;
  137. }
  138. }