Skip to content

Commit 0ba194b

Browse files
committed
Save point for ListDiffing.
Next issue to be resolved: <li> content is not separated... all on one line. Fixes were in for this, but I screwed it up and had to revert backwards like 4 hours.
1 parent d063f5d commit 0ba194b

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

demo/demo_text.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,19 @@
22

33
$demos = array(
44
array(
5-
'old' => "<ol>
6-
<li>During the 40 kW exposure, flames shall not spread to the ceiling.</li>
5+
'old' => "<ol>
6+
<li>During the 40 kW exposure, flames <li>yeaaaa she <li>MICKEY <li>HAPPY MEAL</li> MOUSE</li> did</li> <li>noooooo</li> shall not spread to the ceiling.</li>
77
<li>The flame shall not spread to the outer extremities of the samples on the 8-foot by 12-foot (203 by 305 mm) walls.</li>
88
<li>Flashover, as defined in NFPA 265, shall not occur.</li>
9-
<li>The total smoke released throughout the test shall not exceed 1,000 m<sup>2</sup>. Stuff:
10-
<ol>
9+
<li>The total smoke released throughout the test shall not exceed 1,000 m<sup>2</sup>. Stuff: <ol>
1110
<li>All the stuff I didnt</li>
1211
<li>Ok, I can</li>
1312
<li>Oh no she didnt.</li>
1413
</ol>
1514
</li>
1615
</ol>",
1716

18-
'new' => "<ol>
19-
<li>During the 40 kW exposure, flames shall not spread to the ceiling.</li>
20-
<li>The flame shall not spread to the outer extremities of the samples on the 8-foot by 12-foot (203 by 305 mm) walls.</li>
21-
<li>Flashover, as defined in NFPA 265, shall not occur.</li>
22-
<li>The total smoke <li>yep she did</li> released throughout the test shall not exceed 1,000 m<sup>2</sup>. Stuff:
23-
<ol>
24-
<li>All the stuff I didnt</li>
25-
<li>Ok, I can</li>
26-
<li>Oh no she didnt.</li>
27-
</ol>
28-
</li>
29-
</ol>"
17+
'new' => "<ol> <li>During the 40 kW exposure, flames shall not spread to the ceiling.</li> <li>The flame shall not spread to the outer extremities of the samples on the 8-foot by 12-foot (203 by 305 mm) walls.</li> <li>Flashover, as defined in NFPA 265, shall not occur.</li> <li>The total smoke <li>yep she did</li> released throughout the test shall not exceed 1,000 m<sup>2</sup>. Stuff: <ol> <li>All the stuff I didnt</li> <li>Ok, I can</li> <li>Oh no she didnt.</li> </ol> </li> </ol>"
3018
)
3119
);
3220
header('Content-Type: application/json');

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function matchAndCompareLists()
9999
* Index the list, starting positions, so that we can refer back to it later.
100100
* This is used to see where one list node starts and another ends.
101101
*/
102-
$this->indexLists();
102+
//$this->indexLists();
103103

104104
/**
105105
* Compare the lists and build $textMatches array with the matches.
@@ -114,6 +114,8 @@ protected function compareChildLists()
114114
// Always compare the new against the old.
115115
// Compare each new string against each old string.
116116
$bestMatchPercentages = array();
117+
$this->dump($this->childLists['new'], '=========comparechildlists');
118+
$this->dump($this->childLists['old'], '=========comparechildlists');
117119
foreach ($this->childLists['new'] as $thisKey => $thisList) {
118120
$bestMatchPercentages[$thisKey] = array();
119121
foreach ($this->childLists['old'] as $thatKey => $thatList) {
@@ -201,6 +203,8 @@ function ($v) use ($percent) {
201203

202204
// Save the matches.
203205
$this->textMatches = $matches;
206+
$this->dump($this->textMatches);
207+
die;
204208
}
205209

206210
/**
@@ -210,7 +214,7 @@ protected function buildChildLists()
210214
{
211215
$this->childLists['old'] = $this->getListsContent($this->list['old']);
212216
$this->childLists['new'] = $this->getListsContent($this->list['new']);
213-
die;
217+
$this->dump($this->childLists);
214218
}
215219

216220
/**
@@ -364,47 +368,81 @@ public function replaceListIsolatedDiffTags()
364368
*/
365369
protected function getListsContent(array $contentArray, $stripTags = true)
366370
{
367-
$lematches = array();
371+
$lematches = array('content' => '', 'kids' => array());
368372
$arrayDepth = 0;
369-
$previousDepth = 0;
370-
$count = 0;
371373
$status = "//////////////////// STATUS \\\\\\\\\\\\\\\\\\\\\\";
374+
$nestedCount = array();
372375
foreach ($contentArray as $index => $word) {
373-
$previousDepth = $arrayDepth;
376+
374377
if ($this->isOpeningListTag($word)) {
375378
$arrayDepth++;
376-
$changed = true;
377-
$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '++', 'word' => $word, 'changed' => $changed), $status);
379+
if (!array_key_exists($arrayDepth, $nestedCount)) {
380+
$nestedCount[$arrayDepth] = 1;
381+
} else {
382+
$nestedCount[$arrayDepth]++;
383+
}
384+
//$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '++', 'word' => $word, 'changed' => $changed), $status);
378385
continue;
379386
}
380387

