1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait PrototypeNodeTrait |
4
|
|
|
* |
5
|
|
|
* @filesource PrototypeNodeTrait.php |
6
|
|
|
* @created 11.05.2017 |
7
|
|
|
* @package chillerlan\PrototypeDOM\Node |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
* |
12
|
|
|
* @noinspection PhpParamsInspection |
13
|
|
|
* @noinspection PhpIncompatibleReturnTypeInspection |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace chillerlan\PrototypeDOM\Node; |
17
|
|
|
|
18
|
|
|
use chillerlan\PrototypeDOM\NodeList; |
19
|
|
|
|
20
|
|
|
use function trim; |
21
|
|
|
|
22
|
|
|
use const XML_ELEMENT_NODE, XML_TEXT_NODE; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @implements \chillerlan\PrototypeDOM\Node\PrototypeNode |
26
|
|
|
* |
27
|
|
|
* @property string $nodeName |
28
|
|
|
* @property string $nodeValue |
29
|
|
|
* @property int $nodeType |
30
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $parentNode |
31
|
|
|
* @property \DOMNodeList $childNodes |
32
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $firstChild |
33
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $lastChild |
34
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $previousSibling |
35
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $nextSibling |
36
|
|
|
* @property \DOMNamedNodeMap $attributes |
37
|
|
|
* @property \chillerlan\PrototypeDOM\Document $ownerDocument |
38
|
|
|
* @property string $namespaceURI |
39
|
|
|
* @property string $prefix |
40
|
|
|
* @property string $localName |
41
|
|
|
* @property string $baseURI |
42
|
|
|
* @property string $textContent |
43
|
|
|
*/ |
44
|
|
|
trait PrototypeNodeTrait{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public function recursivelyCollect(string $property, int $maxLength = null, int $nodeType = null):NodeList{ |
50
|
|
|
return $this->ownerDocument->recursivelyCollect($this, $property, $maxLength ?? -1, $nodeType ?? XML_ELEMENT_NODE); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function empty():bool{ |
57
|
|
|
return empty(trim($this->nodeValue)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function inspect(bool $xml = null):string{ |
64
|
|
|
return $this->ownerDocument->inspect($this, $xml); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function removeNode():PrototypeNode{ |
71
|
|
|
|
72
|
|
|
if(!$this->parentNode){ |
73
|
|
|
return $this; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->parentNode->removeChild($this); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function replace(PrototypeNode $newNode):PrototypeNode{ |
83
|
|
|
|
84
|
|
|
if(!$this->parentNode){ |
85
|
|
|
return $this; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->parentNode->replaceChild($this->importNode($newNode), $this); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function cleanWhitespace():PrototypeNode{ |
95
|
|
|
$node = $this->firstChild; |
96
|
|
|
|
97
|
|
|
while($node){ |
98
|
|
|
$nextNode = $node->nextSibling; |
99
|
|
|
|
100
|
|
|
if($node->nodeType === XML_TEXT_NODE && $node->empty()){ |
101
|
|
|
$node->removeNode(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$node = $nextNode; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function purge():PrototypeNode{ |
114
|
|
|
|
115
|
|
|
while($this->hasChildNodes()){ |
|
|
|
|
116
|
|
|
$this->firstChild->removeNode(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function importNode(PrototypeNode $newNode):PrototypeNode{ |
126
|
|
|
return $this->ownerDocument->importNode($newNode, true); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function match(string $selector):bool{ |
133
|
|
|
return $this->ownerDocument->match($this, $selector); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|