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
|
|
|
* @property string $nodeName |
26
|
|
|
* @property string $nodeValue |
27
|
|
|
* @property int $nodeType |
28
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $parentNode |
29
|
|
|
* @property \DOMNodeList $childNodes |
30
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $firstChild |
31
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $lastChild |
32
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $previousSibling |
33
|
|
|
* @property \chillerlan\PrototypeDOM\Node\Element $nextSibling |
34
|
|
|
* @property \DOMNamedNodeMap $attributes |
35
|
|
|
* @property \chillerlan\PrototypeDOM\Document $ownerDocument |
36
|
|
|
* @property string $namespaceURI |
37
|
|
|
* @property string $prefix |
38
|
|
|
* @property string $localName |
39
|
|
|
* @property string $baseURI |
40
|
|
|
* @property string $textContent |
41
|
|
|
*/ |
42
|
|
|
trait PrototypeNodeTrait{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function recursivelyCollect(string $property, int $maxLength = null, int $nodeType = null):NodeList{ |
48
|
|
|
return $this->ownerDocument->recursivelyCollect($this, $property, $maxLength ?? -1, $nodeType ?? XML_ELEMENT_NODE); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
|
|
public function empty():bool{ |
55
|
|
|
return empty(trim($this->nodeValue)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function inspect(bool $xml = null):string{ |
62
|
|
|
return $this->ownerDocument->inspect($this, $xml); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function remove():PrototypeNode{ |
69
|
|
|
|
70
|
|
|
if(!$this->parentNode){ |
71
|
|
|
return $this; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->parentNode->removeChild($this); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function replace(PrototypeNode $newnode):PrototypeNode{ |
81
|
|
|
|
82
|
|
|
if(!$this->parentNode){ |
83
|
|
|
return $this; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->parentNode->replaceChild($this->importNode($newnode), $this); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function cleanWhitespace():PrototypeNode{ |
93
|
|
|
$node = $this->firstChild; |
94
|
|
|
|
95
|
|
|
while($node){ |
96
|
|
|
$nextNode = $node->nextSibling; |
97
|
|
|
|
98
|
|
|
if($node->nodeType === XML_TEXT_NODE && $node->empty()){ |
99
|
|
|
$node->remove(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$node = $nextNode; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function purge():PrototypeNode{ |
112
|
|
|
|
113
|
|
|
while($this->hasChildNodes()){ |
|
|
|
|
114
|
|
|
$this->firstChild->remove(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function importNode(PrototypeNode $newNode):PrototypeNode{ |
124
|
|
|
return $this->ownerDocument->importNode($newNode, true); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritDoc |
129
|
|
|
*/ |
130
|
|
|
public function match(string $selector):bool{ |
131
|
|
|
return $this->ownerDocument->match($this, $selector); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|