381388
if ($this->isClosingListTag($word)) {
382389
$arrayDepth--;
383-
$changed = true;
384-
$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '--', 'word' => $word, 'changed' => $changed), $status);
390+
//$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '--', 'word' => $word, 'changed' => $changed), $status);
385391
continue;
386392
}
387393

388394
if ($arrayDepth > 0) {
389-
$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '==', 'word' => $word, 'changed' => $changed), $status);
390-
$this->addStringToArrayByDepth($word, $lematches, $arrayDepth, $changed);
391-
$this->dump($lematches);
395+
//$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '==', 'word' => $word, 'changed' => $changed), $status);
396+
$this->addStringToArrayByDepth($word, $lematches, $arrayDepth, 1, $nestedCount);
397+
//$this->dump($lematches, '---------- total array at end of this loop ---------');
392398
}
393-
$count++;
394-
$changed = false;
395399
}
396400

397-
var_dump($lematches);
398-
401+
$this->dump($lematches);
402+
die;
399403
//var_dump($contentArray);
400404
//var_dump(implode('', $contentArray));
401405
preg_match_all('/<li>(.*?)<\/li>/s', implode('', $contentArray), $matches);
402-
//var_dump($matches[intval($stripTags)]);
403-
return $matches[intval($stripTags)];
406+
$this->dump($matches[intval($stripTags)], 'XXXXXXXXXXXXXXXXx - matches dump');
407+
return $lematches;
404408
}
405409

406-
protected function addStringToArrayByDepth($word, &$array, $depth = 1, $changed = false, $addedContent = false)
410+
protected function addStringToArrayByDepth($word, &$array, $targetDepth, $thisDepth, $nestedCount)
407411
{
412+
//$this->dump(func_get_args(), '============ addstringfunction vars');
413+
//$this->dump($array);
414+
415+
// determine what depth we're at
416+
if ($targetDepth == $thisDepth) {
417+
// decide on what to do at this level
418+
419+
// if we're on depth 1, add content
420+
$array['content'] .= $word;
421+
422+
//$this->dump($array, '========= ADDED CONTENT TO THIS ARRAY ==========');
423+
} else {
424+
425+
// create first kid if not exist
426+
/*if (count($array['kids']) < 1) {
427+
$this->dump('', "!!! count of kid less than one");
428+
$newArray = array('content' => '', 'kids' => array());
429+
$array['kids'][] = $newArray;
430+
}*/
431+
432+
//$this->dump(($depth > $previousDepth), "!!! depth greater than prev");
433+
//$this->dump(($previousDepth > $depth), "!!! depth less than prev");
434+
if ($nestedCount[$targetDepth] > count($array['kids'])) {
435+
$newArray = array('content' => '', 'kids' => array());
436+
$array['kids'][] = $newArray;
437+
$array['content'] .= "[[REPLACE_LIST_ITEM]]";
438+
}
439+
// continue to the next depth
440+
$thisDepth++;
441+
442+
// get last kid and send to next depth
443+
444+
$this->addStringToArrayByDepth($word, $array['kids'][count($array['kids']) - 1], $targetDepth, $thisDepth, $nestedCount);
445+
}
408446
/* Structure
409447
* $matches = array(
410448
* 0 = array(
@@ -416,11 +454,13 @@ protected function addStringToArrayByDepth($word, &$array, $depth = 1, $changed
416454
* )
417455
* )
418456
*
419-
*/
457+
420458
//$this->dump(func_get_args(), "======func args========");
421459
422460
if ($depth === 1) {
423-
if ($changed) {
461+
if ($changed && $previousDepth > $depth) {
462+
463+
} else {
424464
$array[] = array('content' => '', 'kids' => array());
425465
}
426466
$array[count($array) - 1]['content'] .= $word;
@@ -429,11 +469,12 @@ protected function addStringToArrayByDepth($word, &$array, $depth = 1, $changed
429469
$depth--;
430470
$this->dump($array, "---------------DOWN");
431471
$this->addStringToArrayByDepth($word, $array, $depth, $changed, true);
432-
}
472+
}*/
433473
}
434474

435475
protected function dump($content, $text = null)
436476
{
477+
ini_set('xdebug.var_display_max_depth', '10');
437478
if ($text) {
438479
var_dump($text);
439480
}

0 commit comments

Comments
 (0)