Ver código fonte

[CssSelector] fixed CssSelector::toXPath() when the CSS selector is an empty string

Fabien Potencier 13 anos atrás
pai
commit
a82737528c

+ 4 - 0
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]);
             }

+ 1 - 0
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'));