diff --git a/src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php b/src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php index 9ca1e0fc43d4..db352334f0a2 100644 --- a/src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php +++ b/src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php @@ -31,9 +31,18 @@ class ClassParser implements ParserInterface */ public function parse($source) { - // matches "." - if (preg_match('~^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$~', $source, $matches)) { - return array(new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null), $matches[2]))); + // Matches an optional namespace, optional element, and required class + // $source = 'test|input.ab6bd_field'; + // $matches = array (size=5) + // 0 => string 'test:input.ab6bd_field' (length=22) + // 1 => string 'test:' (length=5) + // 2 => string 'test' (length=4) + // 3 => string 'input' (length=5) + // 4 => string 'ab6bd_field' (length=11) + if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?\.([\w-]+)$/i', trim($source), $matches)) { + return array( + new SelectorNode(new ClassNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4])) + ); } return array(); diff --git a/src/Symfony/Component/CssSelector/Parser/Shortcut/ElementParser.php b/src/Symfony/Component/CssSelector/Parser/Shortcut/ElementParser.php index 042d430529d8..eae18b94679f 100644 --- a/src/Symfony/Component/CssSelector/Parser/Shortcut/ElementParser.php +++ b/src/Symfony/Component/CssSelector/Parser/Shortcut/ElementParser.php @@ -30,9 +30,15 @@ class ElementParser implements ParserInterface */ public function parse($source) { - // matches "" - if (preg_match('~^[ \t\r\n\f]*([a-zA-Z][a-zA-Z0-9_-]*|\\*)[ \t\r\n\f]*$~', $source, $matches)) { - return array(new SelectorNode(new ElementNode(null, $matches[1]))); + // Matches an optional namespace, required element or `*` + // $source = 'testns|testel'; + // $matches = array (size=4) + // 0 => string 'testns:testel' (length=13) + // 1 => string 'testns:' (length=7) + // 2 => string 'testns' (length=6) + // 3 => string 'testel' (length=6) + if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)$/i', trim($source), $matches)) { + return array(new SelectorNode(new ElementNode($matches[2] ?: null, $matches[3]))); } return array(); diff --git a/src/Symfony/Component/CssSelector/Parser/Shortcut/EmptyStringParser.php b/src/Symfony/Component/CssSelector/Parser/Shortcut/EmptyStringParser.php index de8819340592..0031f7cfef3b 100644 --- a/src/Symfony/Component/CssSelector/Parser/Shortcut/EmptyStringParser.php +++ b/src/Symfony/Component/CssSelector/Parser/Shortcut/EmptyStringParser.php @@ -34,8 +34,8 @@ class EmptyStringParser implements ParserInterface */ public function parse($source) { - // matches "" - if (preg_match('~^$~', $source, $matches)) { + // Matches an empty string + if ($source == '') { return array(new SelectorNode(new ElementNode(null, '*'))); } diff --git a/src/Symfony/Component/CssSelector/Parser/Shortcut/HashParser.php b/src/Symfony/Component/CssSelector/Parser/Shortcut/HashParser.php index 2a900f4a6b57..95d7d5f91174 100644 --- a/src/Symfony/Component/CssSelector/Parser/Shortcut/HashParser.php +++ b/src/Symfony/Component/CssSelector/Parser/Shortcut/HashParser.php @@ -31,9 +31,18 @@ class HashParser implements ParserInterface */ public function parse($source) { - // matches "#" - if (preg_match('~^[ \t\r\n\f]*([a-zA-Z][a-zA-Z0-9_-]*|\\*)?#([a-zA-Z0-9_-]+)[ \t\r\n\f]*$~', $source, $matches)) { - return array(new SelectorNode(new HashNode(new ElementNode(null, $matches[1] ?: null), $matches[2]))); + // Matches an optional namespace, optional element, and required id + // $source = 'test|input#ab6bd_field'; + // $matches = array (size=5) + // 0 => string 'test:input#ab6bd_field' (length=22) + // 1 => string 'test:' (length=5) + // 2 => string 'test' (length=4) + // 3 => string 'input' (length=5) + // 4 => string 'ab6bd_field' (length=11) + if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?#([\w-]+)$/i', trim($source), $matches)) { + return array( + new SelectorNode(new HashNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4])) + ); } return array(); diff --git a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ClassParserTest.php b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ClassParserTest.php index d04cad4138c5..403c7af8ed2e 100644 --- a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ClassParserTest.php +++ b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ClassParserTest.php @@ -34,7 +34,11 @@ public function testParse($source, $representation) public function getParseTestData() { return array( - array('.class', 'Class[Element[*].class]'), + array('.testclass', 'Class[Element[*].testclass]'), + array('testel.testclass', 'Class[Element[testel].testclass]'), + array('testns|.testclass', 'Class[Element[testns|*].testclass]'), + array('testns|*.testclass', 'Class[Element[testns|*].testclass]'), + array('testns|testel.testclass', 'Class[Element[testns|testel].testclass]'), ); } } diff --git a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ElementParserTest.php b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ElementParserTest.php index 107cac940c5a..2e436dad92f6 100644 --- a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ElementParserTest.php +++ b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ElementParserTest.php @@ -34,9 +34,10 @@ public function testParse($source, $representation) public function getParseTestData() { return array( - array('p', 'Element[p]'), array('*', 'Element[*]'), - array('h1', 'Element[h1]'), + array('testel', 'Element[testel]'), + array('testns|*', 'Element[testns|*]'), + array('testns|testel', 'Element[testns|testel]'), ); } } diff --git a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/EmptyStringParserTest.php b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/EmptyStringParserTest.php new file mode 100644 index 000000000000..8477fa126eb5 --- /dev/null +++ b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/EmptyStringParserTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut; + +use Symfony\Component\CssSelector\Node\SelectorNode; +use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser; + +/** + * @author Jean-François Simon + */ +class EmptyStringParserTest extends \PHPUnit_Framework_TestCase +{ + public function testParse() + { + $parser = new EmptyStringParser(); + $selectors = $parser->parse(''); + $this->assertEquals(1, count($selectors)); + + /** @var SelectorNode $selector */ + $selector = $selectors[0]; + $this->assertEquals('Element[*]', (string) $selector->getTree()); + + $selectors = $parser->parse('this will produce an empty array'); + $this->assertEquals(0, count($selectors)); + } +} diff --git a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/HashParserTest.php b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/HashParserTest.php index d9fee19dd779..41ecfc66799f 100644 --- a/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/HashParserTest.php +++ b/src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/HashParserTest.php @@ -34,8 +34,11 @@ public function testParse($source, $representation) public function getParseTestData() { return array( - array('#id', 'Hash[Element[*]#id]'), - array('h1#main', 'Hash[Element[h1]#main]'), + array('#testid', 'Hash[Element[*]#testid]'), + array('testel#testid', 'Hash[Element[testel]#testid]'), + array('testns|#testid', 'Hash[Element[testns|*]#testid]'), + array('testns|*#testid', 'Hash[Element[testns|*]#testid]'), + array('testns|testel#testid', 'Hash[Element[testns|testel]#testid]'), ); } }