Skip to content

Commit 7e42a52

Browse files
committed
Cleaned up code in preparation of review.
1 parent 8cac676 commit 7e42a52

File tree

3 files changed

+114
-93
lines changed

3 files changed

+114
-93
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.hasOwnProperty('diff') ? data.diff : data;
41+
$scope.diff = data.diff;
4242
$scope.loading = false;
4343
});
4444
};

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -63,96 +63,14 @@ protected function indexNewWords()
6363
$this->wordIndices[ $word ] = array( $i );
6464
}
6565
}
66-
//$this->dump($this->wordIndices, " ============= WORD INDICES");
6766
}
6867

6968
protected function replaceIsolatedDiffTags()
7069
{
71-
//$this->dump($this->oldWords, "========== SPLIT INTO NEW WORDS === OLD WORDS >>> BEFORE");
72-
//$this->dump($this->newWords, "========== SPLIT INTO NEW WORDS === NEW WORDS >>> BEFORE");
7370
$this->oldIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->oldWords);
7471
$this->newIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->newWords);
75-
//$this->dump($this->oldWords, "========== SPLIT INTO NEW WORDS === OLD WORDS");
76-
//$this->dump($this->newWords, "========== SPLIT INTO NEW WORDS === NEW WORDS");
77-
//$this->dump($this->oldIsolatedDiffTags, " ======================= ReplaceIsolatedDiffTags === old isolated diff tags");
78-
//$this->dump($this->newIsolatedDiffTags, " ======================= ReplaceIsolatedDiffTags === new isolated diff tags");
7972

8073
}
81-
82-
protected function convertHtmlToListOfWords($characterString)
83-
{
84-
$mode = 'character';
85-
$current_word = '';
86-
$words = array();
87-
foreach ($characterString as $i => $character) {
88-
switch ($mode) {
89-
case 'character':
90-
if ( $this->isStartOfTag( $character ) ) {
91-
if ($current_word != '') {
92-
$words[] = $current_word;
93-
}
94-
$current_word = "<";
95-
$mode = 'tag';
96-
} elseif ( preg_match( "[^\s]", $character ) > 0 ) {
97-
if ($current_word != '') {
98-
$words[] = $current_word;
99-
}
100-
$current_word = $character;
101-
$mode = 'whitespace';
102-
} else {
103-
if (
104-
(ctype_alnum($character) && (strlen($current_word) == 0 || $this->isPartOfWord($current_word))) ||
105-
(in_array($character, $this->specialCaseChars) && isset($characterString[$i+1]) && $this->isPartOfWord($characterString[$i+1]))
106-
) {
107-
$current_word .= $character;
108-
} else {
109-
$words[] = $current_word;
110-
$current_word = $character;
111-
}
112-
}
113-
break;
114-
case 'tag' :
115-
if ( $this->isEndOfTag( $character ) ) {
116-
$current_word .= ">";
117-
$words[] = $current_word;
118-
$current_word = "";
119-
120-
if ( !preg_match('[^\s]', $character ) ) {
121-
$mode = 'whitespace';
122-
} else {
123-
$mode = 'character';
124-
}
125-
} else {
126-
$current_word .= $character;
127-
}
128-
break;
129-
case 'whitespace':
130-
if ( $this->isStartOfTag( $character ) ) {
131-
if ($current_word != '') {
132-
$words[] = $current_word;
133-
}
134-
$current_word = "<";
135-
$mode = 'tag';
136-
} elseif ( preg_match( "[^\s]", $character ) ) {
137-
$current_word .= $character;
138-
} else {
139-
if ($current_word != '') {
140-
$words[] = $current_word;
141-
}
142-
$current_word = $character;
143-
$mode = 'character';
144-
}
145-
break;
146-
default:
147-
break;
148-
}
149-
}
150-
if ($current_word != '') {
151-
$words[] = $current_word;
152-
}
153-
154-
return $words;
155-
}
15674

