10-04-2026

This commit is contained in:
Kevin Adametz 2026-04-10 17:18:17 +02:00
parent 4d6b4930b2
commit 4bb89aad8c
836 changed files with 52961 additions and 5950 deletions

View file

@ -0,0 +1,197 @@
<?php
declare(strict_types=1);
namespace App\Services;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMXPath;
/**
* Der Flux-Editor (TipTap) nutzt für Hervorhebungen das Element `mark` (Highlight-Extension).
* Im Frontend wird derselbe optische Zweck oft mit `span.text-secondary` umgesetzt.
* Diese Klasse wandelt beim Laden in den Editor und beim Speichern zwischen beiden Formen um.
*/
final class CmsFluxEditorHtmlTransformer
{
/**
* JSON-Felder, die im CMS-Modal mit flux:editor (Rich-Text) bearbeitet werden.
*
* @var list<string>
*/
public const RICH_TEXT_JSON_FIELDS = [
'description',
'text',
'content',
'help',
'answer',
'quote',
];
public static function toEditor(string $html): string
{
if ($html === '' || ! str_contains($html, 'text-secondary')) {
return $html;
}
return self::spansWithClassToMarks($html);
}
public static function fromEditor(string $html): string
{
if ($html === '' || ! str_contains($html, '<mark')) {
return $html;
}
return self::marksToSecondarySpans($html);
}
/**
* @param array<int, array<string, mixed>|array{_value: string}> $items
* @return array<int, array<string, mixed>|array{_value: string}>
*/
public static function toEditorJsonItems(array $items, bool $isStringArray): array
{
if ($isStringArray) {
return $items;
}
return array_map(function ($item) {
if (! is_array($item)) {
return $item;
}
$out = [];
foreach ($item as $key => $value) {
if (in_array($key, self::RICH_TEXT_JSON_FIELDS, true) && is_string($value)) {
$out[$key] = self::toEditor($value);
} else {
$out[$key] = $value;
}
}
return $out;
}, $items);
}
/**
* @param array<int, array<string, mixed>|array{_value: string}> $items
* @return array<int, array<string, mixed>|array{_value: string}>
*/
public static function fromEditorJsonItems(array $items, bool $isStringArray): array
{
if ($isStringArray) {
return $items;
}
return array_map(function ($item) {
if (! is_array($item)) {
return $item;
}
$out = [];
foreach ($item as $key => $value) {
if (in_array($key, self::RICH_TEXT_JSON_FIELDS, true) && is_string($value)) {
$out[$key] = self::fromEditor($value);
} else {
$out[$key] = $value;
}
}
return $out;
}, $items);
}
private static function spansWithClassToMarks(string $html): string
{
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$wrapped = '<?xml encoding="utf-8"?><div id="cms-flux-root">'.$html.'</div>';
$dom->loadHTML($wrapped, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();
$root = $dom->getElementById('cms-flux-root');
if (! $root instanceof DOMElement) {
return $html;
}
$xpath = new DOMXPath($dom);
$expression = '//span[contains(concat(" ", normalize-space(@class), " "), " text-secondary ")]';
$nodes = [];
foreach ($xpath->query($expression) ?? [] as $node) {
$nodes[] = $node;
}
usort($nodes, fn ($a, $b) => self::nodeDepth($b) <=> self::nodeDepth($a));
foreach ($nodes as $span) {
if (! $span instanceof DOMElement || $span->tagName !== 'span') {
continue;
}
$mark = $dom->createElement('mark');
while ($span->firstChild) {
$mark->appendChild($span->firstChild);
}
$span->parentNode?->replaceChild($mark, $span);
}
return self::extractInnerHtml($dom, $root);
}
private static function marksToSecondarySpans(string $html): string
{
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$wrapped = '<?xml encoding="utf-8"?><div id="cms-flux-root">'.$html.'</div>';
$dom->loadHTML($wrapped, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();
$root = $dom->getElementById('cms-flux-root');
if (! $root instanceof DOMElement) {
return $html;
}
$xpath = new DOMXPath($dom);
$nodes = [];
foreach ($xpath->query('//mark') ?? [] as $node) {
$nodes[] = $node;
}
usort($nodes, fn ($a, $b) => self::nodeDepth($b) <=> self::nodeDepth($a));
foreach ($nodes as $mark) {
if (! $mark instanceof DOMElement) {
continue;
}
$span = $dom->createElement('span');
$span->setAttribute('class', 'text-secondary');
while ($mark->firstChild) {
$span->appendChild($mark->firstChild);
}
$mark->parentNode?->replaceChild($span, $mark);
}
return self::extractInnerHtml($dom, $root);
}
private static function nodeDepth(DOMNode $node): int
{
$d = 0;
while ($node->parentNode) {
$d++;
$node = $node->parentNode;
}
return $d;
}
private static function extractInnerHtml(DOMDocument $dom, DOMElement $root): string
{
$html = '';
foreach ($root->childNodes as $child) {
$html .= $dom->saveHTML($child);
}
return $html;
}
}