Skip to content

Commit 4d74d11

Browse files
committed
Create classes for table elements, and start on matching logic
1 parent f249285 commit 4d74d11

File tree

12 files changed

+750
-323
lines changed

12 files changed

+750
-323
lines changed

demo/index.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22

33
use Caxy\HtmlDiff\HtmlDiff;
44

5-
require __DIR__.'/../lib/Caxy/HtmlDiff/AbstractDiff.php';
6-
require __DIR__.'/../lib/Caxy/HtmlDiff/HtmlDiff.php';
7-
require __DIR__.'/../lib/Caxy/HtmlDiff/TableDiff.php';
8-
require __DIR__.'/../lib/Caxy/HtmlDiff/Match.php';
9-
require __DIR__.'/../lib/Caxy/HtmlDiff/Operation.php';
10-
11-
$input = file_get_contents('php://input');
12-
135
ini_set('display_errors', 1);
146
error_reporting(E_ERROR);
157

8+
$classes = array(
9+
'Caxy/HtmlDiff/AbstractDiff',
10+
'Caxy/HtmlDiff/HtmlDiff',
11+
'Caxy/HtmlDiff/Table/TableDiff',
12+
'Caxy/HtmlDiff/Table/AbstractTableElement',
13+
'Caxy/HtmlDiff/Table/Table',
14+
'Caxy/HtmlDiff/Table/TableRow',
15+
'Caxy/HtmlDiff/Table/TableCell',
16+
'Caxy/HtmlDiff/Table/TablePosition',
17+
'Caxy/HtmlDiff/Table/TableMatch',
18+
'Caxy/HtmlDiff/Match',
19+
'Caxy/HtmlDiff/Operation',
20+
);
21+
22+
foreach ($classes as $class) {
23+
require __DIR__.'/../lib/'.$class.'.php';
24+
}
25+
26+
$input = file_get_contents('php://input');
27+
1628
if ($input) {
1729
$data = json_decode($input, true);
1830
$diff = new HtmlDiff($data['oldText'], $data['newText'], 'UTF-8', array());

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Caxy\HtmlDiff;
44

5+
use Caxy\HtmlDiff\Table\TableDiff;
6+
57
class HtmlDiff extends AbstractDiff
68
{
79
protected $wordIndices;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff\Table;
4+
5+
abstract class AbstractTableElement
6+
{
7+
protected $domNode;
8+
9+
public function __construct(\DOMNode $domNode = null)
10+
{
11+
$this->domNode = $domNode;
12+
}
13+
14+
public function getDomNode()
15+
{
16+
return $this->domNode;
17+
}
18+
19+
public function setDomNode(\DOMNode $domNode)
20+
{
21+
$this->domNode = $domNode;
22+
23+
return $this;
24+
}
25+
26+
public function getInnerHtml()
27+
{
28+
$innerHtml = '';
29+
30+
if ($this->domNode) {
31+
foreach ($this->domNode->childNodes as $child) {
32+
$innerHtml .= static::htmlFromNode($child);
33+
}
34+
}
35+
36+
return $innerHtml;
37+
}
38+
39+
public static function htmlFromNode($node)
40+
{
41+
$domDocument = new \DOMDocument();
42+
$newNode = $domDocument->importNode($node, true);
43+
$domDocument->appendChild($newNode);
44+
return trim($domDocument->saveHTML());
45+
}
46+
}

lib/Caxy/HtmlDiff/Table/Table.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff\Table;
4+
5+
class Table extends AbstractTableElement
6+
{
7+
protected $rows = array();
8+
9+
protected $domNode;
10+
11+
public function getRows()
12+
{
13+
return $this->rows;
14+
}
15+
16+
public function addRow(TableRow $row)
17+
{
18+
$this->rows[] = $row;
19+
20+
if (!$row->getTable()) {
21+
$row->setTable($this);
22+
}
23+
}
24+
25+
public function removeRow(TableRow $row)
26+
{
27+
$key = array_search($row, $this->rows, true);
28+
29+
if ($key !== false) {
30+
unset($this->rows[$key]);
31+
if ($row->getTable()) {
32+
$row->setTable(null);
33+
}
34+
}
35+
}
36+
37+
public function getRow($index)
38+
{
39+
return isset($this->rows[$index]) ? $this->rows[$index] : null;
40+
}
41+
42+
public function insertRows($rows, $position = null)
43+
{
44+
if ($position === null) {
45+
$this->rows = array_merge($this->rows, $rows);
46+
} else {
47+
array_splice($this->rows, $position, 0, $rows);
48+
}
49+
}
50+
51+
public function getCellByPosition(TablePosition $position)
52+
{
53+
$row = $this->getRow($position->getRow());
54+
55+
return $row ? $row->getCell($position->getCell()) : null;
56+
}
57+
58+
public function getPositionBefore(TablePosition $position, $offset = 1)
59+
{
60+
if ($position->getCell() > ($offset - 1)) {
61+
$newRow = $position->getRow();
62+
$newCell = $position->getCell() - $offset;
63+
} elseif ($position->getRow() > 0) {
64+
$cellsToMove = $offset;
65+
$newRow = $position->getRow();
66+
$newCell = $position->getCell();
67+
68+
while ($cellsToMove > 0 && $newRow >= 0) {
69+
if ($cellsToMove > $newCell) {
70+
$newRow--;
71+
if ($newRow < 0) {
72+
return null;
73+
}
74+
75+
$cellsToMove = $cellsToMove - ($newCell + 1);
76+
$cellCount = count($this->getRow($newRow)->getCells());
77+
$newCell = $cellCount - 1;
78+
} else {
79+
$newCell = $newCell - $cellsToMove;
80+
$cellsToMove -= $newCell;
81+
}
82+
}
83+
} else {
84+
return null;
85+
}
86+
87+
if ($newRow >= 0 && $newCell >= 0) {
88+
return new TablePosition($newRow, $newCell);
89+
}
90+
91+
return null;
92+
}
93+
94+
public function getPositionAfter(TablePosition $position, $offset = 1)
95+
{
96+
$cellsToMove = $offset;
97+
$newRow = $position->getRow();
98+
$newCell = $position->getCell();
99+
100+
while ($cellsToMove > 0 && $newRow < count($this->rows)) {
101+
$cellCount = count($this->getRow($newRow)->getCells());
102+
103+
$cellsLeft = $cellCount - $newCell - 1;
104+
105+
if ($cellsToMove > $cellsLeft) {
106+
$newRow++;
107+
$cellsToMove -= $cellsLeft - 1;
108+
$newCell = 0;
109+
} else {
110+
$newCell = $newCell + $cellsToMove;
111+
$cellsToMove -= $cellsLeft;
112+
}
113+
}
114+
115+
if ($newRow >= 0 && $newCell >= 0) {
116+
return new TablePosition($newRow, $newCell);
117+
}
118+
119+
return null;
120+
}
121+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff\Table;
4+
5+
class TableCell extends AbstractTableElement
6+
{
7+
protected $row;
8+
9+
public function getRow()
10+
{
11+
return $this->row;
12+
}
13+
14+
public function setRow(TableRow $row = null)
15+
{
16+
$this->row = $row;
17+
18+
if (!in_array($this, $row->getCells())) {
19+
$row->addCell($this);
20+
}
21+
22+
return $this;
23+
}
24+
}

0 commit comments

Comments
 (0)