Skip to content

Commit 234d5e3

Browse files
committed
Add diffing strategy selection
1 parent a9a6789 commit 234d5e3

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
abstract class AbstractDiff
66
{
7+
const STRATEGY_MATCHING = 'matching';
8+
const STRATEGY_RELATIVE = 'relative';
9+
710
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
811
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\'');
912
public static $defaultGroupDiffs = true;
@@ -22,6 +25,8 @@ abstract class AbstractDiff
2225
protected $matchThreshold = 80;
2326
protected $debug = false;
2427

28+
protected $strategy = self::STRATEGY_MATCHING;
29+
2530
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
2631
{
2732
if ($specialCaseTags === null) {
@@ -41,6 +46,18 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
4146
$this->setSpecialCaseChars(static::$defaultSpecialCaseChars);
4247
}
4348

49+
public function setStrategy($strategy)
50+
{
51+
$this->strategy = $strategy;
52+
53+
return $this;
54+
}
55+
56+
public function getStrategy()
57+
{
58+
return $this->strategy;
59+
}
60+
4461
public function setDebug($debug)
4562
{
4663
$this->debug = $debug;

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ protected function diffTables($oldText, $newText)
265265
{
266266
$diff = new TableDiff($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
267267
$diff->setMatchThreshold($this->matchThreshold);
268+
$diff->setStrategy($this->strategy);
268269

269270
return $diff->build();
270271
}

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
*/
1717
class TableDiff extends AbstractDiff
1818
{
19-
const STRATEGY_MATCHING = 'matching';
20-
const STRATEGY_RELATIVE = 'relative';
21-
2219
/**
2320
* @var null|Table
2421
*/
@@ -69,18 +66,6 @@ public function __construct($oldText, $newText, $encoding, $specialCaseTags, $gr
6966
$this->purifier = new \HTMLPurifier($config);
7067
}
7168

72-
public function setStrategy($strategy)
73-
{
74-
$this->strategy = $strategy;
75-
76-
return $this;
77-
}
78-
79-
public function getStrategy()
80-
{
81-
return $this->strategy;
82-
}
83-
8469
public function build()
8570
{
8671
$this->buildTableDoms();
@@ -185,26 +170,18 @@ protected function diffTableContent()
185170
$newMatchData[$newIndex] = array();
186171
}
187172

188-
$oldText = $oldRow->getInnerHtml();
189-
$newText = $newRow->getInnerHtml();
190-
191173
// similar_text
192-
// $percentage = null;
193-
// similar_text($oldText, $newText, $percentage);
194174
$percentage = $this->getMatchPercentage($oldRow, $newRow);
195175

196176
$oldMatchData[$oldIndex][$newIndex] = $percentage;
197177
$newMatchData[$newIndex][$oldIndex] = $percentage;
198178
}
199179
}
200180

201-
$matches = $this->getRowMatches($oldMatchData, $newMatchData);
202-
203-
addDebugOutput($matches, __METHOD__);
204-
205181
// new solution for diffing rows
206182
switch ($this->strategy) {
207183
case self::STRATEGY_MATCHING:
184+
$matches = $this->getRowMatches($oldMatchData, $newMatchData);
208185
$this->diffTableRowsWithMatches($oldRows, $newRows, $matches);
209186
break;
210187

@@ -213,6 +190,7 @@ protected function diffTableContent()
213190
break;
214191

215192
default:
193+
$matches = $this->getRowMatches($oldMatchData, $newMatchData);
216194
$this->diffTableRowsWithMatches($oldRows, $newRows, $matches);
217195
break;
218196
}
@@ -309,7 +287,6 @@ protected function processEqualOperation($operation, $oldRows, $newRows, &$appli
309287

310288
foreach ($targetNewRows as $index => $newRow) {
311289
if (!isset($targetOldRows[$index])) {
312-
addDebugOutput('failed finding matchign row', __METHOD__);
313290
continue;
314291
}
315292

@@ -433,7 +410,7 @@ protected function diffTableRows($oldRows, $newRows, $oldMatchData)
433410

434411
if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) {
435412
// insert row as new
436-
$this->diffAndAppendRows(null, $row, $appliedRowSpans, true);
413+
$this->diffAndAppendRows(null, $row, $appliedRowSpans);
437414
$difference--;
438415

439416
continue;

0 commit comments

Comments
 (0)