| 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 |
|
| 43 |
require_once dirname(__FILE__).'/Array.php'; |
| 44 |
|
| 45 |
class WPR_Diff_Renderer_Html_Inline extends WPR_Diff_Renderer_Html_Array |
| 46 |
{ |
| 47 |
/** |
| 48 |
* Render a and return diff with changes between the two sequences |
| 49 |
* displayed inline (under each other) |
| 50 |
* |
| 51 |
* @return string The generated inline diff. |
| 52 |
*/ |
| 53 |
public function render() |
| 54 |
{ |
| 55 |
$changes = parent::render(); |
| 56 |
$html = ''; |
| 57 |
if(empty($changes)) { |
| 58 |
return $html; |
| 59 |
} |
| 60 |
|
| 61 |
$html .= '<table class="Differences DifferencesInline">'; |
| 62 |
$html .= '<thead>'; |
| 63 |
$html .= '<tr>'; |
| 64 |
$html .= '<th>Old</th>'; |
| 65 |
$html .= '<th>New</th>'; |
| 66 |
$html .= '<th>Differences</th>'; |
| 67 |
$html .= '</tr>'; |
| 68 |
$html .= '</thead>'; |
| 69 |
foreach($changes as $i => $blocks) { |
| 70 |
// If this is a separate block, we're condensing code so output ..., |
| 71 |
// indicating a significant portion of the code has been collapsed as |
| 72 |
// it is the same |
| 73 |
if($i > 0) { |
| 74 |
$html .= '<tbody class="Skipped">'; |
| 75 |
$html .= '<th>…</th>'; |
| 76 |
$html .= '<th>…</th>'; |
| 77 |
$html .= '<td> </td>'; |
| 78 |
$html .= '</tbody>'; |
| 79 |
} |
| 80 |
|
| 81 |
foreach($blocks as $change) { |
| 82 |
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">'; |
| 83 |
// Equal changes should be shown on both sides of the diff |
| 84 |
if($change['tag'] == 'equal') { |
| 85 |
foreach($change['base']['lines'] as $no => $line) { |
| 86 |
$fromLine = $change['base']['offset'] + $no + 1; |
| 87 |
$toLine = $change['changed']['offset'] + $no + 1; |
| 88 |
$html .= '<tr>'; |
| 89 |
$html .= '<th>'.$fromLine.'</th>'; |
| 90 |
$html .= '<th>'.$toLine.'</th>'; |
| 91 |
$html .= '<td class="Left">'.$line.'</td>'; |
| 92 |
$html .= '</tr>'; |
| 93 |
} |
| 94 |
} |
| 95 |
// Added lines only on the right side |
| 96 |
else if($change['tag'] == 'insert') { |
| 97 |
foreach($change['changed']['lines'] as $no => $line) { |
| 98 |
$toLine = $change['changed']['offset'] + $no + 1; |
| 99 |
$html .= '<tr>'; |
| 100 |
$html .= '<th> </th>'; |
| 101 |
$html .= '<th>'.$toLine.'</th>'; |
| 102 |
$html .= '<td class="Right"><ins>'.$line.'</ins> </td>'; |
| 103 |
$html .= '</tr>'; |
| 104 |
} |
| 105 |
} |
| 106 |
// Show deleted lines only on the left side |
| 107 |
else if($change['tag'] == 'delete') { |
| 108 |
foreach($change['base']['lines'] as $no => $line) { |
| 109 |
$fromLine = $change['base']['offset'] + $no + 1; |
| 110 |
$html .= '<tr>'; |
| 111 |
$html .= '<th>'.$fromLine.'</th>'; |
| 112 |
$html .= '<th> </th>'; |
| 113 |
$html .= '<td class="Left"><del>'.$line.'</del> </td>'; |
| 114 |
$html .= '</tr>'; |
| 115 |
} |
| 116 |
} |
| 117 |
// Show modified lines on both sides |
| 118 |
else if($change['tag'] == 'replace') { |
| 119 |
foreach($change['base']['lines'] as $no => $line) { |
| 120 |
$fromLine = $change['base']['offset'] + $no + 1; |
| 121 |
$html .= '<tr>'; |
| 122 |
$html .= '<th>'.$fromLine.'</th>'; |
| 123 |
$html .= '<th> </th>'; |
| 124 |
$html .= '<td class="Left"><span>'.$line.'</span></td>'; |
| 125 |
$html .= '</tr>'; |
| 126 |
} |
| 127 |
|
| 128 |
foreach($change['changed']['lines'] as $no => $line) { |
| 129 |
$toLine = $change['changed']['offset'] + $no + 1; |
| 130 |
$html .= '<tr>'; |
| 131 |
$html .= '<th> </th>'; |
| 132 |
$html .= '<th>'.$toLine.'</th>'; |
| 133 |
$html .= '<td class="Right"><span>'.$line.'</span></td>'; |
| 134 |
$html .= '</tr>'; |
| 135 |
} |
| 136 |
} |
| 137 |
$html .= '</tbody>'; |
| 138 |
} |
| 139 |
} |
| 140 |
$html .= '</table>'; |
| 141 |
return $html; |
| 142 |
} |
| 143 |
} |
| 144 |
|