Skip to content

Commit c7d60bd

Browse files
committed
Merge pull request #20 from caxy/feature-list_diffing-new
feature-list_diffing-new
2 parents 0a3b65f + cf2e447 commit c7d60bd

File tree

1 file changed

+92
-30
lines changed

1 file changed

+92
-30
lines changed

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 92 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ListDiff extends HtmlDiff
88
* This is the minimum percentage a list item can match its counterpart in order to be considered a match.
99
* @var integer
1010
*/
11-
protected static $listMatchThreshold = 50;
11+
protected static $listMatchThreshold = 35;
1212

1313
/** @var array */
1414
protected $listWords = array();
@@ -433,56 +433,118 @@ protected function buildChildLists()
433433
* Build the content of the class.
434434
*/
435435
protected function diff()
436-
{
436+
{
437437
// Add the opening parent node from listType. So if ol, <ol>, etc.
438438
$this->content = $this->addListTypeWrapper();
439439

440440
$oldIndexCount = 0;
441+
$diffOrderNewKeys = array_keys($this->diffOrderIndex['new']);
441442
foreach ($this->diffOrderIndex['new'] as $key => $index) {
442443

443444
if ($index['type'] == "list") {
445+
446+
// Check to see if an old list was deleted.
447+
$oldMatch = $this->getArrayByColumnValue($this->textMatches, 'old', $index['position']);
448+
if ($oldMatch && $oldMatch['new'] === null) {
449+
$newList = '';
450+
$oldList = $this->getListByMatch($oldMatch, 'old');
451+
$this->content .= $this->addListElementToContent($newList, $oldList, $oldMatch);
452+
}
453+
444454
$match = $this->getArrayByColumnValue($this->textMatches, 'new', $index['position']);
445455
$newList = $this->childLists['new'][$match['new']];
446-
$oldList = array_key_exists($match['old'], $this->childLists['old'])
447-
? $this->childLists['old'][$match['old']]
448-
: '';
449-
450-
$content = "<li>";
451-
$content .= $this->processPlaceholders(
452-
$this->diffElements(
453-
$this->convertListContentArrayToString($oldList),
454-
$this->convertListContentArrayToString($newList),
455-
false
456-
),
457-
$match
458-
);
459-
$content .= "</li>";
460-
$this->content .= $content;
456+
$oldList = $this->getListByMatch($match, 'old');
457+
$this->content .= $this->addListElementToContent($newList, $oldList, $match);
461458
}
462459

463460
if ($index['type'] == 'content') {
464-
$newContent = $this->contentIndex['new'][$index['position']];
465-
466-
$oldDiffOrderIndexMatch = array_key_exists($oldIndexCount, $this->diffOrderIndex['old'])
467-
? $this->diffOrderIndex['old'][$oldIndexCount]
468-
: '';
469-
470-
$oldContent = $oldDiffOrderIndexMatch && array_key_exists($oldDiffOrderIndexMatch['position'], $this->contentIndex['old'])
471-
? $this->contentIndex['old'][$oldDiffOrderIndexMatch['position']]
472-
: '';
473-
474-
$diffObject = new HtmlDiff($oldContent, $newContent);
475-
$content = $diffObject->build();
476-
$this->content .= $content;
461+
$this->content .= $this->addContentElementsToContent($oldIndexCount, $index['position']);
477462
}
478463

479464
$oldIndexCount++;
465+
466+
if ($key == $diffOrderNewKeys[count($diffOrderNewKeys) - 1]) {
467+
foreach ($this->diffOrderIndex['old'] as $oldKey => $oldIndex) {
468+
if ($oldKey > $key) {
469+
if ($oldIndex['type'] == 'list') {
470+
$oldMatch = $this->getArrayByColumnValue($this->textMatches, 'old', $oldIndex['position']);
471+
if ($oldMatch && $oldMatch['new'] === null) {
472+
$newList = '';
473+
$oldList = $this->getListByMatch($oldMatch, 'old');
474+
$this->content .= $this->addListElementToContent($newList, $oldList, $oldMatch);
475+
}
476+
} else {
477+
$this->content .= $this->addContentElementsToContent($oldKey);
478+
}
479+
}
480+
}
481+
}
480482
}
481483

482484
// Add the closing parent node from listType. So if ol, </ol>, etc.
483485
$this->content .= $this->addListTypeWrapper(false);
484486
}
485487

488+
/**
489+
*
490+
* @param string $newList
491+
* @param string $oldList
492+
* @param array $match
493+
* @return string
494+
*/
495+
protected function addListElementToContent($newList, $oldList, array $match)
496+
{
497+
$content = "<li>";
498+
$content .= $this->processPlaceholders(
499+
$this->diffElements(
500+
$this->convertListContentArrayToString($oldList),
501+
$this->convertListContentArrayToString($newList),
502+
false
503+
),
504+
$match
505+
);
506+
$content .= "</li>";
507+
return $content;
508+
}
509+
510+
/**
511+
*
512+
* @param integer $oldIndexCount
513+
* @param null|integer $newPosition
514+
* @return string
515+
*/
516+
protected function addContentElementsToContent($oldIndexCount, $newPosition = null)
517+
{
518+
$newContent = $newPosition && array_key_exists($newPosition, $this->contentIndex['new'])
519+
? $this->contentIndex['new'][$newPosition]
520+
: '';
521+
522+
$oldDiffOrderIndexMatch = array_key_exists($oldIndexCount, $this->diffOrderIndex['old'])
523+
? $this->diffOrderIndex['old'][$oldIndexCount]
524+
: '';
525+
526+
$oldContent = $oldDiffOrderIndexMatch && array_key_exists($oldDiffOrderIndexMatch['position'], $this->contentIndex['old'])
527+
? $this->contentIndex['old'][$oldDiffOrderIndexMatch['position']]
528+
: '';
529+
530+
$diffObject = new HtmlDiff($oldContent, $newContent);
531+
$content = $diffObject->build();
532+
return $content;
533+
}
534+
535+
/**
536+
*
537+
* @param array $match
538+
* @param string $type
539+
* @return array|string
540+
*/
541+
protected function getListByMatch(array $match, $type = 'new')
542+
{
543+
return array_key_exists($match[$type], $this->childLists[$type])
544+
? $this->childLists[$type][$match[$type]]
545+
: '';
546+
}
547+
486548
/**
487549
* This function replaces array_column function in PHP for older versions of php.
488550
*

0 commit comments

Comments
 (0)