15775
protected function createIsolatedDiffTagPlaceholders(&$words)
15876
{
@@ -281,8 +199,8 @@ protected function diffElements($oldText, $newText)
281199
$wrapEnd = '';
282200

283201
if (preg_match_all($pattern, $newText, $matches)) {
284-
$wrapStart = count($matches[0]) ? $matches[0][0] : '';
285-
$wrapEnd = count($matches[0]) > 1 ? $matches[0][1] : '';
202+
$wrapStart = $matches[0][0];
203+
$wrapEnd = $matches[0][1];
286204
}
287205
$oldText = preg_replace($pattern, '', $oldText);
288206
$newText = preg_replace($pattern, '', $newText);

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,57 @@
44

55
class ListDiff extends HtmlDiff
66
{
7+
/** @var array */
78
protected $listWords = array();
9+
10+
/** @var array */
811
protected $listTags = array();
12+
13+
/** @var array */
914
protected $listIsolatedDiffTags = array();
15+
16+
/** @var array */
1017
protected $isolatedDiffTags = array (
1118
'ol' => '[[REPLACE_ORDERED_LIST]]',
1219
'ul' => '[[REPLACE_UNORDERED_LIST]]',
1320
'dl' => '[[REPLACE_DEFINITION_LIST]]',
1421
);
22+
23+
/**
24+
* List (li) placeholder.
25+
* @var string
26+
*/
1527
protected static $listPlaceHolder = "[[REPLACE_LIST_ITEM]]";
16-
protected $listType; // holds the type of list this is ol, ul, dl
17-
protected $list; // hold the old/new content of the content of the list
18-
protected $childLists; // contains the old/new child lists content within this list
19-
protected $textMatches; // contains the old/new text strings that match
20-
protected $listsIndex; // contains the indexed start positions of each list within word string.
28+
29+
/**
30+
* Holds the type of list this is ol, ul, dl.
31+
* @var string
32+
*/
33+
protected $listType;
34+
35+
/**
36+
* Hold the old/new content of the content of the list.
37+
* @var array
38+
*/
39+
protected $list;
40+
41+
/**
42+
* Contains the old/new child lists content within this list.
43+
* @var array
44+
*/
45+
protected $childLists;
46+
47+
/**
48+
* Contains the old/new text strings that match
49+
* @var array
50+
*/
51+
protected $textMatches;
52+
53+
/**
54+
* Contains the indexed start positions of each list within word string.
55+
* @var array
56+
*/
57+
protected $listsIndex;
2158

2259
/**
2360
* We're using the same functions as the parent in build() to get us to the point of
@@ -87,6 +124,10 @@ protected function formatThisListContent()
87124
}
88125
}
89126

127+
/**
128+
* @param string $tag
129+
* @return string
130+
*/
90131
protected function getAndStripTag($tag)
91132
{
92133
$content = explode(' ', preg_replace("/[^A-Za-z0-9 ]/", '', $tag));
@@ -250,6 +291,12 @@ protected function diff()
250291
$this->content .= $this->addListTypeWrapper(false);
251292
}
252293

294+
/**
295+
* Converts the list (li) content arrays to string.
296+
*
297+
* @param array $listContentArray
298+
* @return string
299+
*/
253300
protected function convertListContentArrayToString($listContentArray)
254301
{
255302
if (!is_array($listContentArray)) {
@@ -278,6 +325,10 @@ protected function convertListContentArrayToString($listContentArray)
278325
/**
279326
* Return the contents of each list node.
280327
* Process any placeholders for nested lists.
328+
*
329+
* @param string $text
330+
* @param array $matches
331+
* @return string
281332
*/
282333
protected function processPlaceholders($text, array $matches)
283334
{
@@ -313,6 +364,12 @@ protected function processPlaceholders($text, array $matches)
313364
return implode(' ', $returnText);
314365
}
315366

367+
/**
368+
* Checks to see if a diff tag is in string.
369+
*
370+
* @param string $word
371+
* @return string
372+
*/
316373
protected function checkWordForDiffTag($word)
317374
{
318375
foreach ($this->isolatedDiffTags as $diffTag) {
@@ -334,6 +391,9 @@ protected function checkWordForDiffTag($word)
334391

335392
/**
336393
* Used to remove new lines.
394+
*
395+
* @param string $text
396+
* @return string
337397
*/
338398
protected function stripNewLine($text)
339399
{
@@ -342,6 +402,10 @@ protected function stripNewLine($text)
342402

343403
/**
344404
* Grab the list content using the listsIndex array.
405+
*
406+
* @param string $indexKey
407+
* @param array $matches
408+
* @return array
345409
*/
346410
protected function getListContent($indexKey = 'new', array $matches)
347411
{
@@ -360,7 +424,14 @@ protected function getListContent($indexKey = 'new', array $matches)
360424
return $bucket;
361425
}
362426

363-
protected function findEndForIndex($index, $start)
427+
/**
428+
* Finds the end of list within its index.
429+
*
430+
* @param array $index
431+
* @param integer $start
432+
* @return integer
433+
*/
434+
protected function findEndForIndex(array $index, $start)
364435
{
365436
$array = array_splice($index, $start);
366437
$count = 0;
@@ -410,6 +481,9 @@ protected function indexLists()
410481

411482
/**
412483
* Adds the opening or closing list html element, based on listType.
484+
*
485+
* @param boolean $opening
486+
* @return string
413487
*/
414488
protected function addListTypeWrapper($opening = true)
415489
{
@@ -427,6 +501,10 @@ public function replaceListIsolatedDiffTags()
427501

428502
/**
429503
* Grab the contents of a list node.
504+
*
505+
* @param array $contentArray
506+
* @param boolean $stripTags
507+
* @return array
430508
*/
431509
protected function getListsContent(array $contentArray, $stripTags = true)
432510
{
@@ -458,7 +536,19 @@ protected function getListsContent(array $contentArray, $stripTags = true)
458536
return $lematches;
459537
}
460538

461-
protected function addStringToArrayByDepth($word, &$array, $targetDepth, $thisDepth, $nestedCount)
539+
/**
540+
* This function helps build the list content array of a list.
541+
* If a list has another list within it, the inner list is replaced with the list placeholder and the inner list
542+
* content becomes a child of the parent list.
543+
* This goes recursively down.
544+
*
545+
* @param string $word
546+
* @param array $array
547+
* @param integer $targetDepth
548+
* @param integer $thisDepth
549+
* @param array $nestedCount
550+
*/
551+
protected function addStringToArrayByDepth($word, array &$array, $targetDepth, $thisDepth, array $nestedCount)
462552
{
463553
// determine what depth we're at
464554
if ($targetDepth == $thisDepth) {
@@ -500,6 +590,7 @@ protected function addStringToArrayByDepth($word, &$array, $targetDepth, $thisDe
500590
);
501591

502592
} else {
593+
503594
if ($nestedCount[$targetDepth] > count($array[count($array) - 1]['kids'])) {
504595
$array[count($array) - 1]['kids'][] = $newArray;
505596
$array[count($array) - 1]['content'] .= self::$listPlaceHolder;
@@ -520,6 +611,12 @@ protected function addStringToArrayByDepth($word, &$array, $targetDepth, $thisDe
520611
}
521612
}
522613

614+
/**
615+
* Checks if text is opening list tag.
616+
*
617+
* @param string $item
618+
* @return boolean
619+
*/
523620
protected function isOpeningListTag($item)
524621
{
525622
if (preg_match("#<li[^>]*>\\s*#iU", $item)) {
@@ -528,7 +625,13 @@ protected function isOpeningListTag($item)
528625

529626
return false;
530627
}
531-
628+
629+
/**
630+
* Check if text is closing list tag.
631+
*
632+
* @param string $item
633+
* @return boolean
634+
*/
532635
protected function isClosingListTag($item)
533636
{
534637
if (preg_match("#</li[^>]*>\\s*#iU", $item)) {

0 commit comments

Comments
 (0)