53b35a95473cc03ed00f0bc6fab3be5b704c8373
[yaffs-website] / vendor / symfony / css-selector / Tests / Parser / ParserTest.php
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;
13
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;
20
21 class ParserTest extends TestCase
22 {
23     /** @dataProvider getParserTestData */
24     public function testParser($source, $representation)
25     {
26         $parser = new Parser();
27
28         $this->assertEquals($representation, array_map(function (SelectorNode $node) {
29             return (string) $node->getTree();
30         }, $parser->parse($source)));
31     }
32
33     /** @dataProvider getParserExceptionTestData */
34     public function testParserException($source, $message)
35     {
36         $parser = new Parser();
37
38         try {
39             $parser->parse($source);
40             $this->fail('Parser should throw a SyntaxErrorException.');
41         } catch (SyntaxErrorException $e) {
42             $this->assertEquals($message, $e->getMessage());
43         }
44     }
45
46     /** @dataProvider getPseudoElementsTestData */
47     public function testPseudoElements($source, $element, $pseudo)
48     {
49         $parser = new Parser();
50         $selectors = $parser->parse($source);
51         $this->assertCount(1, $selectors);
52
53         /** @var SelectorNode $selector */
54         $selector = $selectors[0];
55         $this->assertEquals($element, (string) $selector->getTree());
56         $this->assertEquals($pseudo, (string) $selector->getPseudoElement());
57     }
58
59     /** @dataProvider getSpecificityTestData */
60     public function testSpecificity($source, $value)
61     {
62         $parser = new Parser();
63         $selectors = $parser->parse($source);
64         $this->assertCount(1, $selectors);
65
66         /** @var SelectorNode $selector */
67         $selector = $selectors[0];
68         $this->assertEquals($value, $selector->getSpecificity()->getValue());
69     }
70
71     /** @dataProvider getParseSeriesTestData */
72     public function testParseSeries($series, $a, $b)
73     {
74         $parser = new Parser();
75         $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
76         $this->assertCount(1, $selectors);
77
78         /** @var FunctionNode $function */
79         $function = $selectors[0]->getTree();
80         $this->assertEquals(array($a, $b), Parser::parseSeries($function->getArguments()));
81     }
82
83     /** @dataProvider getParseSeriesExceptionTestData */
84     public function testParseSeriesException($series)
85     {
86         $parser = new Parser();
87         $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
88         $this->assertCount(1, $selectors);
89
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());
94     }
95
96     public function getParserTestData()
97     {
98         return array(
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']]")),
138         );
139     }
140
141     public function getParserExceptionTestData()
142     {
143         return array(
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()),
168         );
169     }
170
171     public function getPseudoElementsTestData()
172     {
173         return array(
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'),
190         );
191     }
192
193     public function getSpecificityTestData()
194     {
195         return array(
196             array('*', 0),
197             array(' foo', 1),
198             array(':empty ', 10),
199             array(':before', 1),
200             array('*:before', 1),
201             array(':nth-child(2)', 10),
202             array('.bar', 10),
203             array('[baz]', 10),
204             array('[baz="4"]', 10),
205             array('[baz^="4"]', 10),
206             array('#lipsum', 100),
207             array(':not(*)', 0),
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),
218         );
219     }
220
221     public function getParseSeriesTestData()
222     {
223         return array(
224             array('1n+3', 1, 3),
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),
232             array('n-5', 1, -5),
233             array('odd', 2, 1),
234             array('even', 2, 0),
235             array('3n', 3, 0),
236             array('n', 1, 0),
237             array('+n', 1, 0),
238             array('-n', -1, 0),
239             array('5', 0, 5),
240         );
241     }
242
243     public function getParseSeriesExceptionTestData()
244     {
245         return array(
246             array('foo'),
247             array('n+'),
248         );
249     }
250 }