Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
4 / 14 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PseudoElementSelector | |
28.57% |
4 / 14 |
|
33.33% |
1 / 3 |
31.32 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| specificity | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
6.99 | |||
| toString | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Selector; |
| 6 | |
| 7 | /** |
| 8 | * Pseudo-element selector per Selectors 4 §3.5 / §12: `::before`, `::after`, |
| 9 | * `::marker`, `::first-line`, `::first-letter`, `::placeholder`, |
| 10 | * `::slotted(...)`, `::part(...)`, `::theme(...)`, etc. |
| 11 | * |
| 12 | * Specificity (0, 0, 1) — same as a type selector. Functional pseudo-elements |
| 13 | * like `::slotted()` and `::part()` carry their argument selectors but their |
| 14 | * inner arguments add to specificity as if they were `:is()` (max of inner). |
| 15 | */ |
| 16 | final readonly class PseudoElementSelector extends SimpleSelector |
| 17 | { |
| 18 | public function __construct( |
| 19 | public string $name, |
| 20 | public ?SelectorList $arguments = null, |
| 21 | ) {} |
| 22 | |
| 23 | public function specificity(): Specificity |
| 24 | { |
| 25 | $base = new Specificity(0, 0, 1); |
| 26 | if ($this->arguments === null || $this->arguments->selectors === []) { |
| 27 | return $base; |
| 28 | } |
| 29 | $max = $this->arguments->selectors[0]->specificity(); |
| 30 | foreach (array_slice($this->arguments->selectors, 1) as $sel) { |
| 31 | $max = $max->max($sel->specificity()); |
| 32 | } |
| 33 | return $base->add($max); |
| 34 | } |
| 35 | |
| 36 | public function toString(): string |
| 37 | { |
| 38 | if ($this->arguments !== null) { |
| 39 | $parts = []; |
| 40 | foreach ($this->arguments->selectors as $sel) { |
| 41 | $parts[] = $sel->toString(); |
| 42 | } |
| 43 | return '::' . $this->name . '(' . implode(', ', $parts) . ')'; |
| 44 | } |
| 45 | return '::' . $this->name; |
| 46 | } |
| 47 | } |