@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | /** |
| 27 | 27 | * |
| 28 | 28 | */ |
| 29 | -class Document extends DOMDocument{ |
|
| 29 | +class Document extends DOMDocument { |
|
| 30 | 30 | |
| 31 | 31 | protected const NODE_CLASSES = [ |
| 32 | 32 | 'DOMAttr' => Attr::class, |
@@ -56,14 +56,14 @@ discard block |
||
| 56 | 56 | * @param string|null $version |
| 57 | 57 | * @param string|null $encoding |
| 58 | 58 | */ |
| 59 | - public function __construct($content = null, bool $xml = null, string $version = null, string $encoding = null){ |
|
| 59 | + public function __construct($content = null, bool $xml = null, string $version = null, string $encoding = null) { |
|
| 60 | 60 | parent::__construct($version ?? '1.0', $encoding ?? 'UTF-8'); |
| 61 | 61 | |
| 62 | - foreach($this::NODE_CLASSES as $baseClass => $extendedClass){ |
|
| 62 | + foreach ($this::NODE_CLASSES as $baseClass => $extendedClass) { |
|
| 63 | 63 | $this->registerNodeClass($baseClass, $extendedClass); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if($content !== null){ |
|
| 66 | + if ($content !== null) { |
|
| 67 | 67 | $this->loadDocument($content, $xml); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | /** |
| 78 | 78 | * @return string|null |
| 79 | 79 | */ |
| 80 | - public function getTitle():?string{ |
|
| 80 | + public function getTitle(): ?string{ |
|
| 81 | 81 | return $this->select(['head > title'])->item(0)->nodeValue ?? null; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | public function setTitle(string $title):void{ |
| 88 | 88 | $currentTitle = $this->select(['head > title'])->item(0); |
| 89 | 89 | |
| 90 | - if($currentTitle instanceof Element){ |
|
| 90 | + if ($currentTitle instanceof Element) { |
|
| 91 | 91 | $currentTitle->update($title); |
| 92 | 92 | |
| 93 | 93 | return; |
@@ -96,10 +96,10 @@ discard block |
||
| 96 | 96 | $head = $this->select(['head'])->item(0); |
| 97 | 97 | $currentTitle = $this->newElement('title')->update($title); |
| 98 | 98 | |
| 99 | - if(!$head){ |
|
| 99 | + if (!$head) { |
|
| 100 | 100 | $html = $this->select(['html'])->first(); |
| 101 | 101 | |
| 102 | - if(!$html instanceof PrototypeHTMLElement){ |
|
| 102 | + if (!$html instanceof PrototypeHTMLElement) { |
|
| 103 | 103 | throw new DOMException('html header missing'); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -119,19 +119,19 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public function loadDocument($content, bool $xml = null):Document{ |
| 121 | 121 | |
| 122 | - if($content instanceof NodeList){ |
|
| 122 | + if ($content instanceof NodeList) { |
|
| 123 | 123 | return $this->insertNodeList($content); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if($content instanceof DOMNodeList){ |
|
| 126 | + if ($content instanceof DOMNodeList) { |
|
| 127 | 127 | return $this->insertNodeList(new NodeList($content)); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - if(!is_string($content)){ |
|
| 130 | + if (!is_string($content)) { |
|
| 131 | 131 | throw new DOMException('invalid document content'); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if(is_file($content) && is_readable($content)){ |
|
| 134 | + if (is_file($content) && is_readable($content)) { |
|
| 135 | 135 | return $this->loadDocumentFile($content, $xml); |
| 136 | 136 | } |
| 137 | 137 | |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | ? $this->load($file, $options) |
| 149 | 149 | : $this->loadHTMLFile($file, $options); |
| 150 | 150 | |
| 151 | - if($result === false){ |
|
| 151 | + if ($result === false) { |
|
| 152 | 152 | throw new DOMException('failed to load document from file: '.$file); // @codeCoverageIgnore |
| 153 | 153 | } |
| 154 | 154 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | ? $this->loadXML($documentSource, $options) |
| 166 | 166 | : $this->loadHTML($documentSource, $options); |
| 167 | 167 | |
| 168 | - if($result === false){ |
|
| 168 | + if ($result === false) { |
|
| 169 | 169 | throw new DOMException('failed to load document from string'); // @codeCoverageIgnore |
| 170 | 170 | } |
| 171 | 171 | |
@@ -177,19 +177,19 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | public function toNodeList($content):NodeList{ |
| 179 | 179 | |
| 180 | - if($content instanceof NodeList){ |
|
| 180 | + if ($content instanceof NodeList) { |
|
| 181 | 181 | return $content; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - if($content instanceof DOMNode || $content instanceof PrototypeNode){ |
|
| 184 | + if ($content instanceof DOMNode || $content instanceof PrototypeNode) { |
|
| 185 | 185 | return new NodeList([$content]); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - if($content instanceof DOMNodeList || is_iterable($content)){ |
|
| 188 | + if ($content instanceof DOMNodeList || is_iterable($content)) { |
|
| 189 | 189 | return new NodeList($content); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if(is_string($content)){ |
|
| 192 | + if (is_string($content)) { |
|
| 193 | 193 | $document = new self; |
| 194 | 194 | $document->loadHTML('<html lang="en"><body id="-import-content">'.$content.'</body></html>'); |
| 195 | 195 | |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | /** |
| 214 | 214 | * |
| 215 | 215 | */ |
| 216 | - public function query(string $xpath, DOMNode $contextNode = null):?NodeList{ |
|
| 216 | + public function query(string $xpath, DOMNode $contextNode = null): ?NodeList{ |
|
| 217 | 217 | $q = (new DOMXPath($this))->query($xpath, $contextNode); |
| 218 | 218 | |
| 219 | 219 | return $q !== false ? new NodeList($q) : null; |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | /** |
| 223 | 223 | * |
| 224 | 224 | */ |
| 225 | - public function querySelectorAll(string $selector, DOMNode $contextNode = null, string $axis = null):?NodeList{ |
|
| 225 | + public function querySelectorAll(string $selector, DOMNode $contextNode = null, string $axis = null): ?NodeList{ |
|
| 226 | 226 | return $this->query($this->cssSelectorConverter->toXPath($selector, $axis ?? 'descendant-or-self::'), $contextNode); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -232,9 +232,9 @@ discard block |
||
| 232 | 232 | public function removeElementsBySelector(array $selectors, DOMNode $contextNode = null, string $axis = null):Document{ |
| 233 | 233 | $nodes = $this->select($selectors, $contextNode, $axis ?? 'descendant-or-self::'); |
| 234 | 234 | |
| 235 | - if(count($nodes) > 0){ |
|
| 235 | + if (count($nodes) > 0) { |
|
| 236 | 236 | |
| 237 | - foreach($nodes as $node){ |
|
| 237 | + foreach ($nodes as $node) { |
|
| 238 | 238 | $node->removeNode(); |
| 239 | 239 | } |
| 240 | 240 | |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | */ |
| 249 | 249 | public function insertNodeList(NodeList $nodeList):Document{ |
| 250 | 250 | |
| 251 | - foreach($nodeList as $node){ |
|
| 251 | + foreach ($nodeList as $node) { |
|
| 252 | 252 | $this->appendChild($this->importNode($node->cloneNode(true), true)); |
| 253 | 253 | } |
| 254 | 254 | |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | */ |
| 272 | 272 | public function inspect(DOMNode $context = null, bool $xml = null):string{ |
| 273 | 273 | |
| 274 | - if($xml === true){ |
|
| 274 | + if ($xml === true) { |
|
| 275 | 275 | return $this->saveXML($context); |
| 276 | 276 | } |
| 277 | 277 | |
@@ -286,15 +286,15 @@ discard block |
||
| 286 | 286 | $nodeType = $nodeType ?? XML_ELEMENT_NODE; |
| 287 | 287 | $elements = new NodeList; |
| 288 | 288 | |
| 289 | - foreach($selectors ?? ['*'] as $selector){ |
|
| 289 | + foreach ($selectors ?? ['*'] as $selector) { |
|
| 290 | 290 | |
| 291 | - if(!is_string($selector)){ |
|
| 291 | + if (!is_string($selector)) { |
|
| 292 | 292 | continue; |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - foreach($this->querySelectorAll($selector, $contextNode, $axis ?? 'descendant-or-self::') as $element){ |
|
| 295 | + foreach ($this->querySelectorAll($selector, $contextNode, $axis ?? 'descendant-or-self::') as $element) { |
|
| 296 | 296 | |
| 297 | - if($element->nodeType === $nodeType){ |
|
| 297 | + if ($element->nodeType === $nodeType) { |
|
| 298 | 298 | $elements[] = $element; |
| 299 | 299 | } |
| 300 | 300 | |
@@ -314,15 +314,15 @@ discard block |
||
| 314 | 314 | $maxLength = $maxLength ?? -1; |
| 315 | 315 | $nodes = new NodeList; |
| 316 | 316 | |
| 317 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
| 317 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
| 318 | 318 | |
| 319 | - while($element = $element->{$property}){ |
|
| 319 | + while ($element = $element->{$property}) { |
|
| 320 | 320 | |
| 321 | - if($element->nodeType === $nodeType){ |
|
| 321 | + if ($element->nodeType === $nodeType) { |
|
| 322 | 322 | $nodes[] = $element; |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - if(count($nodes) === $maxLength){ |
|
| 325 | + if (count($nodes) === $maxLength) { |
|
| 326 | 326 | break; |
| 327 | 327 | } |
| 328 | 328 | |
@@ -336,15 +336,15 @@ discard block |
||
| 336 | 336 | /** |
| 337 | 337 | * @see https://secure.php.net/manual/dom.constants.php |
| 338 | 338 | */ |
| 339 | - public function recursivelyFind(PrototypeNode $element, string $property = null, string $selector = null, int $index = null, int $nodeType = null):?DOMNode{ |
|
| 339 | + public function recursivelyFind(PrototypeNode $element, string $property = null, string $selector = null, int $index = null, int $nodeType = null): ?DOMNode{ |
|
| 340 | 340 | $nodeType = $nodeType ?? XML_ELEMENT_NODE; |
| 341 | 341 | $index = $index ?? 0; |
| 342 | 342 | |
| 343 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
| 343 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
| 344 | 344 | |
| 345 | - while($element = $element->{$property}){ |
|
| 345 | + while ($element = $element->{$property}) { |
|
| 346 | 346 | |
| 347 | - if($element->nodeType !== $nodeType || $selector !== null && !$element->match($selector) || --$index >= 0){ |
|
| 347 | + if ($element->nodeType !== $nodeType || $selector !== null && !$element->match($selector) || --$index >= 0) { |
|
| 348 | 348 | continue; |
| 349 | 349 | } |
| 350 | 350 | |
@@ -361,9 +361,9 @@ discard block |
||
| 361 | 361 | */ |
| 362 | 362 | public function match(DOMNode $element, string $selector):bool{ |
| 363 | 363 | |
| 364 | - foreach($this->select([$selector]) as $match){ |
|
| 364 | + foreach ($this->select([$selector]) as $match) { |
|
| 365 | 365 | |
| 366 | - if($element->isSameNode($match)){ |
|
| 366 | + if ($element->isSameNode($match)) { |
|
| 367 | 367 | return true; |
| 368 | 368 | } |
| 369 | 369 | |
@@ -379,7 +379,7 @@ discard block |
||
| 379 | 379 | /** @var \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement $element */ |
| 380 | 380 | $element = $this->createElement($tag); |
| 381 | 381 | |
| 382 | - if($attributes !== null){ |
|
| 382 | + if ($attributes !== null) { |
|
| 383 | 383 | $element->setAttributes($attributes); |
| 384 | 384 | } |
| 385 | 385 | |
@@ -392,9 +392,9 @@ discard block |
||
| 392 | 392 | * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement|\DOMNode|null |
| 393 | 393 | * @throws \DOMException |
| 394 | 394 | */ |
| 395 | - public function getElementById($elementId):?DOMNode{ |
|
| 395 | + public function getElementById($elementId): ?DOMNode{ |
|
| 396 | 396 | |
| 397 | - if(!is_string($elementId)){ |
|
| 397 | + if (!is_string($elementId)) { |
|
| 398 | 398 | throw new DOMException('invalid element id'); |
| 399 | 399 | } |
| 400 | 400 | |