Complex classes like HtmlPageCrawler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlPageCrawler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class HtmlPageCrawler extends Crawler |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * the (internal) root element name used when importing html fragments |
||
| 19 | * */ |
||
| 20 | const FRAGMENT_ROOT_TAGNAME = '_root'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get an HtmlPageCrawler object from a HTML string, DOMNode, DOMNodeList or HtmlPageCrawler |
||
| 24 | * |
||
| 25 | * This is the equivalent to jQuery's $() function when used for wrapping DOMNodes or creating DOMElements from HTML code. |
||
| 26 | * |
||
| 27 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array $content |
||
| 28 | * @return HtmlPageCrawler |
||
| 29 | * @api |
||
| 30 | */ |
||
| 31 | 11 | public static function create($content) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Adds the specified class(es) to each element in the set of matched elements. |
||
| 42 | * |
||
| 43 | * @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
||
| 44 | * @return HtmlPageCrawler $this for chaining |
||
| 45 | * @api |
||
| 46 | */ |
||
| 47 | 1 | public function addClass($name) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Insert content, specified by the parameter, after each element in the set of matched elements. |
||
| 71 | * |
||
| 72 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 73 | * @return HtmlPageCrawler $this for chaining |
||
| 74 | * @api |
||
| 75 | */ |
||
| 76 | 3 | public function after($content) |
|
| 77 | { |
||
| 78 | 3 | $content = self::create($content); |
|
| 79 | 3 | $newnodes = array(); |
|
| 80 | 3 | foreach ($this as $i => $node) { |
|
| 81 | /** @var \DOMNode $node */ |
||
| 82 | 3 | $refnode = $node->nextSibling; |
|
| 83 | 3 | foreach ($content as $newnode) { |
|
| 84 | /** @var \DOMNode $newnode */ |
||
| 85 | 3 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 86 | 3 | if ($refnode === null) { |
|
| 87 | 3 | $node->parentNode->appendChild($newnode); |
|
| 88 | 3 | } else { |
|
| 89 | $node->parentNode->insertBefore($newnode, $refnode); |
||
| 90 | } |
||
| 91 | 3 | $newnodes[] = $newnode; |
|
| 92 | 3 | } |
|
| 93 | 3 | } |
|
| 94 | 3 | $content->clear(); |
|
| 95 | 3 | $content->add($newnodes); |
|
| 96 | 3 | return $this; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Insert HTML content as child nodes of each element after existing children |
||
| 101 | * |
||
| 102 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
||
| 103 | * @return HtmlPageCrawler $this for chaining |
||
| 104 | * @api |
||
| 105 | */ |
||
| 106 | 2 | public function append($content) |
|
| 107 | { |
||
| 108 | 2 | $content = self::create($content); |
|
| 109 | 2 | $newnodes = array(); |
|
| 110 | 2 | foreach ($this as $i => $node) { |
|
| 111 | /** @var \DOMNode $node */ |
||
| 112 | 2 | foreach ($content as $newnode) { |
|
| 113 | /** @var \DOMNode $newnode */ |
||
| 114 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 115 | // if ($newnode->ownerDocument !== $node->ownerDocument) { |
||
| 116 | // $newnode = $node->ownerDocument->importNode($newnode, true); |
||
| 117 | // } else { |
||
| 118 | // if ($i > 0) { |
||
| 119 | // $newnode = $newnode->cloneNode(true); |
||
| 120 | // } |
||
| 121 | // } |
||
| 122 | 2 | $node->appendChild($newnode); |
|
| 123 | 2 | $newnodes[] = $newnode; |
|
| 124 | 2 | } |
|
| 125 | 2 | } |
|
| 126 | 2 | $content->clear(); |
|
| 127 | 2 | $content->add($newnodes); |
|
| 128 | 2 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Insert every element in the set of matched elements to the end of the target. |
||
| 133 | * |
||
| 134 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 135 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 136 | * @api |
||
| 137 | */ |
||
| 138 | 1 | public function appendTo($element) |
|
| 139 | { |
||
| 140 | 1 | $e = self::create($element); |
|
| 141 | 1 | $newnodes = array(); |
|
| 142 | 1 | foreach ($e as $i => $node) { |
|
| 143 | /** @var \DOMNode $node */ |
||
| 144 | 1 | foreach ($this as $newnode) { |
|
| 145 | /** @var \DOMNode $newnode */ |
||
| 146 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 147 | 1 | $node->appendChild($newnode); |
|
| 148 | 1 | $newnodes[] = $newnode; |
|
| 149 | 1 | } |
|
| 150 | 1 | } |
|
| 151 | 1 | return self::create($newnodes); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the attribute value of the first node of the list, or sets an attribute on each element |
||
| 156 | * |
||
| 157 | * @see HtmlPageCrawler::getAttribute() |
||
| 158 | * @see HtmlPageCrawler::setAttribute |
||
| 159 | * |
||
| 160 | * @param string $name |
||
| 161 | * @param null|string $value |
||
| 162 | * @return null|string|HtmlPageCrawler |
||
| 163 | * @api |
||
| 164 | */ |
||
| 165 | 1 | public function attr($name, $value = null) |
|
| 166 | { |
||
| 167 | 1 | if ($value === null) { |
|
| 168 | 1 | return $this->getAttribute($name); |
|
| 169 | } else { |
||
| 170 | 1 | return $this->setAttribute($name, $value); |
|
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets an attribute on each element |
||
| 176 | * |
||
| 177 | * @param string $name |
||
| 178 | * @param string $value |
||
| 179 | * @return HtmlPageCrawler $this for chaining |
||
| 180 | */ |
||
| 181 | 2 | public function setAttribute($name, $value) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the attribute value of the first node of the list. |
||
| 194 | * |
||
| 195 | * @param string $name The attribute name |
||
| 196 | * @return string|null The attribute value or null if the attribute does not exist |
||
| 197 | * @throws \InvalidArgumentException When current node is empty |
||
| 198 | * |
||
| 199 | */ |
||
| 200 | 1 | public function getAttribute($name) |
|
| 201 | { |
||
| 202 | 1 | if (!count($this)) { |
|
| 203 | throw new \InvalidArgumentException('The current node list is empty.'); |
||
| 204 | } |
||
| 205 | 1 | $node = $this->getNode(0); |
|
| 206 | 1 | return $node->hasAttribute($name) ? $node->getAttribute($name) : null; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
| 211 | * |
||
| 212 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 213 | * @return HtmlPageCrawler $this for chaining |
||
| 214 | * @api |
||
| 215 | */ |
||
| 216 | 2 | public function before($content) |
|
| 217 | { |
||
| 218 | 2 | $content = self::create($content); |
|
| 219 | 2 | $newnodes = array(); |
|
| 220 | 2 | foreach ($this as $i => $node) { |
|
| 221 | /** @var \DOMNode $node */ |
||
| 222 | 2 | foreach ($content as $newnode) { |
|
| 223 | /** @var \DOMNode $newnode */ |
||
| 224 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 225 | 2 | $node->parentNode->insertBefore($newnode, $node); |
|
| 226 | 2 | $newnodes[] = $newnode; |
|
| 227 | 2 | } |
|
| 228 | 2 | } |
|
| 229 | 2 | $content->clear(); |
|
| 230 | 2 | $content->add($newnodes); |
|
| 231 | 2 | return $this; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Create a deep copy of the set of matched elements. |
||
| 236 | * |
||
| 237 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
| 238 | * |
||
| 239 | * @return HtmlPageCrawler |
||
| 240 | * @api |
||
| 241 | */ |
||
| 242 | 1 | public function makeClone() |
|
| 246 | |||
| 247 | 1 | public function __clone() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get one CSS style property of the first element or set it for all elements in the list |
||
| 260 | * |
||
| 261 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
| 262 | * |
||
| 263 | * @see HtmlPageCrawler::getStyle() |
||
| 264 | * @see HtmlPageCrawler::setStyle() |
||
| 265 | * |
||
| 266 | * @param string $key The name of the style property |
||
| 267 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
| 268 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
| 269 | * @api |
||
| 270 | */ |
||
| 271 | 1 | public function css($key, $value = null) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * get one CSS style property of the first element |
||
| 282 | * |
||
| 283 | * @param string $key name of the property |
||
| 284 | * @return string|null value of the property |
||
| 285 | */ |
||
| 286 | 1 | public function getStyle($key) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * set one CSS style property for all elements in the list |
||
| 294 | * |
||
| 295 | * @param string $key name of the property |
||
| 296 | * @param string $value value of the property |
||
| 297 | * @return HtmlPageCrawler $this for chaining |
||
| 298 | */ |
||
| 299 | 1 | public function setStyle($key, $value) |
|
| 300 | { |
||
| 301 | 1 | foreach ($this as $node) { |
|
| 302 | 1 | if ($node instanceof \DOMElement) { |
|
| 303 | /** @var \DOMElement $node */ |
||
| 304 | 1 | $styles = Helpers::cssStringToArray($node->getAttribute('style')); |
|
| 305 | 1 | if ($value != '') { |
|
| 306 | 1 | $styles[$key] = $value; |
|
| 307 | 1 | } elseif (isset($styles[$key])) { |
|
| 308 | 1 | unset($styles[$key]); |
|
| 309 | 1 | } |
|
| 310 | 1 | $node->setAttribute('style', Helpers::cssArrayToString($styles)); |
|
| 311 | 1 | } |
|
| 312 | 1 | } |
|
| 313 | 1 | return $this; |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Removes all child nodes and text from all nodes in set |
||
| 318 | * |
||
| 319 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
| 320 | * @return HtmlPageCrawler $this |
||
| 321 | * @api |
||
| 322 | */ |
||
| 323 | public function makeEmpty() |
||
| 324 | { |
||
| 325 | foreach ($this as $node) { |
||
| 326 | $node->nodeValue = ''; |
||
| 327 | } |
||
| 328 | return $this; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Determine whether any of the matched elements are assigned the given class. |
||
| 333 | * |
||
| 334 | * @param string $name |
||
| 335 | * @return bool |
||
| 336 | * @api |
||
| 337 | */ |
||
| 338 | 2 | public function hasClass($name) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get the HTML contents of the first element in the set of matched elements |
||
| 353 | * or set the HTML contents of every matched element. |
||
| 354 | * |
||
| 355 | * Function is here for compatibility with jQuery: When called with a parameter, it is |
||
| 356 | * equivalent to setInnerHtml(), without parameter it is the same as getInnerHtml() |
||
| 357 | * |
||
| 358 | * @see HtmlPageCrawler::setInnerHtml() |
||
| 359 | * @see HtmlPageCrawler::getInnerHtml() |
||
| 360 | * |
||
| 361 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
||
| 362 | * |
||
| 363 | * @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
||
| 364 | * @api |
||
| 365 | */ |
||
| 366 | public function html($html = null) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the innerHTML contents of the first element |
||
| 378 | * |
||
| 379 | * @return string HTML code fragment |
||
| 380 | */ |
||
| 381 | 2 | public function getInnerHtml() |
|
| 382 | { |
||
| 383 | 2 | $html = ''; |
|
| 384 | 2 | foreach ($this->getNode(0)->childNodes as $node) { |
|
| 385 | 2 | $html .= $node->ownerDocument->saveHTML($node); |
|
| 386 | 2 | } |
|
| 387 | 2 | return $html; |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set the HTML contents of each element |
||
| 392 | * |
||
| 393 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 394 | * @return HtmlPageCrawler $this for chaining |
||
| 395 | */ |
||
| 396 | 1 | public function setInnerHtml($content) |
|
| 397 | { |
||
| 398 | 1 | $content = self::create($content); |
|
| 399 | 1 | foreach ($this as $node) { |
|
| 400 | 1 | $node->nodeValue = ''; |
|
| 401 | 1 | foreach ($content as $newnode) { |
|
| 402 | /** @var \DOMNode $node */ |
||
| 403 | /** @var \DOMNode $newnode */ |
||
| 404 | 1 | $newnode = static::importNewnode($newnode, $node); |
|
| 405 | 1 | $node->appendChild($newnode); |
|
| 406 | 1 | } |
|
| 407 | 1 | } |
|
| 408 | 1 | return $this; |
|
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Insert every element in the set of matched elements after the target. |
||
| 413 | * |
||
| 414 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 415 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 416 | * @api |
||
| 417 | */ |
||
| 418 | 1 | public function insertAfter($element) |
|
| 419 | { |
||
| 420 | 1 | $e = self::create($element); |
|
| 421 | 1 | $newnodes = array(); |
|
| 422 | 1 | foreach ($e as $i => $node) { |
|
| 423 | /** @var \DOMNode $node */ |
||
| 424 | 1 | $refnode = $node->nextSibling; |
|
| 425 | 1 | foreach ($this as $newnode) { |
|
| 426 | /** @var \DOMNode $newnode */ |
||
| 427 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 428 | 1 | if ($refnode === null) { |
|
| 429 | 1 | $node->parentNode->appendChild($newnode); |
|
| 430 | 1 | } else { |
|
| 431 | 1 | $node->parentNode->insertBefore($newnode, $refnode); |
|
| 432 | } |
||
| 433 | 1 | $newnodes[] = $newnode; |
|
| 434 | 1 | } |
|
| 435 | 1 | } |
|
| 436 | 1 | return self::create($newnodes); |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Insert every element in the set of matched elements before the target. |
||
| 441 | * |
||
| 442 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 443 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 444 | * @api |
||
| 445 | */ |
||
| 446 | 1 | public function insertBefore($element) |
|
| 447 | { |
||
| 448 | 1 | $e = self::create($element); |
|
| 449 | 1 | $newnodes = array(); |
|
| 450 | 1 | foreach ($e as $i => $node) { |
|
| 451 | /** @var \DOMNode $node */ |
||
| 452 | 1 | foreach ($this as $newnode) { |
|
| 453 | /** @var \DOMNode $newnode */ |
||
| 454 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 455 | 1 | $node->parentNode->insertBefore($newnode, $node); |
|
| 456 | 1 | $newnodes[] = $newnode; |
|
| 457 | 1 | } |
|
| 458 | 1 | } |
|
| 459 | 1 | return self::create($newnodes); |
|
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
| 464 | * |
||
| 465 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 466 | * @return HtmlPageCrawler $this for chaining |
||
| 467 | * @api |
||
| 468 | */ |
||
| 469 | 1 | public function prepend($content) |
|
| 470 | { |
||
| 471 | 1 | $content = self::create($content); |
|
| 472 | 1 | $newnodes = array(); |
|
| 473 | 1 | foreach ($this as $i => $node) { |
|
| 474 | 1 | $refnode = $node->firstChild; |
|
| 475 | /** @var \DOMNode $node */ |
||
| 476 | 1 | foreach ($content as $newnode) { |
|
| 477 | /** @var \DOMNode $newnode */ |
||
| 478 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 479 | 1 | if ($refnode === null) { |
|
| 480 | $node->appendChild($newnode); |
||
| 481 | } else { |
||
| 482 | 1 | $node->insertBefore($newnode, $refnode); |
|
| 483 | } |
||
| 484 | 1 | $newnodes[] = $newnode; |
|
| 485 | 1 | } |
|
| 486 | 1 | } |
|
| 487 | 1 | $content->clear(); |
|
| 488 | 1 | $content->add($newnodes); |
|
| 489 | 1 | return $this; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Insert every element in the set of matched elements to the beginning of the target. |
||
| 494 | * |
||
| 495 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 496 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
| 497 | * @api |
||
| 498 | */ |
||
| 499 | public function prependTo($element) |
||
| 500 | { |
||
| 501 | $e = self::create($element); |
||
| 502 | $newnodes = array(); |
||
| 503 | foreach ($e as $i => $node) { |
||
| 504 | $refnode = $node->firstChild; |
||
| 505 | /** @var \DOMNode $node */ |
||
| 506 | foreach ($this as $newnode) { |
||
| 507 | /** @var \DOMNode $newnode */ |
||
| 508 | $newnode = static::importNewnode($newnode, $node, $i); |
||
| 509 | if ($refnode === null) { |
||
| 510 | $node->appendChild($newnode); |
||
| 511 | } else { |
||
| 512 | $node->insertBefore($newnode, $refnode); |
||
| 513 | } |
||
| 514 | $newnodes[] = $newnode; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | return self::create($newnodes); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Remove the set of matched elements from the DOM. |
||
| 522 | * |
||
| 523 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
| 524 | * but leaves them in the DOM) |
||
| 525 | * |
||
| 526 | * @api |
||
| 527 | */ |
||
| 528 | 2 | public function remove() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Remove an attribute from each element in the set of matched elements. |
||
| 543 | * |
||
| 544 | * Alias for removeAttribute for compatibility with jQuery |
||
| 545 | * |
||
| 546 | * @param string $name |
||
| 547 | * @return HtmlPageCrawler |
||
| 548 | * @api |
||
| 549 | */ |
||
| 550 | 1 | public function removeAttr($name) |
|
| 551 | { |
||
| 552 | 1 | return $this->removeAttribute($name); |
|
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Remove an attribute from each element in the set of matched elements. |
||
| 557 | * |
||
| 558 | * @param string $name |
||
| 559 | * @return HtmlPageCrawler |
||
| 560 | */ |
||
| 561 | 1 | public function removeAttribute($name) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Remove a class from each element in the list |
||
| 576 | * |
||
| 577 | * @param string $name |
||
| 578 | * @return HtmlPageCrawler $this for chaining |
||
| 579 | * @api |
||
| 580 | */ |
||
| 581 | 2 | public function removeClass($name) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Replace each target element with the set of matched elements. |
||
| 601 | * |
||
| 602 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 603 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 604 | * @api |
||
| 605 | */ |
||
| 606 | 2 | public function replaceAll($element) |
|
| 607 | { |
||
| 608 | 2 | $e = self::create($element); |
|
| 609 | 2 | $newnodes = array(); |
|
| 610 | 2 | foreach ($e as $i => $node) { |
|
| 611 | /** @var \DOMNode $node */ |
||
| 612 | 2 | $parent = $node->parentNode; |
|
| 613 | 2 | $refnode = $node->nextSibling; |
|
| 614 | 2 | foreach ($this as $j => $newnode) { |
|
| 615 | /** @var \DOMNode $newnode */ |
||
| 616 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 617 | 2 | if ($j == 0) { |
|
| 618 | 2 | $parent->replaceChild($newnode, $node); |
|
| 619 | 2 | } else { |
|
| 620 | 1 | $parent->insertBefore($newnode, $refnode); |
|
| 621 | } |
||
| 622 | 2 | $newnodes[] = $newnode; |
|
| 623 | 2 | } |
|
| 624 | 2 | } |
|
| 625 | 2 | return self::create($newnodes); |
|
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
| 630 | * |
||
| 631 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 632 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 633 | * @api |
||
| 634 | */ |
||
| 635 | 2 | public function replaceWith($content) |
|
| 636 | { |
||
| 637 | 2 | $content = self::create($content); |
|
| 638 | 2 | $newnodes = array(); |
|
| 639 | 2 | foreach ($this as $i => $node) { |
|
| 640 | /** @var \DOMNode $node */ |
||
| 641 | 2 | $parent = $node->parentNode; |
|
| 642 | 2 | $refnode = $node->nextSibling; |
|
| 643 | 2 | foreach ($content as $j => $newnode) { |
|
| 644 | /** @var \DOMNode $newnode */ |
||
| 645 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 646 | 2 | if ($j == 0) { |
|
| 647 | 2 | $parent->replaceChild($newnode, $node); |
|
| 648 | 2 | } else { |
|
| 649 | 1 | $parent->insertBefore($newnode, $refnode); |
|
| 650 | } |
||
| 651 | 2 | $newnodes[] = $newnode; |
|
| 652 | 2 | } |
|
| 653 | 2 | } |
|
| 654 | 2 | $content->clear(); |
|
| 655 | 2 | $content->add($newnodes); |
|
| 656 | 2 | return $this; |
|
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get the combined text contents of each element in the set of matched elements, including their descendants, |
||
| 661 | * or set the text contents of the matched elements. |
||
| 662 | * |
||
| 663 | * ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
||
| 664 | * this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
||
| 665 | * must call ->first() before calling ->text(), e.g. |
||
| 666 | * |
||
| 667 | * in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
||
| 668 | * in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
||
| 669 | * |
||
| 670 | * @param null|string $text |
||
| 671 | * @return string|HtmlPageCrawler |
||
| 672 | * @api |
||
| 673 | */ |
||
| 674 | 1 | public function text($text = null) |
|
| 691 | |||
| 692 | |||
| 693 | /** |
||
| 694 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
| 695 | * |
||
| 696 | * @param string $classname One or more classnames separated by spaces |
||
| 697 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 698 | * @api |
||
| 699 | */ |
||
| 700 | 1 | public function toggleClass($classname) |
|
| 701 | { |
||
| 702 | 1 | $classes = explode(' ', $classname); |
|
| 703 | 1 | foreach ($this as $i => $node) { |
|
| 704 | 1 | $c = self::create($node); |
|
| 705 | /** @var \DOMNode $node */ |
||
| 706 | 1 | foreach ($classes as $class) { |
|
| 707 | 1 | if ($c->hasClass($class)) { |
|
| 708 | 1 | $c->removeClass($class); |
|
| 709 | 1 | } else { |
|
| 710 | 1 | $c->addClass($class); |
|
| 711 | } |
||
| 712 | 1 | } |
|
| 713 | 1 | } |
|
| 714 | 1 | return $this; |
|
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
| 719 | * |
||
| 720 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 721 | * @api |
||
| 722 | */ |
||
| 723 | 1 | public function unwrap() |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Remove the matched elements, but promote the children to take their place. |
||
| 736 | * |
||
| 737 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 738 | * @api |
||
| 739 | */ |
||
| 740 | 1 | public function unwrapInner() |
|
| 756 | |||
| 757 | |||
| 758 | /** |
||
| 759 | * Wrap an HTML structure around each element in the set of matched elements |
||
| 760 | * |
||
| 761 | * The HTML structure must contain only one root node, e.g.: |
||
| 762 | * Works: <div><div></div></div> |
||
| 763 | * Does not work: <div></div><div></div> |
||
| 764 | * |
||
| 765 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
| 766 | * @return HtmlPageCrawler $this for chaining |
||
| 767 | * @api |
||
| 768 | */ |
||
| 769 | 1 | public function wrap($wrappingElement) |
|
| 770 | { |
||
| 771 | 1 | $content = self::create($wrappingElement); |
|
| 772 | 1 | $newnodes = array(); |
|
| 773 | 1 | foreach ($this as $i => $node) { |
|
| 774 | /** @var \DOMNode $node */ |
||
| 775 | 1 | $newnode = $content->getNode(0); |
|
| 776 | /** @var \DOMNode $newnode */ |
||
| 777 | // $newnode = static::importNewnode($newnode, $node, $i); |
||
| 778 | 1 | if ($newnode->ownerDocument !== $node->ownerDocument) { |
|
| 779 | 1 | $newnode = $node->ownerDocument->importNode($newnode, true); |
|
| 780 | 1 | } else { |
|
| 781 | if ($i > 0) { |
||
| 782 | $newnode = $newnode->cloneNode(true); |
||
| 783 | } |
||
| 784 | } |
||
| 785 | 1 | $oldnode = $node->parentNode->replaceChild($newnode, $node); |
|
| 786 | 1 | while ($newnode->hasChildNodes()) { |
|
| 787 | 1 | $elementFound = false; |
|
| 788 | 1 | foreach ($newnode->childNodes as $child) { |
|
| 789 | 1 | if ($child instanceof \DOMElement) { |
|
| 790 | 1 | $newnode = $child; |
|
| 791 | 1 | $elementFound = true; |
|
| 792 | 1 | break; |
|
| 793 | } |
||
| 794 | 1 | } |
|
| 795 | 1 | if (!$elementFound) { |
|
| 796 | break; |
||
| 797 | } |
||
| 798 | 1 | } |
|
| 799 | 1 | $newnode->appendChild($oldnode); |
|
| 800 | 1 | $newnodes[] = $newnode; |
|
| 801 | 1 | } |
|
| 802 | 1 | $content->clear(); |
|
| 803 | 1 | $content->add($newnodes); |
|
| 804 | 1 | return $this; |
|
| 805 | } |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
| 809 | * |
||
| 810 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 811 | * @throws \LogicException |
||
| 812 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 813 | * @api |
||
| 814 | */ |
||
| 815 | 1 | public function wrapAll($content) |
|
| 816 | { |
||
| 817 | 1 | $content = self::create($content); |
|
| 818 | 1 | $parent = $this->getNode(0)->parentNode; |
|
| 819 | 1 | foreach ($this as $i => $node) { |
|
| 820 | /** @var \DOMNode $node */ |
||
| 821 | 1 | if ($node->parentNode !== $parent) { |
|
| 822 | throw new \LogicException('Nodes to be wrapped with wrapAll() must all have the same parent'); |
||
| 823 | } |
||
| 824 | 1 | } |
|
| 825 | |||
| 826 | 1 | $newnode = $content->getNode(0); |
|
| 827 | /** @var \DOMNode $newnode */ |
||
| 828 | 1 | $newnode = static::importNewnode($newnode, $parent); |
|
| 829 | |||
| 830 | 1 | $newnode = $parent->insertBefore($newnode,$this->getNode(0)); |
|
| 831 | 1 | $content->clear(); |
|
| 832 | 1 | $content->add($newnode); |
|
| 833 | |||
| 834 | 1 | while ($newnode->hasChildNodes()) { |
|
| 835 | $elementFound = false; |
||
| 836 | foreach ($newnode->childNodes as $child) { |
||
| 837 | if ($child instanceof \DOMElement) { |
||
| 838 | $newnode = $child; |
||
| 839 | $elementFound = true; |
||
| 840 | break; |
||
| 841 | } |
||
| 842 | } |
||
| 843 | if (!$elementFound) { |
||
| 844 | break; |
||
| 845 | } |
||
| 846 | } |
||
| 847 | 1 | foreach ($this as $i => $node) { |
|
| 848 | /** @var \DOMNode $node */ |
||
| 849 | 1 | $newnode->appendChild($node); |
|
| 850 | 1 | } |
|
| 851 | 1 | return $this; |
|
| 852 | } |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
| 856 | * |
||
| 857 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 858 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 859 | * @api |
||
| 860 | */ |
||
| 861 | 1 | public function wrapInner($content) |
|
| 869 | |||
| 870 | /** |
||
| 871 | * Get the HTML code fragment of all elements and their contents. |
||
| 872 | * |
||
| 873 | * If the first node contains a complete HTML document return the |
||
| 874 | * DocType if exists |
||
| 875 | * |
||
| 876 | * @return string HTML code (fragment) |
||
| 877 | * @api |
||
| 878 | */ |
||
| 879 | 3 | public function saveHTML() |
|
| 892 | |||
| 893 | public function __toString() |
||
| 897 | |||
| 898 | /** |
||
| 899 | * checks whether the first node contains a complete html document |
||
| 900 | * (as opposed to a document fragment) |
||
| 901 | * |
||
| 902 | * @return boolean |
||
| 903 | */ |
||
| 904 | 3 | public function isHtmlDocument() |
|
| 917 | |||
| 918 | /** |
||
| 919 | * get ownerDocument of the first element |
||
| 920 | * |
||
| 921 | * @return \DOMDocument|null |
||
| 922 | */ |
||
| 923 | public function getDOMDocument() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Filters the list of nodes with a CSS selector. |
||
| 937 | * |
||
| 938 | * @param string $selector |
||
| 939 | * @return HtmlPageCrawler |
||
| 940 | */ |
||
| 941 | 6 | public function filter($selector) |
|
| 945 | |||
| 946 | /** |
||
| 947 | * Filters the list of nodes with an XPath expression. |
||
| 948 | * |
||
| 949 | * @param string $xpath An XPath expression |
||
| 950 | * |
||
| 951 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
| 952 | * |
||
| 953 | * @api |
||
| 954 | */ |
||
| 955 | 1 | public function filterXPath($xpath) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
| 962 | * |
||
| 963 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
| 964 | * |
||
| 965 | * |
||
| 966 | * @param string $content A string to parse as HTML/XML |
||
| 967 | * @param null|string $type The content type of the string |
||
| 968 | * |
||
| 969 | * @return null|void |
||
| 970 | */ |
||
| 971 | 12 | public function addContent($content, $type = null) |
|
| 983 | |||
| 984 | 10 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
| 996 | |||
| 997 | /** |
||
| 998 | * returns the first node |
||
| 999 | * deprecated, use getNode(0) instead |
||
| 1000 | * |
||
| 1001 | * @return \DOMNode|null |
||
| 1002 | * @deprecated |
||
| 1003 | * @see Crawler::getNode |
||
| 1004 | */ |
||
| 1005 | public function getFirstNode() |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * @param int $position |
||
| 1012 | * |
||
| 1013 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
| 1014 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1015 | * |
||
| 1016 | * @return \DOMElement|null |
||
| 1017 | */ |
||
| 1018 | 5 | public function getNode($position) |
|
| 1022 | |||
| 1023 | /** |
||
| 1024 | * |
||
| 1025 | * get all nodes in Crawler in Array format, so that we can use |
||
| 1026 | * foreach to loop through the elements |
||
| 1027 | * |
||
| 1028 | * @return Array HtmlPageCrawler |
||
| 1029 | */ |
||
| 1030 | public function getNodes() |
||
| 1031 | { |
||
| 1032 | return $this->each(function($node) { |
||
| 1033 | return $node; |
||
| 1034 | }); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Returns the node name of the first node of the list. |
||
| 1039 | * |
||
| 1040 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
| 1041 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
| 1042 | * with Symfony < 2.6 |
||
| 1043 | * |
||
| 1044 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1045 | * |
||
| 1046 | * @return string The node name |
||
| 1047 | * |
||
| 1048 | * @throws \InvalidArgumentException When current node is empty |
||
| 1049 | */ |
||
| 1050 | 1 | public function nodeName() |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Adds a node to the current list of nodes. |
||
| 1060 | * |
||
| 1061 | * This method uses the appropriate specialized add*() method based |
||
| 1062 | * on the type of the argument. |
||
| 1063 | * |
||
| 1064 | * Overwritten from parent to allow Crawler to be added |
||
| 1065 | * |
||
| 1066 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
| 1067 | * |
||
| 1068 | * @api |
||
| 1069 | */ |
||
| 1070 | 14 | public function add($node) |
|
| 1080 | |||
| 1081 | /** |
||
| 1082 | * @param \DOMNode $newnode |
||
| 1083 | * @param \DOMNode $referencenode |
||
| 1084 | * @param int $clone |
||
| 1085 | * @return \DOMNode |
||
| 1086 | */ |
||
| 1087 | 5 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
| 1100 | * |
||
| 1101 | * @return bool |
||
| 1102 | */ |
||
| 1103 | 1 | public function isDisconnected() |
|
| 1108 | |||
| 1109 | 1 | public function __get($name) |
|
| 1118 | } |
||
| 1119 |
Really long classes often contain too much logic and violate the single responsibility principle.
We suggest to take a look at the “Code” section for options on how to refactor this code.