Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
10 / 12 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AttributeSelector | |
83.33% |
10 / 12 |
|
66.67% |
2 / 3 |
8.30 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| specificity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toString | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
6.29 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Selector; |
| 6 | |
| 7 | /** |
| 8 | * Attribute selector per Selectors 4 ยง6.5: `[name op value flag]`. |
| 9 | * |
| 10 | * Specificity (0, 1, 0). The optional ASCII-case-insensitive flag `i` (or |
| 11 | * case-sensitive `s`) modifies the value comparison. |
| 12 | */ |
| 13 | final readonly class AttributeSelector extends SimpleSelector |
| 14 | { |
| 15 | public function __construct( |
| 16 | public string $name, |
| 17 | public AttributeMatchType $matchType = AttributeMatchType::Exists, |
| 18 | public ?string $value = null, |
| 19 | public ?string $namespacePrefix = null, |
| 20 | public bool $caseInsensitive = false, |
| 21 | ) {} |
| 22 | |
| 23 | public function specificity(): Specificity |
| 24 | { |
| 25 | return new Specificity(0, 1, 0); |
| 26 | } |
| 27 | |
| 28 | public function toString(): string |
| 29 | { |
| 30 | $name = $this->namespacePrefix !== null |
| 31 | ? $this->namespacePrefix . '|' . $this->name |
| 32 | : $this->name; |
| 33 | if ($this->matchType === AttributeMatchType::Exists) { |
| 34 | return '[' . $name . ']'; |
| 35 | } |
| 36 | $value = $this->value ?? ''; |
| 37 | $needsQuoting = preg_match('/[^A-Za-z0-9_-]/', $value) === 1 || $value === ''; |
| 38 | $quoted = $needsQuoting ? '"' . str_replace('"', '\\"', $value) . '"' : $value; |
| 39 | $flag = $this->caseInsensitive ? ' i' : ''; |
| 40 | return '[' . $name . $this->matchType->value . $quoted . $flag . ']'; |
| 41 | } |
| 42 | } |