|
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 SN\DaisyDiff\Html\Ancestor; |
|
12
|
|
|
|
|
13
|
|
|
use SN\DaisyDiff\Html\Ancestor\TagToString\TagToStringFactory; |
|
14
|
|
|
use SN\DaisyDiff\Html\ChangeText; |
|
15
|
|
|
use SN\DaisyDiff\Html\Dom\TagNode; |
|
16
|
|
|
use SN\DaisyDiff\Html\Modification\HtmlLayoutChange; |
|
17
|
|
|
use SN\DaisyDiff\RangeDifferencer\RangeDifference; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ChangeTextGenerator |
|
21
|
|
|
*/ |
|
22
|
|
|
class ChangeTextGenerator |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var HtmlLayoutChange[] */ |
|
25
|
|
|
private $htmlLayoutChanges = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var AncestorComparator */ |
|
28
|
|
|
private $ancestorComparator; |
|
29
|
|
|
|
|
30
|
|
|
/** @var AncestorComparator */ |
|
31
|
|
|
private $other; |
|
32
|
|
|
|
|
33
|
|
|
/** @var TagToStringFactory */ |
|
34
|
|
|
private $factory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param AncestorComparator $ancestorComparator |
|
38
|
|
|
* @param AncestorComparator $other |
|
39
|
|
|
*/ |
|
40
|
23 |
|
public function __construct(AncestorComparator $ancestorComparator, AncestorComparator $other) |
|
41
|
|
|
{ |
|
42
|
23 |
|
$this->ancestorComparator = $ancestorComparator; |
|
43
|
23 |
|
$this->other = $other; |
|
44
|
23 |
|
$this->factory = new TagToStringFactory(); |
|
45
|
23 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param RangeDifference[] $differences |
|
49
|
|
|
* @return ChangeText |
|
50
|
|
|
*/ |
|
51
|
22 |
|
public function getChanged(array $differences): ChangeText |
|
52
|
|
|
{ |
|
53
|
22 |
|
$text = new ChangeText(); |
|
54
|
22 |
|
$rootListOpened = false; |
|
55
|
|
|
|
|
56
|
22 |
|
if (\count($differences) > 1) { |
|
57
|
|
|
$text->startElement('ul', ['class' => 'changelist']); |
|
58
|
|
|
$rootListOpened = true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
22 |
|
for ($j = 0, $jMax = \count($differences); $j < $jMax; $j++) { |
|
62
|
|
|
/** @var RangeDifference $d */ |
|
63
|
22 |
|
$d = $differences[$j]; |
|
64
|
22 |
|
$lvl1ListOpened = false; |
|
65
|
|
|
|
|
66
|
22 |
|
if ($rootListOpened) { |
|
67
|
|
|
$text->startElement('li'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
22 |
|
if ($d->getLeftLength() + $d->getRightLength() > 1) { |
|
71
|
16 |
|
$text->startElement('ul', ['class' => 'changelist']); |
|
72
|
16 |
|
$lvl1ListOpened = true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Left are the old ones. |
|
76
|
22 |
|
for ($i = $d->getLeftStart(), $iMax = $d->getLeftEnd(); $i < $iMax; $i++) { |
|
77
|
19 |
|
if ($lvl1ListOpened) { |
|
78
|
15 |
|
$text->startElement('li'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Add a bullet for an old tag. |
|
82
|
19 |
|
$this->addTagOld($text, $this->other->getAncestor($i)); |
|
83
|
|
|
|
|
84
|
19 |
|
if ($lvl1ListOpened) { |
|
85
|
15 |
|
$text->endElement('li'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Right are the new ones. |
|
90
|
22 |
|
for ($i = $d->getRightStart(), $iMax = $d->getRightEnd(); $i < $iMax; $i++) { |
|
91
|
19 |
|
if ($lvl1ListOpened) { |
|
92
|
16 |
|
$text->startElement('li'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Add a bullet for an old tag. |
|
96
|
19 |
|
$this->addTagNew($text, $this->getAncestor($i)); |
|
97
|
|
|
|
|
98
|
19 |
|
if ($lvl1ListOpened) { |
|
99
|
16 |
|
$text->endElement('li'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
22 |
|
if ($lvl1ListOpened) { |
|
104
|
16 |
|
$text->endElement('ul'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
22 |
|
if ($rootListOpened) { |
|
108
|
|
|
$text->endElement('li'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
22 |
|
if ($rootListOpened) { |
|
113
|
|
|
$text->endElement('ul'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
22 |
|
return $text; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param ChangeText $text |
|
121
|
|
|
* @param TagNode $ancestor |
|
122
|
|
|
*/ |
|
123
|
19 |
|
private function addTagOld(ChangeText $text, TagNode $ancestor): void |
|
124
|
|
|
{ |
|
125
|
19 |
|
$tagToString = $this->factory->create($ancestor); |
|
126
|
19 |
|
$tagToString->getRemovedDescription($text); |
|
127
|
19 |
|
$this->htmlLayoutChanges[] = $tagToString->getHtmlLayoutChange(); |
|
128
|
19 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param ChangeText $text |
|
132
|
|
|
* @param TagNode $ancestor |
|
133
|
|
|
*/ |
|
134
|
19 |
|
private function addTagNew(ChangeText $text, TagNode $ancestor): void |
|
135
|
|
|
{ |
|
136
|
19 |
|
$tagToString = $this->factory->create($ancestor); |
|
137
|
19 |
|
$tagToString->getAddedDescription($text); |
|
138
|
19 |
|
$this->htmlLayoutChanges[] = $tagToString->getHtmlLayoutChange(); |
|
139
|
19 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param int $index |
|
143
|
|
|
* @return TagNode |
|
144
|
|
|
*/ |
|
145
|
19 |
|
private function getAncestor(int $index): TagNode |
|
146
|
|
|
{ |
|
147
|
19 |
|
return $this->ancestorComparator->getAncestor($index); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return HtmlLayoutChange[] |
|
152
|
|
|
*/ |
|
153
|
22 |
|
public function getHtmlLayoutChanges(): array |
|
154
|
|
|
{ |
|
155
|
22 |
|
return $this->htmlLayoutChanges; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|