diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 947345c2b9..5dd4dba3cd 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -854,7 +854,7 @@ class Crawler extends \SplObjectStorage $expression = $nonMatchingExpression; } elseif (0 === strpos($expression, 'descendant::')) { $expression = 'descendant-or-self::' . substr($expression, strlen('descendant::')); - } elseif (0 !== strpos($expression, 'descendant-or-self::')) { + } elseif (!preg_match('/^(ancestor|ancestor-or-self|attribute|child|descendant-or-self|following|following-sibling|parent|preceding|preceding-sibling|self)::/', $expression)) { $expression = 'self::' .$expression; } $expressions[] = $parenthesis.$expression; diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 2c52465670..4c9a4bcecf 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -411,6 +411,74 @@ EOF $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained'); } + public function testFilterXPathWithAncestorAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(2, $crawler->filterXPath('ancestor::*')); + } + + public function testFilterXPathWithAncestorOrSelfAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(3, $crawler->filterXPath('ancestor-or-self::*')); + } + + public function testFilterXPathWithAttributeAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(2, $crawler->filterXPath('attribute::*')); + } + + public function testFilterXPathWithChildAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//body'); + + $this->assertCount(2, $crawler->filterXPath('child::input')); + } + + public function testFilterXPathWithFollowingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//a'); + + $this->assertCount(3, $crawler->filterXPath('following::div')); + } + + public function testFilterXPathWithFollowingSiblingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//a'); + + $this->assertCount(2, $crawler->filterXPath('following-sibling::div')); + } + + public function testFilterXPathWithParentAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//button'); + + $this->assertEquals('foo', $crawler->filterXPath('parent::*')->attr('action')); + } + + public function testFilterXPathWithPrecedingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(13, $crawler->filterXPath('preceding::*')); + } + + public function testFilterXPathWithPrecedingSiblingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(9, $crawler->filterXPath('preceding-sibling::*')); + } + + public function testFilterXPathWithSelfAxes() + { + $this->assertCount(1, $this->createTestCrawler()->filterXPath('self::*')); + } + /** * @covers Symfony\Component\DomCrawler\Crawler::filter */