<?php

namespace App\Framework\View\Processors;

use App\Framework\View\DomProcessor;
use App\Framework\View\DomWrapper;
use App\Framework\View\RenderContext;
use Dom\HTMLDocument;
use Dom\Node;

final readonly class CommentStripProcessor implements DomProcessor
{

    public function process(DomWrapper $dom, RenderContext $context): DomWrapper
    {
        $this->removeComments($dom->document);
        return $dom;
    }

    private function removeComments(Node $node): void
    {
        // Wir gehen rekursiv durch alle Childnodes
        for ($i = $node->childNodes->length - 1; $i >= 0; $i--) {
            $child = $node->childNodes->item($i);
            if ($child->nodeType === XML_COMMENT_NODE) {
                $node->removeChild($child);
            } else {
                // Rekursion in die nächste Ebene
                $this->removeComments($child);
            }
        }
    }
}
