Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AttrFunction | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toCss | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Value; |
| 6 | |
| 7 | /** |
| 8 | * `attr(<attr-name> [<type-or-unit>], <fallback>?)` per CSS Values |
| 9 | * 5 §11. Resolves to the named attribute's value at computed-value |
| 10 | * time, optionally coerced into the declared type and falling back |
| 11 | * to the supplied default when the attribute is missing. |
| 12 | * |
| 13 | * Modern syntax (`attr(<attr-name> type(<syntax>), <fallback>)`) |
| 14 | * stores the syntax string verbatim in `$typeOrUnit`; the cascade |
| 15 | * handles either form. |
| 16 | * |
| 17 | * content: attr(data-name); |
| 18 | * content: attr(data-name string); |
| 19 | * content: attr(data-name string, "(none)"); |
| 20 | * width: attr(data-w px, 100px); |
| 21 | * color: attr(data-c color, currentcolor); |
| 22 | */ |
| 23 | final readonly class AttrFunction extends Value |
| 24 | { |
| 25 | public function __construct( |
| 26 | public string $attributeName, |
| 27 | /** |
| 28 | * Optional type/unit hint. One of CSS Values 5's named |
| 29 | * types (`string`, `number`, `integer`, `length`, `angle`, |
| 30 | * `time`, `frequency`, `percentage`, `color`, `url`, `ident`) |
| 31 | * or a CSS unit token (`px`, `em`, etc.) — stored verbatim. |
| 32 | * Null when the author wrote the bare attr(<name>) form. |
| 33 | */ |
| 34 | public ?string $typeOrUnit = null, |
| 35 | /** |
| 36 | * Optional fallback expression when the attribute is |
| 37 | * missing or its value can't be coerced into the declared |
| 38 | * type. |
| 39 | */ |
| 40 | public ?Value $fallback = null, |
| 41 | ) {} |
| 42 | |
| 43 | public function toCss(): string |
| 44 | { |
| 45 | $parts = [$this->attributeName]; |
| 46 | if ($this->typeOrUnit !== null) { |
| 47 | $parts[] = $this->typeOrUnit; |
| 48 | } |
| 49 | $head = 'attr(' . implode(' ', $parts); |
| 50 | return $this->fallback !== null |
| 51 | ? $head . ', ' . $this->fallback->toCss() . ')' |
| 52 | : $head . ')'; |
| 53 | } |
| 54 | } |