Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| HTMLSlotElement | |
0.00% |
0 / 15 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| assignedNodes | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| assignedElements | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| setAssignedNodes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Html\Dom; |
| 6 | |
| 7 | /** |
| 8 | * The <slot> element. Acts as a placeholder in a shadow tree that gets filled |
| 9 | * by light-DOM children of the host during flat-tree composition. |
| 10 | * |
| 11 | * The parser creates instances of this class rather than the generic Element |
| 12 | * for the "slot" tag in the HTML namespace, so layout code can rely on |
| 13 | * `instanceof HTMLSlotElement` instead of name-matching strings. |
| 14 | */ |
| 15 | final class HTMLSlotElement extends Element |
| 16 | { |
| 17 | public ?string $name { |
| 18 | get => $this->getAttribute('name'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * The light-DOM nodes assigned to this slot. Populated by the slot |
| 23 | * distribution algorithm during flat-tree composition; empty until |
| 24 | * composition runs. |
| 25 | * |
| 26 | * For Phase 1B this returns the manual-assignment list set via |
| 27 | * setManuallyAssignedNodes(). The Named-mode auto-assignment that scans |
| 28 | * the host's children for slot="name" matches lives in the layout |
| 29 | * engine (Phase 1E/1F) where the flat tree is composed. |
| 30 | * |
| 31 | * @return list<Node> |
| 32 | */ |
| 33 | public function assignedNodes(bool $flatten = false): array |
| 34 | { |
| 35 | $nodes = $this->manuallyAssigned; |
| 36 | if (!$flatten) { |
| 37 | return $nodes; |
| 38 | } |
| 39 | // Flatten: replace any nested slots with their own assigned nodes. |
| 40 | $out = []; |
| 41 | foreach ($nodes as $n) { |
| 42 | if ($n instanceof self) { |
| 43 | foreach ($n->assignedNodes(true) as $inner) { |
| 44 | $out[] = $inner; |
| 45 | } |
| 46 | } else { |
| 47 | $out[] = $n; |
| 48 | } |
| 49 | } |
| 50 | return $out; |
| 51 | } |
| 52 | |
| 53 | /** @return list<Element> assignedNodes filtered to Elements */ |
| 54 | public function assignedElements(bool $flatten = false): array |
| 55 | { |
| 56 | return array_values(array_filter( |
| 57 | $this->assignedNodes($flatten), |
| 58 | static fn(Node $n): bool => $n instanceof Element, |
| 59 | )); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * For manual slot assignment (SlotAssignment::Manual). Phase 1E's flat- |
| 64 | * tree composer writes the named-mode assignments through the same |
| 65 | * setter; the parser never calls it directly. |
| 66 | * |
| 67 | * @param list<Node> $nodes |
| 68 | */ |
| 69 | public function setAssignedNodes(array $nodes): void |
| 70 | { |
| 71 | $this->manuallyAssigned = $nodes; |
| 72 | } |
| 73 | |
| 74 | /** @var list<Node> */ |
| 75 | private array $manuallyAssigned = []; |
| 76 | } |