*/ private array $root = []; /** @var list element 为 null 表示被丢掉的标签 */ private array $stack = []; public function add(Node $node): void { foreach ($this->stack as $frame) { if ($frame['element'] === null) { return; } } $top = end($this->stack); if ($top === false) { $this->root[] = $node; } else { $top['element']->children[] = $node; } } public function tag(string $literal): void { $tag = HtmlFragment::tagLiteral($literal); if ($tag['kind'] === 'close') { $this->close($tag['tag']); return; } if ($tag['kind'] !== 'open') { return; } $element = HtmlFragment::element($tag['tag'], $tag['attrs']); $void = $tag['selfClosing'] || in_array($tag['tag'], HtmlPolicy::VOID, true); if ($element === false) { if (! $void) { $this->stack[] = ['tag' => $tag['tag'], 'element' => null]; } return; } $this->add($element); if (! $void) { $this->stack[] = ['tag' => $tag['tag'], 'element' => $element]; } } private function close(string $tag): void { for ($i = count($this->stack) - 1; $i >= 0; $i--) { if ($this->stack[$i]['tag'] === $tag) { $this->stack = array_slice($this->stack, 0, $i); return; } } } /** * @return list */ public function nodes(): array { return $this->root; } }