Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d9a748

Browse files
committed
[DomCrawler] always pass base href to subcrawlers
Make sure that all relevant information is passed to created crawlers. To avoid future regressions, this commit backports the approach taken by @stof in #15934 to have a single place in the class that is responsible to create subcrawler instances.
1 parent f80e6c6 commit 3d9a748

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ public function eq($position)
327327
{
328328
foreach ($this as $i => $node) {
329329
if ($i == $position) {
330-
return new static($node, $this->uri, $this->baseHref);
330+
return $this->createSubCrawler($node);
331331
}
332332
}
333333

334-
return new static(null, $this->uri, $this->baseHref);
334+
return $this->createSubCrawler(null);
335335
}
336336

337337
/**
@@ -354,7 +354,7 @@ public function each(\Closure $closure)
354354
{
355355
$data = array();
356356
foreach ($this as $i => $node) {
357-
$data[] = $closure(new static($node, $this->uri, $this->baseHref), $i);
357+
$data[] = $closure($this->createSubCrawler($node), $i);
358358
}
359359

360360
return $data;
@@ -370,7 +370,7 @@ public function each(\Closure $closure)
370370
*/
371371
public function slice($offset = 0, $length = -1)
372372
{
373-
return new static(iterator_to_array(new \LimitIterator($this, $offset, $length)), $this->uri);
373+
return $this->createSubCrawler(iterator_to_array(new \LimitIterator($this, $offset, $length)));
374374
}
375375

376376
/**
@@ -386,12 +386,12 @@ public function reduce(\Closure $closure)
386386
{
387387
$nodes = array();
388388
foreach ($this as $i => $node) {
389-
if (false !== $closure(new static($node, $this->uri, $this->baseHref), $i)) {
389+
if (false !== $closure($this->createSubCrawler($node), $i)) {
390390
$nodes[] = $node;
391391
}
392392
}
393393

394-
return new static($nodes, $this->uri, $this->baseHref);
394+
return $this->createSubCrawler($nodes);
395395
}
396396

397397
/**
@@ -427,7 +427,7 @@ public function siblings()
427427
throw new \InvalidArgumentException('The current node list is empty.');
428428
}
429429

430-
return new static($this->sibling($this->getNode(0)->parentNode->firstChild), $this->uri, $this->baseHref);
430+
return $this->createSubCrawler($this->sibling($this->getNode(0)->parentNode->firstChild));
431431
}
432432

433433
/**
@@ -443,7 +443,7 @@ public function nextAll()
443443
throw new \InvalidArgumentException('The current node list is empty.');
444444
}
445445

446-
return new static($this->sibling($this->getNode(0)), $this->uri, $this->baseHref);
446+
return $this->createSubCrawler($this->sibling($this->getNode(0)));
447447
}
448448

449449
/**
@@ -459,7 +459,7 @@ public function previousAll()
459459
throw new \InvalidArgumentException('The current node list is empty.');
460460
}
461461

462-
return new static($this->sibling($this->getNode(0), 'previousSibling'), $this->uri, $this->baseHref);
462+
return $this->createSubCrawler($this->sibling($this->getNode(0), 'previousSibling'));
463463
}
464464

465465
/**
@@ -484,7 +484,7 @@ public function parents()
484484
}
485485
}
486486

487-
return new static($nodes, $this->uri, $this->baseHref);
487+
return $this->createSubCrawler($nodes);
488488
}
489489

490490
/**
@@ -502,7 +502,7 @@ public function children()
502502

503503
$node = $this->getNode(0)->firstChild;
504504

505-
return new static($node ? $this->sibling($node) : array(), $this->uri, $this->baseHref);
505+
return $this->createSubCrawler($node ? $this->sibling($node) : array());
506506
}
507507

508508
/**
@@ -631,7 +631,7 @@ public function filterXPath($xpath)
631631

632632
// If we dropped all expressions in the XPath while preparing it, there would be no match
633633
if ('' === $xpath) {
634-
return new static(null, $this->uri, $this->baseHref);
634+
return $this->createSubCrawler(null);
635635
}
636636

637637
return $this->filterRelativeXPath($xpath);
@@ -829,7 +829,7 @@ private function filterRelativeXPath($xpath)
829829
{
830830
$prefixes = $this->findNamespacePrefixes($xpath);
831831

832-
$crawler = new static(null, $this->uri, $this->baseHref);
832+
$crawler = $this->createSubCrawler(null);
833833

834834
foreach ($this as $node) {
835835
$domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);
@@ -999,4 +999,18 @@ private function findNamespacePrefixes($xpath)
999999

10001000
return array();
10011001
}
1002+
1003+
/**
1004+
* Creates a crawler for some subnodes.
1005+
*
1006+
* @param \DOMElement|\DOMElement[]|\DOMNodeList|null $nodes
1007+
*
1008+
* @return static
1009+
*/
1010+
private function createSubCrawler($nodes)
1011+
{
1012+
$crawler = new static($nodes, $this->uri, $this->baseHref);
1013+
1014+
return $crawler;
1015+
}
10021016
}

0 commit comments

Comments
 (0)