Skip to content

Commit 3c015d5

Browse files
committed
Updated parsers to support namespaces (fix for ClassParser included)
1 parent a7c9863 commit 3c015d5

File tree

8 files changed

+83
-16
lines changed

8 files changed

+83
-16
lines changed

src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@ class ClassParser implements ParserInterface
3131
*/
3232
public function parse($source)
3333
{
34-
// matches "<selector>.<name>"
35-
if (preg_match('~^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$~', $source, $matches)) {
36-
return array(new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null), $matches[2])));
34+
// Matches an optional namespace, optional element, and required class
35+
// $source = 'test|input.ab6bd_field';
36+
// $matches = array (size=5)
37+
// 0 => string 'test:input.ab6bd_field' (length=22)
38+
// 1 => string 'test:' (length=5)
39+
// 2 => string 'test' (length=4)
40+
// 3 => string 'input' (length=5)
41+
// 4 => string 'ab6bd_field' (length=11)
42+
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?\.([\w-]+)$/i', trim($source), $matches)) {
43+
return array(
44+
new SelectorNode(new ClassNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4]))
45+
);
3746
}
3847

3948
return array();

src/Symfony/Component/CssSelector/Parser/Shortcut/ElementParser.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ class ElementParser implements ParserInterface
3030
*/
3131
public function parse($source)
3232
{
33-
// matches "<element>"
34-
if (preg_match('~^[ \t\r\n\f]*([a-zA-Z][a-zA-Z0-9_-]*|\\*)[ \t\r\n\f]*$~', $source, $matches)) {
35-
return array(new SelectorNode(new ElementNode(null, $matches[1])));
33+
// Matches an optional namespace, required element or `*`
34+
// $source = 'testns|testel';
35+
// $matches = array (size=4)
36+
// 0 => string 'testns:testel' (length=13)
37+
// 1 => string 'testns:' (length=7)
38+
// 2 => string 'testns' (length=6)
39+
// 3 => string 'testel' (length=6)
40+
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)$/i', trim($source), $matches)) {
41+
return array(new SelectorNode(new ElementNode($matches[2] ?: null, $matches[3])));
3642
}
3743

3844
return array();

src/Symfony/Component/CssSelector/Parser/Shortcut/EmptyStringParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class EmptyStringParser implements ParserInterface
3434
*/
3535
public function parse($source)
3636
{
37-
// matches ""
38-
if (preg_match('~^$~', $source, $matches)) {
37+
// Matches an empty string
38+
if ($source == '') {
3939
return array(new SelectorNode(new ElementNode(null, '*')));
4040
}
4141

src/Symfony/Component/CssSelector/Parser/Shortcut/HashParser.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@ class HashParser implements ParserInterface
3131
*/
3232
public function parse($source)
3333
{
34-
// matches "<selector>#<id>"
35-
if (preg_match('~^[ \t\r\n\f]*([a-zA-Z][a-zA-Z0-9_-]*|\\*)?#([a-zA-Z0-9_-]+)[ \t\r\n\f]*$~', $source, $matches)) {
36-
return array(new SelectorNode(new HashNode(new ElementNode(null, $matches[1] ?: null), $matches[2])));
34+
// Matches an optional namespace, optional element, and required id
35+
// $source = 'test|input#ab6bd_field';
36+
// $matches = array (size=5)
37+
// 0 => string 'test:input#ab6bd_field' (length=22)
38+
// 1 => string 'test:' (length=5)
39+
// 2 => string 'test' (length=4)
40+
// 3 => string 'input' (length=5)
41+
// 4 => string 'ab6bd_field' (length=11)
42+
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?#([\w-]+)$/i', trim($source), $matches)) {
43+
return array(
44+
new SelectorNode(new HashNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4]))
45+
);
3746
}
3847

3948
return array();

src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ClassParserTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function testParse($source, $representation)
3434
public function getParseTestData()
3535
{
3636
return array(
37-
array('.class', 'Class[Element[*].class]'),
37+
array('.testclass', 'Class[Element[*].testclass]'),
38+
array('testel.testclass', 'Class[Element[testel].testclass]'),
39+
array('testns|.testclass', 'Class[Element[testns|*].testclass]'),
40+
array('testns|*.testclass', 'Class[Element[testns|*].testclass]'),
41+
array('testns|testel.testclass', 'Class[Element[testns|testel].testclass]'),
3842
);
3943
}
4044
}

src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/ElementParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public function testParse($source, $representation)
3434
public function getParseTestData()
3535
{
3636
return array(
37-
array('p', 'Element[p]'),
3837
array('*', 'Element[*]'),
39-
array('h1', 'Element[h1]'),
38+
array('testel', 'Element[testel]'),
39+
array('testns|*', 'Element[testns|*]'),
40+
array('testns|testel', 'Element[testns|testel]'),
4041
);
4142
}
4243
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
13+
14+
use Symfony\Component\CssSelector\Node\SelectorNode;
15+
use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
16+
17+
/**
18+
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
19+
*/
20+
class EmptyStringParserTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testParse()
23+
{
24+
$parser = new EmptyStringParser();
25+
$selectors = $parser->parse('');
26+
$this->assertEquals(1, count($selectors));
27+
28+
/** @var SelectorNode $selector */
29+
$selector = $selectors[0];
30+
$this->assertEquals('Element[*]', (string) $selector->getTree());
31+
32+
$selectors = $parser->parse('this will produce an empty array');
33+
$this->assertEquals(0, count($selectors));
34+
}
35+
}

src/Symfony/Component/CssSelector/Tests/Parser/Shortcut/HashParserTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public function testParse($source, $representation)
3434
public function getParseTestData()
3535
{
3636
return array(
37-
array('#id', 'Hash[Element[*]#id]'),
38-
array('h1#main', 'Hash[Element[h1]#main]'),
37+
array('#testid', 'Hash[Element[*]#testid]'),
38+
array('testel#testid', 'Hash[Element[testel]#testid]'),
39+
array('testns|#testid', 'Hash[Element[testns|*]#testid]'),
40+
array('testns|*#testid', 'Hash[Element[testns|*]#testid]'),
41+
array('testns|testel#testid', 'Hash[Element[testns|testel]#testid]'),
3942
);
4043
}
4144
}

0 commit comments

Comments
 (0)