Skip to content

[DomCrawler] Deprecated using /_root/ in XPath expressions #16109

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 5, 2015
Merged
Show file tree
Hide file tree
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
24 changes: 13 additions & 11 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ private function relativize($xpath)

// BC for Symfony 2.4 and lower were elements were adding in a fake _root parent
if (0 === strpos($expression, '/_root/')) {
@trigger_error('XPath expressions referencing the fake root node are deprecated since version 2.8 and will be unsupported in 3.0. Please use "./" instead of "/_root/".', E_USER_DEPRECATED);

$expression = './'.substr($expression, 7);
} elseif (0 === strpos($expression, 'self::*/')) {
$expression = './'.substr($expression, 8);
Expand Down Expand Up @@ -1182,20 +1184,20 @@ private function createSubCrawler($nodes)

private function triggerDeprecation($methodName, $useTrace = false)
{
$traces = array();
$caller = array();

if ($useTrace || defined('HHVM_VERSION')) {
$traces = debug_backtrace();
$caller = $traces[2];
}
if (PHP_VERSION_ID >= 50400) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
} else {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}

// The SplObjectStorage class performs calls to its own methods. These
// method calls must not lead to triggered deprecation notices.
if (isset($caller['class']) && 'SplObjectStorage' === $caller['class']) {
return;
// The SplObjectStorage class performs calls to its own methods. These
// method calls must not lead to triggered deprecation notices.
if (isset($trace[2]['class']) && 'SplObjectStorage' === $trace[2]['class']) {
return;
}
}

@trigger_error('The '.$methodName.' method is deprecated as of 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
@trigger_error('The '.$methodName.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ public function testFilterXpathComplexQueries()

$this->assertCount(0, $crawler->filterXPath('/input'));
$this->assertCount(0, $crawler->filterXPath('/body'));
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
$this->assertCount(1, $crawler->filterXPath('./body'));
$this->assertCount(1, $crawler->filterXPath('.//body'));
$this->assertCount(5, $crawler->filterXPath('.//input'));
Expand Down Expand Up @@ -538,11 +537,20 @@ public function testFilterXPathWithFakeRoot()
{
$crawler = $this->createTestCrawler();
$this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node');
$this->assertCount(0, $crawler->filterXPath('/_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
$this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node');
$this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
}

/** @group legacy */
public function testLegacyFilterXPathWithFakeRoot()
{
$crawler = $this->createTestCrawler();
$this->assertCount(0, $crawler->filterXPath('/_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');

$crawler = $this->createTestCrawler()->filterXPath('//body');
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
}

public function testFilterXPathWithAncestorAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
Expand Down