@@ -87,6 +87,9 @@ discard block |
||
| 87 | 87 | * ugly * |
| 88 | 88 | ********/ |
| 89 | 89 | |
| 90 | + /** |
|
| 91 | + * @param string $content |
|
| 92 | + */ |
|
| 90 | 93 | public function _loadDocument($content, $xml = false){ |
| 91 | 94 | |
| 92 | 95 | switch(true){ |
@@ -108,7 +111,7 @@ discard block |
||
| 108 | 111 | } |
| 109 | 112 | |
| 110 | 113 | /** |
| 111 | - * @param mixed $content |
|
| 114 | + * @param DOMNodeList $content |
|
| 112 | 115 | * |
| 113 | 116 | * @return \chillerlan\PrototypeDOM\NodeList |
| 114 | 117 | * @throws \Exception |
@@ -12,7 +12,10 @@ |
||
| 12 | 12 | |
| 13 | 13 | namespace chillerlan\PrototypeDOM; |
| 14 | 14 | |
| 15 | -use DOMDocument, DOMNode, DOMNodeList, DOMXPath; |
|
| 15 | +use DOMDocument; |
|
| 16 | +use DOMNode; |
|
| 17 | +use DOMNodeList; |
|
| 18 | +use DOMXPath; |
|
| 16 | 19 | use chillerlan\PrototypeDOM\Node\{Element, PrototypeNode}; |
| 17 | 20 | use chillerlan\PrototypeDOM\Traits\Magic; |
| 18 | 21 | use Symfony\Component\CssSelector\CssSelectorConverter; |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | /** |
| 21 | 21 | * @property string $title |
| 22 | 22 | */ |
| 23 | -class Document extends DOMDocument{ |
|
| 23 | +class Document extends DOMDocument { |
|
| 24 | 24 | use Magic; |
| 25 | 25 | |
| 26 | 26 | const NODE_CLASSES = [ |
@@ -41,14 +41,14 @@ discard block |
||
| 41 | 41 | * @param string|null $version |
| 42 | 42 | * @param string|null $encoding |
| 43 | 43 | */ |
| 44 | - public function __construct($content = null, $xml = false, $version = '1.0', $encoding = 'UTF-8'){ |
|
| 44 | + public function __construct($content = null, $xml = false, $version = '1.0', $encoding = 'UTF-8') { |
|
| 45 | 45 | parent::__construct($version, $encoding); |
| 46 | 46 | |
| 47 | - foreach(self::NODE_CLASSES as $nodeClass){ |
|
| 47 | + foreach (self::NODE_CLASSES as $nodeClass) { |
|
| 48 | 48 | $this->registerNodeClass('DOM'.$nodeClass, __NAMESPACE__.'\\Node\\'.$nodeClass); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if(!is_null($content)){ |
|
| 51 | + if (!is_null($content)) { |
|
| 52 | 52 | $this->_loadDocument($content, $xml); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -59,21 +59,21 @@ discard block |
||
| 59 | 59 | * magic * |
| 60 | 60 | *********/ |
| 61 | 61 | |
| 62 | - public function magic_get_title(){ |
|
| 62 | + public function magic_get_title() { |
|
| 63 | 63 | return $this->select('head > title')->item(0)->nodeValue ?? null; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - public function magic_set_title(string $title){ |
|
| 66 | + public function magic_set_title(string $title) { |
|
| 67 | 67 | $currentTitle = $this->select('head > title')->item(0); |
| 68 | 68 | |
| 69 | - if($currentTitle instanceof Element){ |
|
| 69 | + if ($currentTitle instanceof Element) { |
|
| 70 | 70 | $currentTitle->update($title); |
| 71 | 71 | } |
| 72 | - else{ |
|
| 72 | + else { |
|
| 73 | 73 | $head = $this->select('head')->item(0); |
| 74 | 74 | $currentTitle = $this->newElement('title')->update($title); |
| 75 | 75 | |
| 76 | - if(!$head){ |
|
| 76 | + if (!$head) { |
|
| 77 | 77 | $head = $this->appendChild($this->newElement('head')); |
| 78 | 78 | } |
| 79 | 79 | |
@@ -87,9 +87,9 @@ discard block |
||
| 87 | 87 | * ugly * |
| 88 | 88 | ********/ |
| 89 | 89 | |
| 90 | - public function _loadDocument($content, $xml = false){ |
|
| 90 | + public function _loadDocument($content, $xml = false) { |
|
| 91 | 91 | |
| 92 | - switch(true){ |
|
| 92 | + switch (true) { |
|
| 93 | 93 | case $content instanceof NodeList : return $this->insertNodeList($content); |
| 94 | 94 | case $content instanceof DOMNodeList: return $this->insertNodeList(new NodeList($content)); |
| 95 | 95 | case is_string($content) : return $this->_loadDocumentString($content, $xml); |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - public function _loadDocumentString(string $documentSource, bool $xml = false){ |
|
| 101 | - $options = LIBXML_COMPACT|LIBXML_NONET; |
|
| 100 | + public function _loadDocumentString(string $documentSource, bool $xml = false) { |
|
| 101 | + $options = LIBXML_COMPACT | LIBXML_NONET; |
|
| 102 | 102 | |
| 103 | 103 | $xml |
| 104 | 104 | ? $this->loadXML($documentSource, $options) |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | public function _toNodeList($content):NodeList{ |
| 117 | 117 | |
| 118 | - switch(true){ |
|
| 118 | + switch (true) { |
|
| 119 | 119 | case $content instanceof NodeList : return $content; |
| 120 | 120 | case $content instanceof DOMNodeList: return new NodeList($content); |
| 121 | 121 | case $content instanceof DOMNode : return $this->_arrayToNodeList([$content]); |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | public function _arrayToNodeList(array $array):NodeList{ |
| 148 | 148 | $nodelist = new NodeList; |
| 149 | 149 | |
| 150 | - foreach($array as $node){ |
|
| 150 | + foreach ($array as $node) { |
|
| 151 | 151 | $nodelist[] = $node; |
| 152 | 152 | } |
| 153 | 153 | |
@@ -200,9 +200,9 @@ discard block |
||
| 200 | 200 | public function removeElementsBySelector($selectors, DOMNode $contextNode = null, string $axis = 'descendant-or-self::'):Document{ |
| 201 | 201 | $nodes = $this->select($selectors, $contextNode, $axis); |
| 202 | 202 | |
| 203 | - if(count($nodes) > 0){ |
|
| 203 | + if (count($nodes) > 0) { |
|
| 204 | 204 | /** @var \chillerlan\PrototypeDOM\Node\Element $node */ |
| 205 | - foreach($nodes as $node){ |
|
| 205 | + foreach ($nodes as $node) { |
|
| 206 | 206 | $node->remove(); |
| 207 | 207 | } |
| 208 | 208 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | public function insertNodeList(NodeList $nodeList):Document{ |
| 220 | 220 | |
| 221 | 221 | /** @var \DOMNode $node */ |
| 222 | - foreach($nodeList as $node){ |
|
| 222 | + foreach ($nodeList as $node) { |
|
| 223 | 223 | $this->appendChild($this->importNode($node->cloneNode(true), true)); |
| 224 | 224 | } |
| 225 | 225 | |
@@ -256,21 +256,21 @@ discard block |
||
| 256 | 256 | */ |
| 257 | 257 | public function select($selectors = null, DOMNode $contextNode = null, string $axis = 'descendant-or-self::', int $nodeType = XML_ELEMENT_NODE):NodeList{ |
| 258 | 258 | |
| 259 | - if(is_string($selectors)){ |
|
| 259 | + if (is_string($selectors)) { |
|
| 260 | 260 | $selectors = [trim($selectors)]; |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - if(!is_array($selectors) || empty($selectors)){ |
|
| 263 | + if (!is_array($selectors) || empty($selectors)) { |
|
| 264 | 264 | $selectors = ['*']; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | $elements = new NodeList; |
| 268 | 268 | |
| 269 | - foreach($selectors as $selector){ |
|
| 269 | + foreach ($selectors as $selector) { |
|
| 270 | 270 | |
| 271 | - foreach($this->querySelectorAll($selector, $contextNode, $axis) as $element){ |
|
| 271 | + foreach ($this->querySelectorAll($selector, $contextNode, $axis) as $element) { |
|
| 272 | 272 | |
| 273 | - if($element->nodeType === $nodeType){ |
|
| 273 | + if ($element->nodeType === $nodeType) { |
|
| 274 | 274 | $elements[] = $element; |
| 275 | 275 | } |
| 276 | 276 | |
@@ -294,15 +294,15 @@ discard block |
||
| 294 | 294 | public function recursivelyCollect(PrototypeNode $element, string $property, int $maxLength = -1, int $nodeType = XML_ELEMENT_NODE):NodeList{ |
| 295 | 295 | $nodes = new NodeList; |
| 296 | 296 | |
| 297 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
| 297 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
| 298 | 298 | |
| 299 | - while($element = $element->{$property}){ |
|
| 299 | + while ($element = $element->{$property}) { |
|
| 300 | 300 | |
| 301 | - if($element->nodeType === $nodeType){ |
|
| 301 | + if ($element->nodeType === $nodeType) { |
|
| 302 | 302 | $nodes[] = $element; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - if(count($nodes) === $maxLength){ |
|
| 305 | + if (count($nodes) === $maxLength) { |
|
| 306 | 306 | break; |
| 307 | 307 | } |
| 308 | 308 | |
@@ -322,14 +322,14 @@ discard block |
||
| 322 | 322 | * |
| 323 | 323 | * @return \chillerlan\PrototypeDOM\Node\PrototypeElement|null |
| 324 | 324 | */ |
| 325 | - public function _recursivelyFind(PrototypeNode $element, string $property, string $selector = null, int $index = 0, int $nodeType = XML_ELEMENT_NODE){ |
|
| 325 | + public function _recursivelyFind(PrototypeNode $element, string $property, string $selector = null, int $index = 0, int $nodeType = XML_ELEMENT_NODE) { |
|
| 326 | 326 | |
| 327 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
| 327 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
| 328 | 328 | |
| 329 | 329 | /** @var \chillerlan\PrototypeDOM\Node\Element $element */ |
| 330 | - while($element = $element->{$property}){ |
|
| 330 | + while ($element = $element->{$property}) { |
|
| 331 | 331 | |
| 332 | - if($element->nodeType !== $nodeType || !is_null($selector) && !$element->match($selector) || --$index >= 0){ |
|
| 332 | + if ($element->nodeType !== $nodeType || !is_null($selector) && !$element->match($selector) || --$index >= 0) { |
|
| 333 | 333 | continue; |
| 334 | 334 | } |
| 335 | 335 | |
@@ -351,9 +351,9 @@ discard block |
||
| 351 | 351 | */ |
| 352 | 352 | public function match(PrototypeNode $element, string $selector):bool{ |
| 353 | 353 | |
| 354 | - foreach($this->select($selector) as $match){ |
|
| 354 | + foreach ($this->select($selector) as $match) { |
|
| 355 | 355 | |
| 356 | - if($element->isSameNode($match)){ |
|
| 356 | + if ($element->isSameNode($match)) { |
|
| 357 | 357 | return true; |
| 358 | 358 | } |
| 359 | 359 | |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | /** @var \chillerlan\PrototypeDOM\Node\Element $element */ |
| 375 | 375 | $element = $this->createElement($tag); |
| 376 | 376 | |
| 377 | - if(!is_null($attributes)){ |
|
| 377 | + if (!is_null($attributes)) { |
|
| 378 | 378 | $element->setAttributes($attributes); |
| 379 | 379 | } |
| 380 | 380 | |
@@ -68,8 +68,7 @@ |
||
| 68 | 68 | |
| 69 | 69 | if($currentTitle instanceof Element){ |
| 70 | 70 | $currentTitle->update($title); |
| 71 | - } |
|
| 72 | - else{ |
|
| 71 | + } else{ |
|
| 73 | 72 | $head = $this->select('head')->item(0); |
| 74 | 73 | $currentTitle = $this->newElement('title')->update($title); |
| 75 | 74 | |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | * @property string $src |
| 22 | 22 | * @property string $innerHTML |
| 23 | 23 | */ |
| 24 | -trait HTMLElementTrait{ |
|
| 24 | +trait HTMLElementTrait { |
|
| 25 | 25 | use Magic, ElementTrait; |
| 26 | 26 | |
| 27 | 27 | protected function magic_get_id():string { |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | public function identify(string $newID = null):string { |
| 71 | 71 | $oldID = $this->id; |
| 72 | 72 | |
| 73 | - if(!is_null($newID)){ |
|
| 73 | + if (!is_null($newID)) { |
|
| 74 | 74 | $this->id = $newID; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -85,14 +85,14 @@ discard block |
||
| 85 | 85 | public function classNames():array{ |
| 86 | 86 | $currentClassnames = []; |
| 87 | 87 | |
| 88 | - if($this->hasAttributes()){ |
|
| 88 | + if ($this->hasAttributes()) { |
|
| 89 | 89 | $classnames = explode(' ', $this->class); |
| 90 | 90 | |
| 91 | - if(!empty($classnames)){ |
|
| 91 | + if (!empty($classnames)) { |
|
| 92 | 92 | |
| 93 | - foreach($classnames as $classname){ |
|
| 93 | + foreach ($classnames as $classname) { |
|
| 94 | 94 | |
| 95 | - if(empty($classname)){ |
|
| 95 | + if (empty($classname)) { |
|
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | |
@@ -160,10 +160,10 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @return null|string |
| 162 | 162 | */ |
| 163 | - public function getStyle(string $property){ |
|
| 163 | + public function getStyle(string $property) { |
|
| 164 | 164 | $currentStyle = $this->getStyles(); |
| 165 | 165 | |
| 166 | - if(array_key_exists(strtolower($property), $currentStyle)){ |
|
| 166 | + if (array_key_exists(strtolower($property), $currentStyle)) { |
|
| 167 | 167 | return $currentStyle[$property]; |
| 168 | 168 | } |
| 169 | 169 | |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | public function setStyle(array $style, bool $replace = false):PrototypeHTMLElement{ |
| 182 | 182 | $currentStyle = $this->getStyles(); |
| 183 | 183 | |
| 184 | - if(!$replace){ |
|
| 184 | + if (!$replace) { |
|
| 185 | 185 | $style = array_merge($currentStyle, $style); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - foreach($style as $property => $value){ |
|
| 188 | + foreach ($style as $property => $value) { |
|
| 189 | 189 | $style[$property] = $property.': '.$value.';'; |
| 190 | 190 | } |
| 191 | 191 | |