Skip to content

Commit d063f5d

Browse files
committed
Save point on list content diffing update.
1 parent d5470c5 commit d063f5d

File tree

4 files changed

+129
-23
lines changed

4 files changed

+129
-23
lines changed

demo/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ demo.controller('diffCtrl', ['$scope', '$http', '$sce', '$timeout', function ($s
3838
$scope.loading = true;
3939
$http.post('index.php', { oldText: $scope.oldText, newText: $scope.newText })
4040
.success(function (data) {
41-
$scope.diff = data.diff;
41+
$scope.diff = data.hasOwnProperty('diff') ? data.diff : data;
4242
$scope.loading = false;
4343
});
4444
};

demo/demo_text.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22

33
$demos = array(
44
array(
5-
'old' => "<p><i>This is</i> some sample text to <strong>demonstrate</strong> the capability of the <strong>HTML diff tool</strong>.</p>
6-
<p>It is based on the <b>Ruby</b> implementation found <a href='http://github.com/myobie/htmldiff'>here</a>. Note how the link has no tooltip</p>
7-
<table cellpadding='0' cellspacing='0'>
8-
<tr><td>Some sample text</td><td>Some sample value</td></tr>
9-
<tr><td>Data 1 (this row will be removed)</td><td>Data 2</td></tr>
10-
</table>
11-
Here is a number 2 32<br />
12-
Section 602.1 NAME OF SECTION<br />
13-
Large number 5,000",
5+
'old' => "<ol>
6+
<li>During the 40 kW exposure, flames shall not spread to the ceiling.</li>
7+
<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>
8+
<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>
11+
<li>All the stuff I didnt</li>
12+
<li>Ok, I can</li>
13+
<li>Oh no she didnt.</li>
14+
</ol>
15+
</li>
16+
</ol>",
1417

15-
'new' => "<p>This is some sample <strong>text to</strong> demonstrate the awesome capabilities of the <strong>HTML <u>diff</u> tool</strong>.</p><br/><br/>Extra spacing here that was not here before.
16-
<p>It is <i>based</i> on the Ruby implementation found <a title='Cool tooltip' href='http://github.com/myobie/htmldiff'>here</a>. Note how the link has a tooltip now and the HTML diff algorithm has preserved formatting.</p>
17-
<table cellpadding='0' cellspacing='0'>
18-
<tr><td>Some sample <strong>bold text</strong></td><td>Some sample value</td></tr>
19-
</table>
20-
Here is a number 2 <sup>32</sup><br />
21-
Section 602.2 NAME OF SECTION.<br />
22-
Large numbers 5,001 and 10,000,154"
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>"
2330
)
2431
);
2532
header('Content-Type: application/json');

demo/index.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Caxy\HtmlDiff\HtmlDiff;
44

55
ini_set('display_errors', 1);
6-
error_reporting(E_ERROR);
6+
error_reporting(E_ALL);
77

88
$classes = array(
99
'Caxy/HtmlDiff/AbstractDiff',
@@ -21,11 +21,15 @@
2121

2222
if ($input) {
2323
$data = json_decode($input, true);
24-
$diff = new HtmlDiff($data['oldText'], $data['newText'], 'UTF-8', array());
25-
$diff->build();
24+
$diff = new HtmlDiff($_POST['oldText'], $_POST['newText'], 'UTF-8', array());
25+
try{
26+
echo $diff->build();
27+
} catch (Exception $e) {
28+
echo $e->getMessage();
29+
}
2630

27-
header('Content-Type: application/json');
28-
echo json_encode(array('diff' => $diff->getDifference()));
31+
//header('Content-Type: application/json');
32+
//echo json_encode(array('diff' => $diff->getDifference()));
2933
} else {
3034
header('Content-Type: text/html');
3135
echo file_get_contents('demo.html');

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected function diffListContent()
6767
*/
6868
protected function formatThisListContent()
6969
{
70+
//var_dump($this->oldIsolatedDiffTags);
7071
foreach ($this->oldIsolatedDiffTags as $key => $diffTagArray) {
7172
$openingTag = preg_replace("/[^A-Za-z0-9 ]/", '', $diffTagArray[0]);
7273
$closingTag = preg_replace("/[^A-Za-z0-9 ]/", '', $diffTagArray[count($diffTagArray) - 1]);
@@ -83,6 +84,7 @@ protected function formatThisListContent()
8384
$this->list['new'] = $this->newIsolatedDiffTags[$key];
8485
}
8586
}
87+
//var_dump($this->list);
8688
}
8789

8890
protected function matchAndCompareLists()
@@ -208,6 +210,7 @@ protected function buildChildLists()
208210
{
209211
$this->childLists['old'] = $this->getListsContent($this->list['old']);
210212
$this->childLists['new'] = $this->getListsContent($this->list['new']);
213+
die;
211214
}
212215

213216
/**
@@ -351,6 +354,7 @@ protected function addListTypeWrapper($opening = true)
351354
*/
352355
public function replaceListIsolatedDiffTags()
353356
{
357+
//var_dump($this->list);die;
354358
$this->listIsolatedDiffTags['old'] = $this->createIsolatedDiffTagPlaceholders($this->list['old']);
355359
$this->listIsolatedDiffTags['new'] = $this->createIsolatedDiffTagPlaceholders($this->list['new']);
356360
}
@@ -360,7 +364,98 @@ public function replaceListIsolatedDiffTags()
360364
*/
361365
protected function getListsContent(array $contentArray, $stripTags = true)
362366
{
367+
$lematches = array();
368+
$arrayDepth = 0;
369+
$previousDepth = 0;
370+
$count = 0;
371+
$status = "//////////////////// STATUS \\\\\\\\\\\\\\\\\\\\\\";
372+
foreach ($contentArray as $index => $word) {
373+
$previousDepth = $arrayDepth;
374+
if ($this->isOpeningListTag($word)) {
375+
$arrayDepth++;
376+
$changed = true;
377+
$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '++', 'word' => $word, 'changed' => $changed), $status);
378+
continue;
379+
}
380+
381+
if ($this->isClosingListTag($word)) {
382+
$arrayDepth--;
383+
$changed = true;
384+
$this->dump(array('arrayDepth' => $arrayDepth, 'prev' => $previousDepth, 'action' => '--', 'word' => $word, 'changed' => $changed), $status);
385+
continue;
386+
}
387+
388+
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);
392+
}
393+
$count++;
394+
$changed = false;
395+
}
396+
397+
var_dump($lematches);
398+
399+
//var_dump($contentArray);
400+
//var_dump(implode('', $contentArray));
363401
preg_match_all('/<li>(.*?)<\/li>/s', implode('', $contentArray), $matches);
402+
//var_dump($matches[intval($stripTags)]);
364403
return $matches[intval($stripTags)];
365404
}
405+
406+
protected function addStringToArrayByDepth($word, &$array, $depth = 1, $changed = false, $addedContent = false)
407+
{
408+
/* Structure
409+
* $matches = array(
410+
* 0 = array(
411+
* content => string,
412+
* kids => array(
413+
* content => string,
414+
* kids => array(...)
415+
* )
416+
* )
417+
* )
418+
*
419+
*/
420+
//$this->dump(func_get_args(), "======func args========");
421+
422+
if ($depth === 1) {
423+
if ($changed) {
424+
$array[] = array('content' => '', 'kids' => array());
425+
}
426+
$array[count($array) - 1]['content'] .= $word;
427+
428+
} else {
429+
$depth--;
430+
$this->dump($array, "---------------DOWN");
431+
$this->addStringToArrayByDepth($word, $array, $depth, $changed, true);
432+
}
433+
}
434+
435+
protected function dump($content, $text = null)
436+
{
437+
if ($text) {
438+
var_dump($text);
439+
}
440+
441+
var_dump($content);
442+
}
443+
444+
protected function isOpeningListTag($item)
445+
{
446+
if (preg_match("#<li[^>]*>\\s*#iU", $item)) {
447+
return true;
448+
}
449+
450+
return false;
451+
}
452+
453+
protected function isClosingListTag($item)
454+
{
455+
if (preg_match("#</li[^>]*>\\s*#iU", $item)) {
456+
return true;
457+
}
458+
459+
return false;
460+
}
366461
}

0 commit comments

Comments
 (0)