10000 minor #16109 [DomCrawler] Deprecated using /_root/ in XPath expressio… · symfony/symfony@9bbab98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bbab98

Browse files
committed
minor #16109 [DomCrawler] Deprecated using /_root/ in XPath expressions (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [DomCrawler] Deprecated using /_root/ in XPath expressions | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 6042e86 [DomCrawler] Deprecated using /_root/ in XPath expressions
2 parents aa494d8 + 6042e86 commit 9bbab98

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,8 @@ private function relativize($xpath)
10361036

10371037
// BC for Symfony 2.4 and lower were elements were adding in a fake _root parent
10381038
if (0 === strpos($expression, '/_root/')) {
1039+
@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);
1040+
10391041
$expression = './'.substr($expression, 7);
10401042
} elseif (0 === strpos($expression, 'self::*/')) {
10411043
$expression = './'.substr($expression, 8);
@@ -1182,20 +1184,20 @@ private function createSubCrawler($nodes)
11821184

11831185
private function triggerDeprecation($methodName, $useTrace = false)
11841186
{
1185-
$traces = array();
1186-
$caller = array();
1187-
11881187
if ($useTrace || defined('HHVM_VERSION')) {
1189-
$traces = debug_backtrace();
1190-
$caller = $traces[2];
1191-
}
1188+
if (PHP_VERSION_ID >= 50400) {
1189+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
1190+
} else {
1191+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
1192+
}
11921193

1193-
// The SplObjectStorage class performs calls to its own methods. These
1194-
// method calls must not lead to triggered deprecation notices.
1195-
if (isset($caller['class']) && 'SplObjectStorage' === $caller['class']) {
1196-
return;
1194+
// The SplObjectStorage class performs calls to its own methods. These
1195+
// method calls must not lead to triggered deprecation notices.
1196+
if (isset($trace[2]['class']) && 'SplObjectStorage' === $trace[2]['class']) {
1197+
return;
1198+
}
11971199
}
11981200

1199-
@trigger_error('The '.$methodName.' method is deprecated as of 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
1201+
@trigger_error('The '.$methodName.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
12001202
}
12011203
}

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@ public function testFilterXpathComplexQueries()
446446

447447
$this->assertCount(0, $crawler->filterXPath('/input'));
448448
$this->assertCount(0, $crawler->filterXPath('/body'));
449-
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
450449
$this->assertCount(1, $crawler->filterXPath('./body'));
451450
$this->assertCount(1, $crawler->filterXPath('.//body'));
452451
$this->assertCount(5, $crawler->filterXPath('.//input'));
@@ -538,11 +537,20 @@ public function testFilterXPathWithFakeRoot()
538537
{
539538
$crawler = $this->createTestCrawler();
540539
$this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node');
541-
$this->assertCount(0, $crawler->filterXPath('/_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
542540
$this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node');
543541
$this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
544542
}
545543

544+
/** @group legacy */
545+
public function testLegacyFilterXPathWithFakeRoot()
546+
{
547+
$crawler = $this->createTestCrawler();
548+
$this->assertCount(0, $crawler->filterXPath('/_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
549+
550+
$crawler = $this->createTestCrawler()->filterXPath('//body');
551+
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
552+
}
553+
546554
public function testFilterXPathWithAncestorAxis()
547555
{
548556
$crawler = $this->createTestCrawler()->filterXPath('//form');

0 commit comments

Comments
 (0)
0