<?php

declare(strict_types=1);

namespace App\Framework\View\Processors;

use App\Framework\Template\Processing\StringProcessor;
use App\Framework\View\RenderContext;

final class VoidElementsSelfClosingProcessor implements StringProcessor
{
    // Liste der HTML5-Void-Elemente
    private const array VOID_ELEMENTS = [
        'area', 'base', 'br', 'col', 'embed', 'hr', 'img',
        'input', 'link', 'meta', 'source', 'track', 'wbr',
    ];

    public function process(string $html, RenderContext $context): string
    {
        $pattern = '#<(' . implode('|', self::VOID_ELEMENTS) . ')(\s[^>/]*)?(?<!/)>#i';

        return preg_replace_callback($pattern, function ($m) {
            $tag = $m[1];
            $attrs = $m[2] ?? '';

            return "<$tag$attrs />";
        }, $html);
    }
}
