From a82737528ced35ae1ff2d71e14a48b49286237f8 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 11 Mar 2012 10:18:25 +0100 Subject: [PATCH] [CssSelector] fixed CssSelector::toXPath() when the CSS selector is an empty string --- src/Symfony/Component/CssSelector/CssSelector.php | 4 ++++ tests/Symfony/Tests/Component/CssSelector/CssSelectorTest.php | 1 + 2 files changed, 5 insertions(+) diff --git a/src/Symfony/Component/CssSelector/CssSelector.php b/src/Symfony/Component/CssSelector/CssSelector.php index 930472c3775b..ed74df3fb752 100644 --- a/src/Symfony/Component/CssSelector/CssSelector.php +++ b/src/Symfony/Component/CssSelector/CssSelector.php @@ -45,6 +45,10 @@ class CssSelector static public function toXPath($cssExpr, $prefix = 'descendant-or-self::') { if (is_string($cssExpr)) { + if (!$cssExpr) { + return $prefix.'*'; + } + if (preg_match('#^\w+\s*$#u', $cssExpr, $match)) { return $prefix.trim($match[0]); } diff --git a/tests/Symfony/Tests/Component/CssSelector/CssSelectorTest.php b/tests/Symfony/Tests/Component/CssSelector/CssSelectorTest.php index 0addfc9a8495..23bd5a697dfa 100644 --- a/tests/Symfony/Tests/Component/CssSelector/CssSelectorTest.php +++ b/tests/Symfony/Tests/Component/CssSelector/CssSelectorTest.php @@ -17,6 +17,7 @@ class CssSelectorTest extends \PHPUnit_Framework_TestCase { public function testCsstoXPath() { + $this->assertEquals('descendant-or-self::*', CssSelector::toXPath('')); $this->assertEquals('descendant-or-self::h1', CssSelector::toXPath('h1')); $this->assertEquals("descendant-or-self::h1[@id = 'foo']", CssSelector::toXPath('h1#foo')); $this->assertEquals("descendant-or-self::h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", CssSelector::toXPath('h1.foo'));