8000 [DomCrawler] always pass base href to subcrawlers by xabbuh · Pull Request #16028 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DomCrawler] always pass base href to subcrawlers #16028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[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.
  • Loading branch information
xabbuh committed Sep 30, 2015
commit 3d9a748978f304632362564fba21272c7e66c9d0
40 changes: 27 additions & 13 deletions src/Symfony/Component/DomCrawler/Crawler.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ public function eq($position)
{
foreach ($this as $i => $node) {
if ($i == $position) {
return new static($node, $this->uri, $this->baseHref);
return $this->createSubCrawler($node);
}
}

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

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

return $data;
Expand All @@ -370,7 +370,7 @@ public function each(\Closure $closure)
*/
public function slice($offset = 0, $length = -1)
{
return new static(iterator_to_array(new \LimitIterator($this, $offset, $length)), $this->uri);
return $this->createSubCrawler(iterator_to_array(new \LimitIterator($this, $offset, $length)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for reference, this is the place where the baseHref was missing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in #15934 I asked to backport this to the 2.3 branch, but realised that the slice() method was introduced later. That's why I did not backport your changes to Symfony 2.3.

}

/**
Expand All @@ -386,12 +386,12 @@ public function reduce(\Closure $closure)
{
$nodes = array();
foreach ($this as $i => $node) {
if (false !== $closure(new static($node, $this->uri, $this->baseHref), $i)) {
if (false !== $closure($this->createSubCrawler($node), $i)) {
$nodes[] = $node;
}
}

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

/**
Expand Down Expand Up @@ -427,7 +427,7 @@ public function siblings()
throw new \InvalidArgumentException('The current node list is empty.');
}

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

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

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

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

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

/**
Expand All @@ -484,7 +484,7 @@ public function parents()
}
}

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

/**
Expand All @@ -502,7 +502,7 @@ public function children()

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

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

/**
Expand Down Expand Up @@ -631,7 +631,7 @@ public function filterXPath($xpath)

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

return $this->filterRelativeXPath($xpath);
Expand Down Expand Up @@ -829,7 +829,7 @@ private function filterRelativeXPath($xpath)
{
$prefixes = $this->findNamespacePrefixes($xpath);

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

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

return array();
}

/**
* Creates a crawler for some subnodes.
*
* @param \DOMElement|\DOMElement[]|\DOMNodeList|null $nodes
*
* @return static
*/
private function createSubCrawler($nodes)
{
$crawler = new static($nodes, $this->uri, $this->baseHref);

return $crawler;
}
}
0