ParserTest.php
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Parser;
use Symfony\Component\CssSelector\Parser\Token;
class ParserTest extends TestCase
{
/** @dataProvider getParserTestData */
public function testParser($source, $representation)
{
$parser = new Parser();
$this->assertEquals($representation, array_map(function (SelectorNode $node) {
return (string) $node->getTree();
}, $parser->parse($source)));
}
/** @dataProvider getParserExceptionTestData */
public function testParserException($source, $message)
{
$parser = new Parser();
try {
$parser->parse($source);
$this->fail('Parser should throw a SyntaxErrorException.');
} catch (SyntaxErrorException $e) {
$this->assertEquals($message, $e->getMessage());
}
}
/** @dataProvider getPseudoElementsTestData */
public function testPseudoElements($source, $element, $pseudo)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($element, (string) $selector->getTree());
$this->assertEquals($pseudo, (string) $selector->getPseudoElement());
}
/** @dataProvider getSpecificityTestData */
public function testSpecificity($source, $value)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($value, $selector->getSpecificity()->getValue());
}
/** @dataProvider getParseSeriesTestData */
public function testParseSeries($series, $a, $b)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->assertEquals([$a, $b], Parser::parseSeries($function->getArguments()));
}
/** @dataProvider getParseSeriesExceptionTestData */
public function testParseSeriesException($series)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
Parser::parseSeries($function->getArguments());
}
public function getParserTestData()
{
return [
['*', ['Element[*]']],
['*|*', ['Element[*]']],
['*|foo', ['Element[foo]']],
['foo|*', ['Element[foo|*]']],
['foo|bar', ['Element[foo|bar]']],
['#foo#bar', ['Hash[Hash[Element[*]#foo]#bar]']],
['div>.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div> .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div >.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div > .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
["div \n> \t \t .foo", ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['div, td.foo, div.bar span', ['Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]']],
['div > p', ['CombinedSelector[Element[div] > Element[p]]']],
['td:first', ['Pseudo[Element[td]:first]']],
['td :first', ['CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]']],
['a[name]', ['Attribute[Element[a][name]]']],
["a[ name\t]", ['Attribute[Element[a][name]]']],
['a [name]', ['CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]']],
['[name="foo"]', ["Attribute[Element[*][name = 'foo']]"]],
["[name='foo[1]']", ["Attribute[Element[*][name = 'foo[1]']]"]],
["[name='foo[0][bar]']", ["Attribute[Element[*][name = 'foo[0][bar]']]"]],
['a[rel="include"]', ["Attribute[Element[a][rel = 'include']]"]],
['a[rel = include]', ["Attribute[Element[a][rel = 'include']]"]],
["a[hreflang |= 'en']", ["Attribute[Element[a][hreflang |= 'en']]"]],
['a[hreflang|=en]', ["Attribute[Element[a][hreflang |= 'en']]"]],
['div:nth-child(10)', ["Function[Element[div]:nth-child(['10'])]"]],
[':nth-child(2n+2)', ["Function[Element[*]:nth-child(['2', 'n', '+2'])]"]],
['div:nth-of-type(10)', ["Function[Element[div]:nth-of-type(['10'])]"]],
['div div:nth-of-type(10) .aclass', ["CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]"]],
['label:only', ['Pseudo[Element[label]:only]']],
['a:lang(fr)', ["Function[Element[a]:lang(['fr'])]"]],
['div:contains("foo")', ["Function[Element[div]:contains(['foo'])]"]],
['div#foobar', ['Hash[Element[div]#foobar]']],
['div:not(div.foo)', ['Negation[Element[div]:not(Class[Element[div].foo])]']],
['td ~ th', ['CombinedSelector[Element[td] ~ Element[th]]']],
['.foo[data-bar][data-baz=0]', ["Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]"]],
];
}
public function getParserExceptionTestData()
{
return [
['attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
['attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
['html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()],
[' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()],
['div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()],
[' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()],
['p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()],
['div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()],
[' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()],
['foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()],
['#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()],
['.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
[':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
['[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()],
['[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()],
['[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()],
['[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()],
[':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()],
['[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()],
['[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()],
['[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()],
[':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()],
[':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()],
['foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()],
];
}
public function getPseudoElementsTestData()
{
return [
['foo', 'Element[foo]', ''],
['*', 'Element[*]', ''],
[':empty', 'Pseudo[Element[*]:empty]', ''],
[':BEfore', 'Element[*]', 'before'],
[':aftER', 'Element[*]', 'after'],
[':First-Line', 'Element[*]', 'first-line'],
[':First-Letter', 'Element[*]', 'first-letter'],
['::befoRE', 'Element[*]', 'before'],
['::AFter', 'Element[*]', 'after'],
['::firsT-linE', 'Element[*]', 'first-line'],
['::firsT-letteR', 'Element[*]', 'first-letter'],
['::Selection', 'Element[*]', 'selection'],
['foo:after', 'Element[foo]', 'after'],
['foo::selection', 'Element[foo]', 'selection'],
['lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'],
['video::-webkit-media-controls', 'Element[video]', '-webkit-media-controls'],
];
}
public function getSpecificityTestData()
{
return [
['*', 0],
[' foo', 1],
[':empty ', 10],
[':before', 1],
['*:before', 1],
[':nth-child(2)', 10],
['.bar', 10],
['[baz]', 10],
['[baz="4"]', 10],
['[baz^="4"]', 10],
['#lipsum', 100],
[':not(*)', 0],
[':not(foo)', 1],
[':not(.foo)', 10],
[':not([foo])', 10],
[':not(:empty)', 10],
[':not(#foo)', 100],
['foo:empty', 11],
['foo:before', 2],
['foo::before', 2],
['foo:empty::before', 12],
['#lorem + foo#ipsum:first-child > bar:first-line', 213],
];
}
public function getParseSeriesTestData()
{
return [
['1n+3', 1, 3],
['1n +3', 1, 3],
['1n + 3', 1, 3],
['1n+ 3', 1, 3],
['1n-3', 1, -3],
['1n -3', 1, -3],
['1n - 3', 1, -3],
['1n- 3', 1, -3],
['n-5', 1, -5],
['odd', 2, 1],
['even', 2, 0],
['3n', 3, 0],
['n', 1, 0],
['+n', 1, 0],
['-n', -1, 0],
['5', 0, 5],
];
}
public function getParseSeriesExceptionTestData()
{
return [
['foo'],
['n+'],
];
}
}