Skip to content

Commit 0f1f494

Browse files
committed
Updates for diff matching
1 parent 0f19a43 commit 0f1f494

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function build()
7575

7676
$this->diffDom = new \DOMDocument();
7777

78-
$this->normalizeFormat();
78+
// $this->normalizeFormat();
7979

8080
$this->indexCellValues($this->newTable);
8181

@@ -172,13 +172,15 @@ protected function diffTableContent()
172172
}
173173

174174
// similar_text
175-
$percentage = $this->getMatchPercentage($oldRow, $newRow);
175+
$percentage = $this->getMatchPercentage($oldRow, $newRow, $oldIndex, $newIndex);
176176

177177
$oldMatchData[$oldIndex][$newIndex] = $percentage;
178178
$newMatchData[$newIndex][$oldIndex] = $percentage;
179179
}
180180
}
181181

182+
addDebugOutput($newMatchData, 'matches');
183+
182184
// new solution for diffing rows
183185
switch ($this->strategy) {
184186
case self::STRATEGY_MATCHING:
@@ -267,6 +269,7 @@ protected function diffTableRowsWithMatches($oldRows, $newRows, $matches)
267269

268270
protected function processInsertOperation(Operation $operation, $newRows, &$appliedRowSpans, $forceExpansion = false)
269271
{
272+
addDebugOutput($operation, __METHOD__);
270273
$targetRows = array_slice($newRows, $operation->startInNew, $operation->endInNew - $operation->startInNew);
271274
foreach ($targetRows as $row) {
272275
$this->diffAndAppendRows(null, $row, $appliedRowSpans, $forceExpansion);
@@ -275,6 +278,7 @@ protected function processInsertOperation(Operation $operation, $newRows, &$appl
275278

276279
protected function processDeleteOperation(Operation $operation, $oldRows, &$appliedRowSpans, $forceExpansion = false)
277280
{
281+
addDebugOutput($operation, __METHOD__);
278282
$targetRows = array_slice($oldRows, $operation->startInOld, $operation->endInOld - $operation->startInOld);
279283
foreach ($targetRows as $row) {
280284
$this->diffAndAppendRows($row, null, $appliedRowSpans, $forceExpansion);
@@ -283,6 +287,7 @@ protected function processDeleteOperation(Operation $operation, $oldRows, &$appl
283287

284288
protected function processEqualOperation(Operation $operation, $oldRows, $newRows, &$appliedRowSpans)
285289
{
290+
addDebugOutput($operation, __METHOD__);
286291
$targetOldRows = array_values(array_slice($oldRows, $operation->startInOld, $operation->endInOld - $operation->startInOld));
287292
$targetNewRows = array_values(array_slice($newRows, $operation->startInNew, $operation->endInNew - $operation->startInNew));
288293

@@ -297,6 +302,7 @@ protected function processEqualOperation(Operation $operation, $oldRows, $newRow
297302

298303
protected function processReplaceOperation(Operation $operation, $oldRows, $newRows, &$appliedRowSpans)
299304
{
305+
addDebugOutput($operation, __METHOD__);
300306
$this->processDeleteOperation($operation, $oldRows, $appliedRowSpans, true);
301307
$this->processInsertOperation($operation, $newRows, $appliedRowSpans, true);
302308
}
@@ -835,11 +841,12 @@ protected function diffAndAppendRows($oldRow, $newRow, &$appliedRowSpans, $force
835841
}
836842
}
837843

838-
protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow)
844+
protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow, $oldIndex, $newIndex)
839845
{
840-
$firstCellWeight = 3;
846+
$firstCellWeight = 1.5;
847+
$indexDeltaWeight = 0.25 * (abs($oldIndex - $newIndex));
841848
$thresholdCount = 0;
842-
$totalCount = (min(count($newRow->getCells()), count($oldRow->getCells())) + $firstCellWeight) * 100;
849+
$totalCount = (min(count($newRow->getCells()), count($oldRow->getCells())) + $firstCellWeight + $indexDeltaWeight) * 100;
843850
foreach ($newRow->getCells() as $newIndex => $newCell) {
844851
$oldCell = $oldRow->getCell($newIndex);
845852

0 commit comments

Comments
 (0)