-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathCompoundSelectorTest.php
More file actions
517 lines (451 loc) · 17.4 KB
/
Copy pathCompoundSelectorTest.php
File metadata and controls
517 lines (451 loc) · 17.4 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\Unit\Property\Selector;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\Property\Selector\Component;
use Sabberworm\CSS\Property\Selector\CompoundSelector;
use Sabberworm\CSS\Renderable;
use Sabberworm\CSS\Settings;
use TRegx\PhpUnit\DataProviders\DataProvider;
/**
* @covers \Sabberworm\CSS\Property\Selector\CompoundSelector
*/
final class CompoundSelectorTest extends TestCase
{
/**
* @test
*/
public function implementsRenderable(): void
{
self::assertInstanceOf(Renderable::class, new CompoundSelector('a:hover'));
}
/**
* @test
*/
public function implementsSelectorComponent(): void
{
self::assertInstanceOf(Component::class, new CompoundSelector('a:hover'));
}
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: int<0, max>}>
*/
public static function provideCompoundSelectorAndSpecificity(): 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],
'type with ID' => ['h2#my-mug', 101],
'pseudo-element' => ['::before', 1],
'type with pseudo-element' => ['li::before', 2],
'`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],
// TODO, broken: specificity should be 11, but the calculator doesn't realize the `#` is in a string.
'attribute with `^=`' => ['a[href^="#"]', 111],
'attribute with `*=`' => ['a[href*="example"]', 11],
// TODO, broken: specificity should be 11, but the calculator doesn't realize the `.` is in a string.
'attribute with `$=`' => ['a[href$=".org"]', 21],
'attribute with `~=`' => ['span[title~="bonjour"]', 11],
'attribute with `|=`' => ['[lang|="en"]', 10],
// TODO, broken: specificity should be 11, but the calculator doesn't realize the `i` is in an attribute.
'attribute with case insensitive modifier' => ['a[href*="insensitive" i]', 12],
// TODO, broken: specificity should be 21, but the calculator doesn't realize the `.` is in a string.
'multiple attributes' => ['a[href^="https://"][href$=".org"]', 31],
// TODO, broken: specificity should be 11, but the calculator is treating the `n` as a type selector.
'nth-last-child' => ['li:nth-last-child(2n+3)', 12],
// TODO, maybe broken: specificity should probably be 2 (1 for `p` and 1 for `span`).
'`not` with descendent combinator' => [':not(p span)', 1],
// TODO, maybe broken: specificity should probably be 2 (1 for `p` and 1 for `span`).
'`not` with child combinator' => [':not(p > span)', 1],
// TODO, maybe broken: specificity should probably be 2 (1 for `h1` and 1 for `p`).
'`not` with next-sibling combinator' => [':not(h1 + p)', 1],
// TODO, maybe broken: specificity should probably be 2 (1 for `h1` and 1 for `p`).
'`not` with subsequent-sibling combinator' => [':not(h1 ~ p)', 1],
];
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideCompoundSelectorWithEscapedQuotes(): 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 $compoundSelector
*
* @dataProvider provideCompoundSelectorAndSpecificity
* @dataProvider provideCompoundSelectorWithEscapedQuotes
*/
public function parsesValidCompoundSelector(string $compoundSelector): void
{
$result = CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()));
self::assertInstanceOf(CompoundSelector::class, $result);
self::assertSame($compoundSelector, $result->getValue());
}
/**
* @test
*/
public function parsingAttributeWithEscapedQuoteDoesNotPrematurelyCloseString(): void
{
$compoundSelector = 'input[placeholder="Enter \\"quoted\\" text here"]';
$result = CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()));
self::assertInstanceOf(CompoundSelector::class, $result);
self::assertSame($compoundSelector, $result->getValue());
}
/**
* @test
*/
public function parseDistinguishesEscapedFromUnescapedQuotes(): void
{
// One backslash = escaped quote (should not close string)
$compoundSelector = 'a[data-value="test\\"more"]';
$result = CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()));
self::assertSame($compoundSelector, $result->getValue());
}
/**
* @test
*/
public function parseHandlesEvenNumberOfBackslashesBeforeQuote(): void
{
// Two backslashes = escaped backslash + unescaped quote (should close string)
$compoundSelector = 'a[data-value="test\\\\"]';
$result = CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()));
self::assertSame($compoundSelector, $result->getValue());
}
/**
* @return array<non-empty-string, array{0: string}>
*/
public static function provideInvalidCompoundSelector(): array
{
return [
'empty string' => [''],
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'percent sign' => ['%'],
// This is currently broken.
// 'hash only' => ['#'],
// This is currently broken.
// 'dot only' => ['.'],
'slash' => ['/'],
'less-than sign' => ['<'],
'with space before' => [' a:hover'],
];
}
/**
* The validation checks on `setValue()` are not able to pick up these, because it does not perform any parsing.
*
* @return array<non-empty-string, array{0: string}>
*/
public static function provideInvalidCompoundSelectorForParse(): array
{
return [
'a `:not` missing the closing brace' => [':not(a'],
'a `:not` missing the opening brace' => [':nota)'],
'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\']'],
'attribute value with extra `[`' => ['a[[href="#top"]'],
'attribute value with extra `]`' => ['a[href="#top"]]'],
];
}
/**
* This is valid in a parsing context, but not when passed to `setValue()` or the constructor.
*
* @return array<non-empty-string, array{0: string}>
*/
public static function provideInvalidCompoundSelectorForSetValue(): array
{
return [
'with space after' => ['a:hover '],
];
}
/**
* @test
*
* @param non-empty-string $compoundSelector
*
* @dataProvider provideInvalidCompoundSelector
* @dataProvider provideInvalidCompoundSelectorForParse
*/
public function parseThrowsExceptionWithInvalidCompoundSelector(string $compoundSelector): void
{
$this->expectException(UnexpectedTokenException::class);
CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()));
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideStopCharacters(): array
{
return [
',' => [','],
'{' => ['{'],
'}' => ['}'],
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'child combinator' => ['>'],
'next-sibling combinator' => ['+'],
'subsequent-sibling combinator' => ['~'],
];
}
/**
* @return DataProvider<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*/
public function provideStopCharactersAndValidCompoundSelector(): DataProvider
{
return DataProvider::cross(self::provideStopCharacters(), self::provideCompoundSelectorAndSpecificity());
}
/**
* @test
*
* @param non-empty-string $stopCharacter
* @param non-empty-string $compoundSelector
*
* @dataProvider provideStopCharactersAndValidCompoundSelector
*/
public function parseDoesNotConsumeStopCharacter(string $stopCharacter, string $compoundSelector): void
{
$subject = new ParserState($compoundSelector . $stopCharacter, Settings::create());
CompoundSelector::parse($subject);
self::assertSame($stopCharacter, $subject->peek());
}
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*/
public static function provideCompoundSelectorWithAndWithoutComment(): array
{
return [
'comment before' => ['/*comment*/body.page', 'body.page'],
'comment after' => ['body.page/*comment*/', 'body.page'],
'comment within' => ['p./*comment*/teapot', 'p.teapot'],
'comment within function' => ['p:not(#your-mug,/*comment*/.their-mug)', 'p:not(#your-mug,.their-mug)'],
];
}
/**
* @test
*
* @param non-empty-string $compoundSelectorWith
* @param non-empty-string $compoundSelectorWithout
*
* @dataProvider provideCompoundSelectorWithAndWithoutComment
*/
public function parsesCompoundSelectorWithComment(
string $compoundSelectorWith,
string $compoundSelectorWithout
): void {
$result = CompoundSelector::parse(new ParserState($compoundSelectorWith, Settings::create()));
self::assertInstanceOf(CompoundSelector::class, $result);
self::assertSame($compoundSelectorWithout, $result->getValue());
}
/**
* @test
*
* @param non-empty-string $compoundSelector
*
* @dataProvider provideCompoundSelectorWithAndWithoutComment
*/
public function parseExtractsCommentFromCompoundSelector(string $compoundSelector): void
{
$result = [];
CompoundSelector::parse(new ParserState($compoundSelector, Settings::create()), $result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame('comment', $result[0]->getComment());
}
/**
* @test
*/
public function parsesCompoundSelectorWithTwoComments(): void
{
$result = CompoundSelector::parse(new ParserState('/*comment1*/a:hover/*comment2*/', Settings::create()));
self::assertInstanceOf(CompoundSelector::class, $result);
self::assertSame('a:hover', $result->getValue());
}
/**
* @test
*/
public function parseExtractsTwoCommentsFromCompoundSelector(): void
{
$result = [];
CompoundSelector::parse(new ParserState('/*comment1*/a:hover/*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 $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function constructsWithValueProvided(string $value): void
{
$subject = new CompoundSelector($value);
self::assertSame($value, $subject->getArrayRepresentation()['value']);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideInvalidCompoundSelector
* @dataProvider provideInvalidCompoundSelectorForSetValue
*/
public function constructorThrowsExceptionWithInvalidValue(string $value): void
{
$this->expectException(\UnexpectedValueException::class);
new CompoundSelector($value);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function setValueSetsValueProvided(string $value): void
{
$subject = new CompoundSelector('p.intro');
$subject->setValue($value);
self::assertSame($value, $subject->getArrayRepresentation()['value']);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideInvalidCompoundSelector
* @dataProvider provideInvalidCompoundSelectorForSetValue
*/
public function setValueThrowsExceptionWithInvalidValue(string $value): void
{
$this->expectException(\UnexpectedValueException::class);
$subject = new CompoundSelector('p.intro');
$subject->setValue($value);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function getValueReturnsValueProvidedToConstructor(string $value): void
{
$subject = new CompoundSelector($value);
$result = $subject->getValue();
self::assertSame($value, $result);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function getValueReturnsValueProvidedToSetValue(string $value): void
{
$subject = new CompoundSelector('p.intro');
$subject->setValue($value);
$result = $subject->getValue();
self::assertSame($value, $result);
}
/**
* @test
*
* @param non-empty-string $compoundSelector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function getSpecificityByDefaultReturnsSpecificityOfCompoundSelectorProvidedToConstructor(
string $compoundSelector,
int $expectedSpecificity
): void {
$subject = new CompoundSelector($compoundSelector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @param non-empty-string $compoundSelector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function getSpecificityReturnsSpecificityOfCompoundSelectorLastProvidedViaSetValue(
string $compoundSelector,
int $expectedSpecificity
): void {
$subject = new CompoundSelector('p.intro');
$subject->setValue($compoundSelector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function renderReturnsValueProvided(string $value): void
{
$subject = new CompoundSelector($value);
self::assertSame($value, $subject->render(OutputFormat::create()));
}
/**
* @test
*/
public function getArrayRepresentationIncludesClassName(): void
{
$subject = new CompoundSelector('a:hover');
$result = $subject->getArrayRepresentation();
self::assertSame('CompoundSelector', $result['class']);
}
/**
* @test
*
* @param non-empty-string $value
*
* @dataProvider provideCompoundSelectorAndSpecificity
*/
public function getArrayRepresentationIncludesValue(string $value): void
{
$subject = new CompoundSelector($value);
$result = $subject->getArrayRepresentation();
self::assertSame($value, $result['value']);
}
}