-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSelectorTest.php
More file actions
435 lines (372 loc) · 13.1 KB
/
Copy pathSelectorTest.php
File metadata and controls
435 lines (372 loc) · 13.1 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\Unit\Property;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\Renderable;
use Sabberworm\CSS\Settings;
use TRegx\PhpUnit\DataProviders\DataProvider;
/**
* @covers \Sabberworm\CSS\Property\Selector
*/
final class SelectorTest extends TestCase
{
/**
* @test
*/
public function implementsRenderable(): void
{
$subject = new Selector('a');
self::assertInstanceOf(Renderable::class, $subject);
}
/**
* @test
*/
public function getSelectorByDefaultReturnsSelectorProvidedToConstructor(): void
{
$selector = 'a';
$subject = new Selector($selector);
self::assertSame($selector, $subject->getSelector());
}
/**
* @test
*/
public function setSelectorOverwritesSelectorProvidedToConstructor(): void
{
$subject = new Selector('a');
$selector = 'input';
$subject->setSelector($selector);
self::assertSame($selector, $subject->getSelector());
}
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: int<0, max>}>
*/
public static function provideSelectorsAndSpecificities(): array
{
return [
'type' => ['a', 1],
'class' => ['.highlighted', 10],
'type with class' => ['li.green', 11],
'pseudo-class' => [':hover', 10],
'type with pseudo-class' => ['a:hover', 11],
'class with pseudo-class' => ['.help:hover', 20],
'ID' => ['#file', 100],
'ID and descendent class' => ['#test .help', 110],
'type with ID' => ['h2#my-mug', 101],
'pseudo-element' => ['::before', 1],
'type with pseudo-element' => ['li::before', 2],
'type and descendent type with pseudo-element' => ['ol li::before', 3],
'`not`' => [':not(#your-mug)', 100],
// TODO, broken: The specificity should be the highest of the `:not` arguments, not the sum.
'`not` with multiple arguments' => [':not(#your-mug, .their-mug)', 110],
'attribute with `"`' => ['[alt="{}()[]\\"\',"]', 10],
'attribute with `\'`' => ['[alt=\'{}()[]"\\\',\']', 10],
];
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideSelectorsWithEscapedQuotes(): array
{
return [
'escaped double quote in double-quoted attribute' => ['a[href="test\\"value"]'],
'escaped single quote in single-quoted attribute' => ['a[href=\'test\\\'value\']'],
'multiple escaped double quotes in double-quoted attribute' => ['a[title="say \\"hello\\" world"]'],
'multiple escaped single quotes in single-quoted attribute' => ['a[title=\'say \\\'hello\\\' world\']'],
'escaped quote at start of attribute value' => ['a[data-test="\\"start"]'],
'escaped quote at end of attribute value' => ['a[data-test="end\\""]'],
'escaped backslash followed by quote' => ['a[data-test="test\\\\"]'],
'escaped backslash before escaped quote' => ['a[data-test="test\\\\\\"value"]'],
'triple backslash before quote' => ['a[data-test="test\\\\\\""]'],
'escaped single quotes in selector itself, with other escaped characters'
=> ['.before\\:content-\\[\\\'\\\'\\]:before'],
'escaped double quotes in selector itself, with other escaped characters'
=> ['.before\\:content-\\[\\"\\"\\]:before'],
];
}
/**
* @test
*
* @param non-empty-string $selector
*
* @dataProvider provideSelectorsAndSpecificities
* @dataProvider provideSelectorsWithEscapedQuotes
*/
public function parsesValidSelector(string $selector): void
{
$result = Selector::parse(new ParserState($selector, Settings::create()));
self::assertInstanceOf(Selector::class, $result);
self::assertSame($selector, $result->getSelector());
}
/**
* @test
*/
public function parsingAttributeWithEscapedQuoteDoesNotPrematurelyCloseString(): void
{
$selector = 'input[placeholder="Enter \\"quoted\\" text here"]';
$result = Selector::parse(new ParserState($selector, Settings::create()));
self::assertInstanceOf(Selector::class, $result);
self::assertSame($selector, $result->getSelector());
}
/**
* @test
*/
public function parseDistinguishesEscapedFromUnescapedQuotes(): void
{
// One backslash = escaped quote (should not close string)
$selector = 'a[data-value="test\\"more"]';
$result = Selector::parse(new ParserState($selector, Settings::create()));
self::assertSame($selector, $result->getSelector());
}
/**
* @test
*/
public function parseHandlesEvenNumberOfBackslashesBeforeQuote(): void
{
// Two backslashes = escaped backslash + unescaped quote (should close string)
$selector = 'a[data-value="test\\\\"]';
$result = Selector::parse(new ParserState($selector, Settings::create()));
self::assertSame($selector, $result->getSelector());
}
/**
* @return array<non-empty-string, array{0: string}>
*/
public static function provideInvalidSelectors(): array
{
return [
// This is currently broken.
// 'empty string' => [''],
'percent sign' => ['%'],
// This is currently broken.
// 'hash only' => ['#'],
// This is currently broken.
// 'dot only' => ['.'],
'slash' => ['/'],
'less-than sign' => ['<'],
// This is currently broken.
// 'whitespace only' => [" \t\n\r"],
];
}
/**
* @return array<non-empty-string, array{0: string}>
*/
public static function provideInvalidSelectorsForParse(): array
{
return [
'empty string' => [''],
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'a `:not` missing the closing brace' => [':not(a'],
'a `:not` missing the opening brace' => [':not a)'],
'attribute value missing closing single quote' => ['a[href=\'#top]'],
'attribute value missing closing double quote' => ['a[href="#top]'],
'attribute value with mismatched quotes, single quote opening' => ['a[href=\'#top"]'],
'attribute value with mismatched quotes, double quote opening' => ['a[href="#top\']'],
];
}
/**
* @test
*
* @param non-empty-string $selector
*
* @dataProvider provideInvalidSelectors
* @dataProvider provideInvalidSelectorsForParse
*/
public function parseThrowsExceptionWithInvalidSelector(string $selector): void
{
$this->expectException(UnexpectedTokenException::class);
Selector::parse(new ParserState($selector, Settings::create()));
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideStopCharacters(): array
{
return [
',' => [','],
'{' => ['{'],
'}' => ['}'],
];
}
/**
* @return DataProvider<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*/
public function provideStopCharactersAndValidSelectors(): DataProvider
{
return DataProvider::cross(self::provideStopCharacters(), self::provideSelectorsAndSpecificities());
}
/**
* @test
*
* @param non-empty-string $stopCharacter
* @param non-empty-string $selector
*
* @dataProvider provideStopCharactersAndValidSelectors
*/
public function parseDoesNotConsumeStopCharacter(string $stopCharacter, string $selector): void
{
$subject = new ParserState($selector . $stopCharacter, Settings::create());
Selector::parse($subject);
self::assertSame($stopCharacter, $subject->peek());
}
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*/
public static function provideSelectorsWithAndWithoutComment(): array
{
return [
'comment before' => ['/*comment*/body', 'body'],
'comment after' => ['body/*comment*/', 'body'],
'comment within' => ['./*comment*/teapot', '.teapot'],
'comment within function' => [':not(#your-mug,/*comment*/.their-mug)', ':not(#your-mug,.their-mug)'],
];
}
/**
* @test
*
* @param non-empty-string $selectorWith
* @param non-empty-string $selectorWithout
*
* @dataProvider provideSelectorsWithAndWithoutComment
*/
public function parsesSelectorWithComment(string $selectorWith, string $selectorWithout): void
{
$result = Selector::parse(new ParserState($selectorWith, Settings::create()));
self::assertInstanceOf(Selector::class, $result);
self::assertSame($selectorWithout, $result->getSelector());
}
/**
* @test
*
* @param non-empty-string $selector
*
* @dataProvider provideSelectorsWithAndWithoutComment
*/
public function parseExtractsCommentFromSelector(string $selector): void
{
$result = [];
Selector::parse(new ParserState($selector, Settings::create()), $result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame('comment', $result[0]->getComment());
}
/**
* @test
*/
public function parsesSelectorWithTwoComments(): void
{
$result = Selector::parse(new ParserState('/*comment1*/a/*comment2*/', Settings::create()));
self::assertInstanceOf(Selector::class, $result);
self::assertSame('a', $result->getSelector());
}
/**
* @test
*/
public function parseExtractsTwoCommentsFromSelector(): void
{
$result = [];
Selector::parse(new ParserState('/*comment1*/a/*comment2*/', Settings::create()), $result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame('comment1', $result[0]->getComment());
self::assertInstanceOf(Comment::class, $result[1]);
self::assertSame('comment2', $result[1]->getComment());
}
/**
* @test
*
* @param non-empty-string $selector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideSelectorsAndSpecificities
*/
public function getSpecificityByDefaultReturnsSpecificityOfSelectorProvidedToConstructor(
string $selector,
int $expectedSpecificity
): void {
$subject = new Selector($selector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @param non-empty-string $selector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideSelectorsAndSpecificities
*/
public function getSpecificityReturnsSpecificityOfSelectorLastProvidedViaSetSelector(
string $selector,
int $expectedSpecificity
): void {
$subject = new Selector('p');
$subject->setSelector($selector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @dataProvider provideSelectorsAndSpecificities
* @dataProvider provideSelectorsWithEscapedQuotes
*/
public function isValidForValidSelectorReturnsTrue(string $selector): void
{
self::assertTrue(Selector::isValid($selector));
}
/**
* @test
*
* @dataProvider provideInvalidSelectors
*/
public function isValidForInvalidSelectorReturnsFalse(string $selector): void
{
self::assertFalse(Selector::isValid($selector));
}
/**
* @test
*/
public function cleansUpSpacesWithinSelector(): void
{
$selector = 'p > small';
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function cleansUpTabsWithinSelector(): void
{
$selector = "p\t>\tsmall";
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function cleansUpNewLineWithinSelector(): void
{
$selector = "p\n>\nsmall";
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function doesNotCleanupSpacesWithinAttributeSelector(): void
{
$subject = new Selector('a[title="extra space"]');
self::assertSame('a[title="extra space"]', $subject->getSelector());
}
/**
* @test
*/
public function getArrayRepresentationThrowsException(): void
{
$this->expectException(\BadMethodCallException::class);
$subject = new Selector('a');
$subject->getArrayRepresentation();
}
}