Skip to content

Commit d22deb9

Browse files
committed
Merge pull request #2 from caxy/feature-group_diff_output
Added option to group together diffed words in output
2 parents c5e3069 + 8b0d362 commit d22deb9

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ class HtmlDiff
1414
private $specialCaseOpeningTags = array();
1515
private $specialCaseClosingTags = array();
1616
private $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
17+
private $groupDiffs = true;
1718

18-
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = array())
19+
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = array(), $groupDiffs = true)
1920
{
2021
$this->oldText = $this->purifyHtml(trim($oldText));
2122
$this->newText = $this->purifyHtml(trim($newText));
2223
$this->encoding = $encoding;
2324
$this->content = '';
25+
$this->groupDiffs = $groupDiffs;
2426

2527
$this->setSpecialCaseTags($specialCaseTags);
2628
}
@@ -89,6 +91,16 @@ public function getDifference()
8991
{
9092
return $this->content;
9193
}
94+
95+
public function setGroupDiffs($boolean)
96+
{
97+
$this->groupDiffs = $boolean;
98+
}
99+
100+
public function isGroupDiffs()
101+
{
102+
return $this->groupDiffs;
103+
}
92104

93105
private function getOpeningTag($tag)
94106
{
@@ -525,7 +537,17 @@ private function findMatch($startInOld, $endInOld, $startInNew, $endInNew)
525537
}
526538
$matchLengthAt = $newMatchLengthAt;
527539
}
528-
529-
return $bestMatchSize != 0 ? new Match( $bestMatchInOld, $bestMatchInNew, $bestMatchSize ) : null;
540+
541+
// Skip match if none found or match consists only of whitespace
542+
if ($bestMatchSize != 0 &&
543+
(
544+
!$this->isGroupDiffs() ||
545+
!preg_match('/^\s+$/', implode('', array_slice($this->oldWords, $bestMatchInOld, $bestMatchSize)))
546+
)
547+
) {
548+
return new Match($bestMatchInOld, $bestMatchInNew, $bestMatchSize);
549+
}
550+
551+
return null;
530552
}
531553
}

0 commit comments

Comments
 (0)