77class HtmlDiff extends AbstractDiff
88{
99 protected $ wordIndices ;
10- protected $ oldTables ;
11- protected $ newTables ;
1210 protected $ insertSpaceInReplace = false ;
1311 protected $ newSpecialScript ;
1412 protected $ oldSpecialScript ;
15- protected $ specialElements = array ('ol ' => '[[REPLACE_ORDERED_LIST]] ' , 'ul ' => '[[REPLACE_UNORDERED_LIST]] ' , 'sub ' => '[[REPLACE_SUB_SCRIPT]] ' , 'sup ' => '[[REPLACE_SUPER_SCRIPT]] ' , 'dl ' => '[[REPLACE_DEFINITION_LIST]] ' );
13+ protected $ specialElements = array ('ol ' => '[[REPLACE_ORDERED_LIST]] ' , 'ul ' => '[[REPLACE_UNORDERED_LIST]] ' , 'sub ' => '[[REPLACE_SUB_SCRIPT]] ' , 'sup ' => '[[REPLACE_SUPER_SCRIPT]] ' , 'dl ' => '[[REPLACE_DEFINITION_LIST]] ' , ' table ' => ' [[REPLACE_TABLE]] ' );
1614
1715 /**
1816 * @param boolean $boolean
@@ -36,7 +34,6 @@ public function getInsertSpaceInReplace()
3634 public function build ()
3735 {
3836 $ this ->splitInputsToWords ();
39- $ this ->replaceTables ();
4037 $ this ->replaceSpecialScripts ();
4138 $ this ->indexNewWords ();
4239
@@ -63,13 +60,7 @@ protected function indexNewWords()
6360 }
6461 }
6562
66- protected function replaceTables ()
67- {
68- $ this ->oldTables = $ this ->createTablePlaceholders ($ this ->oldWords );
69- $ this ->newTables = $ this ->createTablePlaceholders ($ this ->newWords );
70- }
71-
72- protected function replaceSuperScripts ()
63+ private function replaceSpecialScripts ()
7364 {
7465 $ this ->oldSpecialScript = $ this ->createSpecialPlaceholders ($ this ->oldWords );
7566 $ this ->newSpecialScript = $ this ->createSpecialPlaceholders ($ this ->newWords );
@@ -133,46 +124,6 @@ private function isClosingSpecialScript($item, $currentSpecialTag = null)
133124 return false ;
134125 }
135126
136- private function createTablePlaceholders (&$ words )
137- {
138- $ openTables = 0 ;
139- $ tableIndices = array ();
140- $ tableStart = 0 ;
141- foreach ($ words as $ index => $ word ) {
142- if ($ this ->isOpeningTable ($ word )) {
143- if ($ openTables === 0 ) {
144- $ tableStart = $ index ;
145- }
146- $ openTables ++;
147- } elseif ($ openTables > 0 && $ this ->isClosingTable ($ word )) {
148- $ openTables --;
149- if ($ openTables === 0 ) {
150- $ tableIndices [] = array ('start ' => $ tableStart , 'length ' => $ index - $ tableStart + 1 );
151- }
152- }
153- }
154-
155- $ tables = array ();
156- $ offset = 0 ;
157- foreach ($ tableIndices as $ tableIndex ) {
158- $ start = $ tableIndex ['start ' ] - $ offset ;
159- $ tables [$ start ] = array_splice ($ words , $ start , $ tableIndex ['length ' ], '[[REPLACE_TABLE]] ' );
160- $ offset += $ tableIndex ['length ' ] - 1 ;
161- }
162-
163- return $ tables ;
164- }
165-
166- protected function isOpeningTable ($ item )
167- {
168- return preg_match ("#<table[^>]*> \\s*#iU " , $ item );
169- }
170-
171- protected function isClosingTable ($ item )
172- {
173- return preg_match ("#</table[^>]*> \\s*#iU " , $ item );
174- }
175-
176127 protected function performOperation ($ operation )
177128 {
178129 switch ($ operation ->action ) {
@@ -205,7 +156,6 @@ protected function processInsertOperation($operation, $cssClass)
205156 foreach ($ this ->newWords as $ pos => $ s ) {
206157 $ matchFound = false ;
207158 if ($ pos >= $ operation ->startInNew && $ pos < $ operation ->endInNew ) {
208-
209159 foreach ($ this ->specialElements as $ specialElement ) {
210160 if ($ s === $ specialElement && isset ($ this ->newSpecialScript [$ pos ]) && $ matchFound === false ) {
211161 foreach ($ this ->newSpecialScript [$ pos ] as $ word ) {
@@ -214,12 +164,7 @@ protected function processInsertOperation($operation, $cssClass)
214164 $ matchFound = true ;
215165 }
216166 }
217- if ($ s === '[[REPLACE_TABLE]] ' && isset ($ this ->newTables [$ pos ]) && $ matchFound === false ) {
218- foreach ($ this ->newTables [$ pos ] as $ word ) {
219- $ text [] = $ word ;
220- }
221- $ matchFound = true ;
222- } else if ($ matchFound === false ){
167+ if ($ matchFound === false ){
223168 $ text [] = $ s ;
224169 }
225170 }
@@ -231,47 +176,70 @@ protected function processDeleteOperation($operation, $cssClass)
231176 {
232177 $ text = array ();
233178 foreach ($ this ->oldWords as $ pos => $ s ) {
179+ $ matchFound = false ;
234180 if ($ pos >= $ operation ->startInOld && $ pos < $ operation ->endInOld ) {
235- if ($ s === '[[REPLACE_TABLE]] ' && isset ($ this ->oldTables [$ pos ])) {
236- foreach ($ this ->oldTables [$ pos ] as $ word ) {
181+ foreach ($ this ->specialElements as $ specialElement )
182+ if ($ s === $ specialElement && isset ($ this ->oldSpecialScript [$ pos ]) && $ matchFound === false ) {
183+ foreach ($ this ->oldSpecialScript [$ pos ] as $ word ) {
237184 $ text [] = $ word ;
238185 }
239- } else {
186+ $ matchFound = true ;
187+ }
188+ if ($ matchFound === false ){
240189 $ text [] = $ s ;
241190 }
242191 }
243192 }
244193 $ this ->insertTag ( "del " , $ cssClass , $ text );
245194 }
246195
247- protected function diffTables ($ oldText , $ newText )
196+ private function diffElements ($ oldText , $ newText )
248197 {
249- $ diff = new TableDiff ($ oldText , $ newText , $ this ->encoding , $ this ->specialCaseTags , $ this ->groupDiffs );
250- return $ diff ->build ();
198+ $ pattern = '/(^<[^>]+>)|(<\/[^>]+>$)/i ' ;
199+ $ matches = array ();
200+ $ wrapStart = '' ;
201+ $ wrapEnd = '' ;
202+ if (preg_match_all ($ pattern , $ newText , $ matches )) {
203+ $ wrapStart = $ matches [0 ][0 ];
204+ $ wrapEnd = $ matches [0 ][1 ];
205+ }
206+ $ oldText = preg_replace ($ pattern , '' , $ oldText );
207+ $ newText = preg_replace ($ pattern , '' , $ newText );
208+
209+ $ diff = new HtmlDiff ($ oldText , $ newText , $ this ->encoding , $ this ->specialCaseTags , $ this ->groupDiffs );
210+ return $ wrapStart . $ diff ->build () . $ wrapEnd ;
211+ //$diff = new ElementDiff($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
212+ //return $diff->build();
213+ return 'todo ' ; // This to be build yet.
251214 }
252215
253216 protected function processEqualOperation ($ operation )
254217 {
255218 $ result = array ();
256219 foreach ($ this ->newWords as $ pos => $ s ) {
220+ $ matchFound = false ;
257221 if ($ pos >= $ operation ->startInNew && $ pos < $ operation ->endInNew ) {
258- if ($ s === '[[REPLACE_TABLE]] ' && isset ($ this ->newTables [$ pos ])) {
259- $ oldText = implode ("" , $ this ->findMatchingTableInOld ($ operation , $ pos ));
260- $ newText = implode ("" , $ this ->newTables [$ pos ]);
261- $ result [] = $ this ->diffTables ($ oldText , $ newText );
262- } else {
222+ foreach ($ this ->specialElements as $ specialElement ) {
223+ if ($ s === $ specialElement && isset ($ this ->newSpecialScript [$ pos ]) && $ matchFound === false ) {
224+ $ oldText = implode ("" , $ this ->findMatchingScriptsInOld ($ operation , $ pos ));
225+ $ newText = implode ("" , $ this ->newSpecialScript [$ pos ]);
226+ $ result [] = $ this ->diffElements ($ oldText , $ newText );
227+ $ matchFound = true ;
228+ }
229+ }
230+ if ($ matchFound === false ){
263231 $ result [] = $ s ;
264232 }
265233 }
266234 }
267235 $ this ->content .= implode ( "" , $ result );
268236 }
269237
270- protected function findMatchingTableInOld ($ operation , $ posInNew )
238+ private function findMatchingScriptsInOld ($ operation , $ posInNew )
271239 {
272240 $ offset = $ posInNew - $ operation ->startInNew ;
273241
274- return $ this ->oldTables [$ operation ->startInOld + $ offset ];
242+ return $ this ->oldSpecialScript [$ operation ->startInOld + $ offset ];
275243 }
276244
277245 protected function insertTag ($ tag , $ cssClass , &$ words )
0 commit comments