4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\Tests\Parser;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
16 use Symfony\Component\CssSelector\Node\FunctionNode;
17 use Symfony\Component\CssSelector\Node\SelectorNode;
18 use Symfony\Component\CssSelector\Parser\Parser;
19 use Symfony\Component\CssSelector\Parser\Token;
21 class ParserTest extends TestCase
23 /** @dataProvider getParserTestData */
24 public function testParser($source, $representation)
26 $parser = new Parser();
28 $this->assertEquals($representation, array_map(function (SelectorNode $node) {
29 return (string) $node->getTree();
30 }, $parser->parse($source)));
33 /** @dataProvider getParserExceptionTestData */
34 public function testParserException($source, $message)
36 $parser = new Parser();
39 $parser->parse($source);
40 $this->fail('Parser should throw a SyntaxErrorException.');
41 } catch (SyntaxErrorException $e) {
42 $this->assertEquals($message, $e->getMessage());
46 /** @dataProvider getPseudoElementsTestData */
47 public function testPseudoElements($source, $element, $pseudo)
49 $parser = new Parser();
50 $selectors = $parser->parse($source);
51 $this->assertCount(1, $selectors);
53 /** @var SelectorNode $selector */
54 $selector = $selectors[0];
55 $this->assertEquals($element, (string) $selector->getTree());
56 $this->assertEquals($pseudo, (string) $selector->getPseudoElement());
59 /** @dataProvider getSpecificityTestData */
60 public function testSpecificity($source, $value)
62 $parser = new Parser();
63 $selectors = $parser->parse($source);
64 $this->assertCount(1, $selectors);
66 /** @var SelectorNode $selector */
67 $selector = $selectors[0];
68 $this->assertEquals($value, $selector->getSpecificity()->getValue());
71 /** @dataProvider getParseSeriesTestData */
72 public function testParseSeries($series, $a, $b)
74 $parser = new Parser();
75 $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
76 $this->assertCount(1, $selectors);
78 /** @var FunctionNode $function */
79 $function = $selectors[0]->getTree();
80 $this->assertEquals(array($a, $b), Parser::parseSeries($function->getArguments()));
83 /** @dataProvider getParseSeriesExceptionTestData */
84 public function testParseSeriesException($series)
86 $parser = new Parser();
87 $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
88 $this->assertCount(1, $selectors);
90 /** @var FunctionNode $function */
91 $function = $selectors[0]->getTree();
92 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
93 Parser::parseSeries($function->getArguments());
96 public function getParserTestData()
99 array('*', array('Element[*]')),
100 array('*|*', array('Element[*]')),
101 array('*|foo', array('Element[foo]')),
102 array('foo|*', array('Element[foo|*]')),
103 array('foo|bar', array('Element[foo|bar]')),
104 array('#foo#bar', array('Hash[Hash[Element[*]#foo]#bar]')),
105 array('div>.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
106 array('div> .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
107 array('div >.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
108 array('div > .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
109 array("div \n> \t \t .foo", array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
110 array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
111 array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
112 array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
113 array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
114 array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
115 array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
116 array('div, td.foo, div.bar span', array('Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]')),
117 array('div > p', array('CombinedSelector[Element[div] > Element[p]]')),
118 array('td:first', array('Pseudo[Element[td]:first]')),
119 array('td :first', array('CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]')),
120 array('a[name]', array('Attribute[Element[a][name]]')),
121 array("a[ name\t]", array('Attribute[Element[a][name]]')),
122 array('a [name]', array('CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]')),
123 array('a[rel="include"]', array("Attribute[Element[a][rel = 'include']]")),
124 array('a[rel = include]', array("Attribute[Element[a][rel = 'include']]")),
125 array("a[hreflang |= 'en']", array("Attribute[Element[a][hreflang |= 'en']]")),
126 array('a[hreflang|=en]', array("Attribute[Element[a][hreflang |= 'en']]")),
127 array('div:nth-child(10)', array("Function[Element[div]:nth-child(['10'])]")),
128 array(':nth-child(2n+2)', array("Function[Element[*]:nth-child(['2', 'n', '+2'])]")),
129 array('div:nth-of-type(10)', array("Function[Element[div]:nth-of-type(['10'])]")),
130 array('div div:nth-of-type(10) .aclass', array("CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]")),
131 array('label:only', array('Pseudo[Element[label]:only]')),
132 array('a:lang(fr)', array("Function[Element[a]:lang(['fr'])]")),
133 array('div:contains("foo")', array("Function[Element[div]:contains(['foo'])]")),
134 array('div#foobar', array('Hash[Element[div]#foobar]')),
135 array('div:not(div.foo)', array('Negation[Element[div]:not(Class[Element[div].foo])]')),
136 array('td ~ th', array('CombinedSelector[Element[td] ~ Element[th]]')),
137 array('.foo[data-bar][data-baz=0]', array("Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]")),
141 public function getParserExceptionTestData()
144 array('attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
145 array('attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
146 array('html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()),
147 array(' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()),
148 array('div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()),
149 array(' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()),
150 array('p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()),
151 array('div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()),
152 array(' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()),
153 array('foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()),
154 array('#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()),
155 array('.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
156 array(':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
157 array('[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()),
158 array('[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()),
159 array('[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()),
160 array('[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()),
161 array(':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()),
162 array('[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()),
163 array('[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()),
164 array('[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()),
165 array(':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()),
166 array(':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()),
167 array('foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()),
171 public function getPseudoElementsTestData()
174 array('foo', 'Element[foo]', ''),
175 array('*', 'Element[*]', ''),
176 array(':empty', 'Pseudo[Element[*]:empty]', ''),
177 array(':BEfore', 'Element[*]', 'before'),
178 array(':aftER', 'Element[*]', 'after'),
179 array(':First-Line', 'Element[*]', 'first-line'),
180 array(':First-Letter', 'Element[*]', 'first-letter'),
181 array('::befoRE', 'Element[*]', 'before'),
182 array('::AFter', 'Element[*]', 'after'),
183 array('::firsT-linE', 'Element[*]', 'first-line'),
184 array('::firsT-letteR', 'Element[*]', 'first-letter'),
185 array('::Selection', 'Element[*]', 'selection'),
186 array('foo:after', 'Element[foo]', 'after'),
187 array('foo::selection', 'Element[foo]', 'selection'),
188 array('lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'),
189 array('video::-webkit-media-controls', 'Element[video]', '-webkit-media-controls'),
193 public function getSpecificityTestData()
198 array(':empty ', 10),
200 array('*:before', 1),
201 array(':nth-child(2)', 10),
204 array('[baz="4"]', 10),
205 array('[baz^="4"]', 10),
206 array('#lipsum', 100),
208 array(':not(foo)', 1),
209 array(':not(.foo)', 10),
210 array(':not([foo])', 10),
211 array(':not(:empty)', 10),
212 array(':not(#foo)', 100),
213 array('foo:empty', 11),
214 array('foo:before', 2),
215 array('foo::before', 2),
216 array('foo:empty::before', 12),
217 array('#lorem + foo#ipsum:first-child > bar:first-line', 213),
221 public function getParseSeriesTestData()
225 array('1n +3', 1, 3),
226 array('1n + 3', 1, 3),
227 array('1n+ 3', 1, 3),
228 array('1n-3', 1, -3),
229 array('1n -3', 1, -3),
230 array('1n - 3', 1, -3),
231 array('1n- 3', 1, -3),
243 public function getParseSeriesExceptionTestData()