PluginProbe
WP Reset / 1.77
WP Reset v1.77
trunk 1.0 1.1 1.11 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.77 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.90 All 42 releases
wp-reset / libs / diff / Renderer / Html / Array.php

Array.php in WP Reset 1.77, at libs/diff/Renderer/Html/Array.php

226 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base renderer for rendering HTML based diffs 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__).'/../Abstract.php';
44
45 class WPR_Diff_Renderer_Html_Array extends WPR_Diff_Renderer_Abstract
46 {
47 /**
48 * @var array Array of the default options that apply to this renderer.
49 */
50 protected $defaultOptions = array(
51 'tabSize' => 4
52 );
53
54 /**
55 * Render and return an array structure suitable for generating HTML
56 * based differences. Generally called by subclasses that generate a
57 * HTML based diff and return an array of the changes to show in the diff.
58 *
59 * @return array An array of the generated chances, suitable for presentation in HTML.
60 */
61 public function render()
62 {
63 // As we'll be modifying a & b to include our change markers,
64 // we need to get the contents and store them here. That way
65 // we're not going to destroy the original data
66 $a = $this->diff->getA();
67 $b = $this->diff->getB();
68
69 $changes = array();
70 $opCodes = $this->diff->getGroupedOpcodes();
71 foreach($opCodes as $group) {
72 $blocks = array();
73 $lastTag = null;
74 $lastBlock = 0;
75 foreach($group as $code) {
76 list($tag, $i1, $i2, $j1, $j2) = $code;
77
78 if($tag == 'replace' && $i2 - $i1 == $j2 - $j1) {
79 for($i = 0; $i < ($i2 - $i1); ++$i) {
80 $fromLine = $a[$i1 + $i];
81 $toLine = $b[$j1 + $i];
82
83 list($start, $end) = $this->getChangeExtent($fromLine, $toLine);
84 if($start != 0 || $end != 0) {
85 $last = $end + strlen($fromLine);
86 $fromLine = substr_replace($fromLine, "\0", $start, 0);
87 $fromLine = substr_replace($fromLine, "\1", $last + 1, 0);
88 $last = $end + strlen($toLine);
89 $toLine = substr_replace($toLine, "\0", $start, 0);
90 $toLine = substr_replace($toLine, "\1", $last + 1, 0);
91 $a[$i1 + $i] = $fromLine;
92 $b[$j1 + $i] = $toLine;
93 }
94 }
95 }
96
97 if($tag != $lastTag) {
98 $blocks[] = array(
99 'tag' => $tag,
100 'base' => array(
101 'offset' => $i1,
102 'lines' => array()
103 ),
104 'changed' => array(
105 'offset' => $j1,
106 'lines' => array()
107 )
108 );
109 $lastBlock = count($blocks)-1;
110 }
111
112 $lastTag = $tag;
113
114 if($tag == 'equal') {
115 $lines = array_slice($a, $i1, ($i2 - $i1));
116 $blocks[$lastBlock]['base']['lines'] += $this->formatLines($lines);
117 $lines = array_slice($b, $j1, ($j2 - $j1));
118 $blocks[$lastBlock]['changed']['lines'] += $this->formatLines($lines);
119 }
120 else {
121 if($tag == 'replace' || $tag == 'delete') {
122 $lines = array_slice($a, $i1, ($i2 - $i1));
123 $lines = $this->formatLines($lines);
124 $lines = str_replace(array("\0", "\1"), array('<del>', '</del>'), $lines);
125 $blocks[$lastBlock]['base']['lines'] += $lines;
126 }
127
128 if($tag == 'replace' || $tag == 'insert') {
129 $lines = array_slice($b, $j1, ($j2 - $j1));
130 $lines = $this->formatLines($lines);
131 $lines = str_replace(array("\0", "\1"), array('<ins>', '</ins>'), $lines);
132 $blocks[$lastBlock]['changed']['lines'] += $lines;
133 }
134 }
135 }
136 $changes[] = $blocks;
137 }
138 return $changes;
139 }
140
141 /**
142 * Given two strings, determine where the changes in the two strings
143 * begin, and where the changes in the two strings end.
144 *
145 * @param string $fromLine The first string.
146 * @param string $toLine The second string.
147 * @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
148 */
149 private function getChangeExtent($fromLine, $toLine)
150 {
151 $start = 0;
152 $limit = min(strlen($fromLine), strlen($toLine));
153 while($start < $limit && $fromLine{$start} == $toLine{$start}) {
154 ++$start;
155 }
156 $end = -1;
157 $limit = $limit - $start;
158 while(-$end <= $limit && substr($fromLine, $end, 1) == substr($toLine, $end, 1)) {
159 --$end;
160 }
161 return array(
162 $start,
163 $end + 1
164 );
165 }
166
167 /**
168 * Format a series of lines suitable for output in a HTML rendered diff.
169 * This involves replacing tab characters with spaces, making the HTML safe
170 * for output, ensuring that double spaces are replaced with &nbsp; etc.
171 *
172 * @param array $lines Array of lines to format.
173 * @return array Array of the formatted lines.
174 */
175 protected function formatLines($lines)
176 {
177 $lines = array_map(array($this, 'ExpandTabs'), $lines);
178 $lines = array_map(array($this, 'HtmlSafe'), $lines);
179 foreach($lines as &$line) {
180 $line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
181 }
182 return $lines;
183 }
184
185 /**
186 * Replace a string containing spaces with a HTML representation using &nbsp;.
187 *
188 * @param string[] $matches Array with preg matches.
189 * @return string The HTML representation of the string.
190 */
191 private function fixSpaces(array $matches)
192 {
193 $spaces = $matches[1];
194 $count = strlen($spaces);
195 if($count == 0) {
196 return '';
197 }
198
199 $div = floor($count / 2);
200 $mod = $count % 2;
201 return str_repeat('&nbsp; ', $div).str_repeat('&nbsp;', $mod);
202 }
203
204 /**
205 * Replace tabs in a single line with a number of spaces as defined by the tabSize option.
206 *
207 * @param string $line The containing tabs to convert.
208 * @return string The line with the tabs converted to spaces.
209 */
210 private function expandTabs($line)
211 {
212 return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
213 }
214
215 /**
216 * Make a string containing HTML safe for output on a page.
217 *
218 * @param string $string The string.
219 * @return string The string with the HTML characters replaced by entities.
220 */
221 private function htmlSafe($string)
222 {
223 return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
224 }
225 }
226