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\Dom; |
12
|
|
|
|
13
|
|
|
use SN\DaisyDiff\Html\Modification\Modification; |
14
|
|
|
use SN\DaisyDiff\Html\Modification\ModificationType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents a piece of text in the HTML file. |
18
|
|
|
*/ |
19
|
|
|
class TextNode extends Node |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
private $s = ''; |
23
|
|
|
|
24
|
|
|
/** @var Modification|null */ |
25
|
|
|
private $modification; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param TagNode $parent |
29
|
|
|
* @param string $s |
30
|
|
|
*/ |
31
|
71 |
|
public function __construct(?TagNode $parent, string $s) |
32
|
|
|
{ |
33
|
71 |
|
parent::__construct($parent); |
34
|
|
|
|
35
|
71 |
|
$this->modification = new Modification(ModificationType::NONE, ModificationType::NONE); |
36
|
71 |
|
$this->s = $s; |
37
|
71 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @todo Check if we need to deep clone this. |
41
|
|
|
* |
42
|
|
|
* @return Node |
43
|
|
|
*/ |
44
|
9 |
|
public function copyTree(): Node |
45
|
|
|
{ |
46
|
9 |
|
$node = clone $this; |
47
|
9 |
|
$node->setParent(null); |
48
|
|
|
|
49
|
9 |
|
return $node; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Node |
54
|
|
|
*/ |
55
|
13 |
|
public function getLeftMostChild(): Node |
56
|
|
|
{ |
57
|
13 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Node |
62
|
|
|
*/ |
63
|
13 |
|
public function getRightMostChild(): Node |
64
|
|
|
{ |
65
|
13 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
11 |
|
public function getMinimalDeletedSet(int $id): array |
72
|
|
|
{ |
73
|
11 |
|
$nodes = []; |
74
|
11 |
|
$modification = $this->getModification(); |
75
|
|
|
|
76
|
11 |
|
if ($modification->getType() === ModificationType::REMOVED && $modification->getId() === $id) { |
77
|
11 |
|
$nodes[] = $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
11 |
|
return $nodes; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
36 |
|
public function getText(): string |
87
|
|
|
{ |
88
|
36 |
|
return $this->s; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Node|null $other |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
17 |
|
public function isSameText(?Node $other): bool |
96
|
|
|
{ |
97
|
17 |
|
if ($other instanceof TextNode) { |
98
|
17 |
|
return $this->getText() === $other->getText(); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Modification|null |
106
|
|
|
*/ |
107
|
43 |
|
public function getModification(): ?Modification |
108
|
|
|
{ |
109
|
43 |
|
return $this->modification; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Modification|null $m |
114
|
|
|
*/ |
115
|
42 |
|
public function setModification(?Modification $m): void |
116
|
|
|
{ |
117
|
42 |
|
$this->modification = $m; |
118
|
42 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
7 |
|
public function __toString(): string |
124
|
|
|
{ |
125
|
7 |
|
return $this->getText(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|