1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Steve Nebes <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace DaisyDiff; |
12
|
|
|
|
13
|
|
|
use DaisyDiff\Html\Ancestor\ChangeText; |
14
|
|
|
use DaisyDiff\Html\DelegatingContentHandler; |
15
|
|
|
use DaisyDiff\Html\Dom\DomTreeBuilder; |
16
|
|
|
use DaisyDiff\Html\HtmlDiffer; |
17
|
|
|
use DaisyDiff\Html\HtmlSaxDiffOutput; |
18
|
|
|
use DaisyDiff\Html\TextNodeComparator; |
19
|
|
|
use DaisyDiff\Xml\XMLReader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Daisy Diff is a library that diffs (compares) HTML. |
23
|
|
|
*/ |
24
|
|
|
class DaisyDiff |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Diffs two HTML strings, returning the result. |
28
|
|
|
* |
29
|
|
|
* @param string $oldSource |
30
|
|
|
* @param string $newSource |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function diff(string $oldSource, string $newSource): string |
34
|
|
|
{ |
35
|
|
|
// Parse $old XML. |
36
|
|
|
$oldHandler = new DomTreeBuilder(); |
37
|
|
|
$reader1 = new XMLReader($oldHandler); |
38
|
|
|
$reader1->parse($oldSource); |
39
|
|
|
|
40
|
|
|
// Parse $new XML. |
41
|
|
|
$newHandler = new DomTreeBuilder(); |
42
|
|
|
$reader2 = new XMLReader($newHandler); |
43
|
|
|
$reader2->parse($newSource); |
44
|
|
|
|
45
|
|
|
// Comparators. |
46
|
|
|
$leftComparator = new TextNodeComparator($oldHandler); |
47
|
|
|
$rightComparator = new TextNodeComparator($newHandler); |
48
|
|
|
|
49
|
|
|
$content = new ChangeText(); |
50
|
|
|
$handler = new DelegatingContentHandler($content); |
51
|
|
|
$output = new HtmlSaxDiffOutput($handler, 'diff'); |
52
|
|
|
$differ = new HtmlDiffer($output); |
53
|
|
|
$differ->diff($leftComparator, $rightComparator); |
54
|
|
|
|
55
|
|
|
return strval($content); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|