Zwei Werkzeuge im Texteditor (Symbol vor dem Absatz, Listenstil); gespeichert werden nur Schlüssel, Bild und Farbe setzt die Ausgabe ein. Kreisfarben aus der Palette, Verwendung wird mitgezählt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
222 lines
8.3 KiB
PHP
222 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms\Widgets\Support;
|
|
|
|
use App\Domain\Catalog\FormattedText;
|
|
use App\Domain\Design\DesignPalette;
|
|
use App\Support\Brand\SymbolCatalog;
|
|
use App\Support\Brand\Symbols;
|
|
use Dom\Element;
|
|
use Dom\HTMLDocument;
|
|
use Dom\Node;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Zeichen im formatierten Text der Website (ADR-69): ein Symbol vor einem Absatz, Nummern im farbigen Kreis, Symbole statt
|
|
* Aufzählungspunkten. Gespeichert werden nur Schlüssel im bereinigten HTML:
|
|
*
|
|
* - `<p data-symbol="haken">` (nur Absätze auf der obersten Ebene des Texts),
|
|
* - `<ol data-marker="circle" data-tone="c1">` (Zahl im Kreis, Farbe aus der Palette oder eine der Markenfarben),
|
|
* - `<ul data-marker="symbol" data-symbol="haken">`.
|
|
*
|
|
* Was nicht zu einem bekannten Schlüssel passt, entfällt beim Speichern. Bilder und Farben stehen nie im gespeicherten
|
|
* Text: {@see self::decorate()} setzt sie erst bei der Ausgabe ein, die Farbe wird dabei aus der Palette gelesen. Eine
|
|
* Farbe, die es nicht mehr gibt, fällt auf die Vorgabe zurück.
|
|
*/
|
|
final class TextMarkers
|
|
{
|
|
/** Attribute, die das Webprofil der Bereinigung an Absätzen und Listen zulässt. */
|
|
public const PARAGRAPH_ATTRIBUTES = ['data-symbol'];
|
|
|
|
public const LIST_ATTRIBUTES = ['data-marker', 'data-symbol', 'data-tone'];
|
|
|
|
/** Zeichen der Listen: Nummern im Kreis (geordnet) und Symbol (Aufzählung). */
|
|
public const MARKER_CIRCLE = 'circle';
|
|
|
|
public const MARKER_SYMBOL = 'symbol';
|
|
|
|
/** Vorgabefarbe, wenn eine Farbe fehlt oder nicht mehr besteht. */
|
|
public const DEFAULT_TONE = 'dark';
|
|
|
|
/** Die Markenflächen, die neben der Palette als Farbe der Kreise angeboten werden. */
|
|
private const BRAND_TONES = ['dark', 'accent'];
|
|
|
|
/**
|
|
* Ein gültiger Symbolschlüssel oder null.
|
|
*/
|
|
public static function symbol(mixed $value): ?string
|
|
{
|
|
return is_string($value) && in_array($value, SymbolCatalog::keys(), true) ? $value : null;
|
|
}
|
|
|
|
/**
|
|
* Farben für die Kreise: Schlüssel => Anzeigename, Fläche, Schrift. Zuerst die Markenfarben, dann die Palette.
|
|
*
|
|
* @return array<string, array{label: string, bg: string, fg: string}>
|
|
*/
|
|
public static function tones(): array
|
|
{
|
|
$tones = [];
|
|
|
|
foreach (DesignPalette::builtIns() as $built) {
|
|
if (in_array($built['key'], self::BRAND_TONES, true)) {
|
|
$tones[$built['key']] = ['label' => $built['label'], 'bg' => $built['bg'], 'fg' => $built['fg']];
|
|
}
|
|
}
|
|
|
|
foreach (DesignPalette::slots() as $slot) {
|
|
$tones[$slot['id']] = ['label' => $slot['name'], 'bg' => $slot['bg'], 'fg' => $slot['fg']];
|
|
}
|
|
|
|
return $tones;
|
|
}
|
|
|
|
/**
|
|
* Die gültigen Attribute eines Absatzes oder einer Liste als Text für das Anfangs-Tag (mit führendem Leerzeichen);
|
|
* leer, wenn nichts Gültiges übrig bleibt. Die Reihenfolge ist fest, damit die kanonische Form stabil bleibt.
|
|
*/
|
|
public static function attributes(string $tag, Element $node): string
|
|
{
|
|
if ($tag === 'p') {
|
|
// Das Symbol steht nur vor einem Absatz des Texts, nicht in Liste, Zitat oder Tabelle.
|
|
$symbol = $node->parentNode !== null && strtolower($node->parentNode->nodeName) === 'body'
|
|
? self::symbol($node->getAttribute('data-symbol'))
|
|
: null;
|
|
|
|
return $symbol === null ? '' : ' data-symbol="'.$symbol.'"';
|
|
}
|
|
|
|
$marker = (string) $node->getAttribute('data-marker');
|
|
$symbol = self::symbol($node->getAttribute('data-symbol'));
|
|
$tone = (string) $node->getAttribute('data-tone');
|
|
$tone = array_key_exists($tone, self::tones()) ? $tone : null;
|
|
|
|
if ($tag === 'ol' && $marker === self::MARKER_CIRCLE) {
|
|
return ' data-marker="circle"'.($tone !== null ? ' data-tone="'.$tone.'"' : '');
|
|
}
|
|
|
|
if ($tag === 'ul' && $marker === self::MARKER_SYMBOL && $symbol !== null) {
|
|
return ' data-marker="symbol" data-symbol="'.$symbol.'"';
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Führt die Attribute eines bereits bereinigten HTML auf gültige Werte zurück (für Texte, die nicht die kanonische
|
|
* Form von {@see FormattedText} durchlaufen, zum Beispiel „Bild und Text“).
|
|
*/
|
|
public static function clean(string $html): string
|
|
{
|
|
if (! str_contains($html, 'data-')) {
|
|
return $html;
|
|
}
|
|
|
|
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
|
|
|
foreach ($document->querySelectorAll('p, ul, ol') as $node) {
|
|
$tag = strtolower($node->nodeName);
|
|
$allowed = [];
|
|
|
|
if (preg_match_all('/ ([a-z-]+)="([^"]*)"/', self::attributes($tag, $node), $found, PREG_SET_ORDER) > 0) {
|
|
foreach ($found as $pair) {
|
|
$allowed[$pair[1]] = $pair[2];
|
|
}
|
|
}
|
|
|
|
foreach (['data-marker', 'data-symbol', 'data-tone'] as $name) {
|
|
$node->removeAttribute($name);
|
|
}
|
|
|
|
foreach ($allowed as $name => $value) {
|
|
$node->setAttribute($name, $value);
|
|
}
|
|
}
|
|
|
|
return trim($document->body->innerHTML);
|
|
}
|
|
|
|
/**
|
|
* Setzt die Zeichen bei der Ausgabe ein: Symbol vor dem Absatz, Kreis um die Nummer (Farbe aus der Palette), Symbol
|
|
* statt Aufzählungspunkt. Ein Text ohne Zeichen bleibt unverändert.
|
|
*/
|
|
public static function decorate(string $html): string
|
|
{
|
|
if (! str_contains($html, 'data-symbol') && ! str_contains($html, 'data-marker')) {
|
|
return $html;
|
|
}
|
|
|
|
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
|
$tones = self::tones();
|
|
|
|
foreach (iterator_to_array($document->querySelectorAll('p[data-symbol]')) as $paragraph) {
|
|
$symbol = self::symbol($paragraph->getAttribute('data-symbol'));
|
|
|
|
if ($symbol === null) {
|
|
continue;
|
|
}
|
|
|
|
$paragraph->setAttribute('class', 'has-symbol');
|
|
$paragraph->insertBefore(self::symbolNode($document, $symbol, 'text-symbol'), $paragraph->firstChild);
|
|
}
|
|
|
|
foreach (iterator_to_array($document->querySelectorAll('ol[data-marker="circle"]')) as $list) {
|
|
$tone = $tones[(string) $list->getAttribute('data-tone')] ?? $tones[self::DEFAULT_TONE] ?? ['bg' => '#032551', 'fg' => '#f4f6f8'];
|
|
$list->setAttribute('class', 'text-list-circle');
|
|
$list->setAttribute('style', '--tone-bg: '.$tone['bg'].'; --tone-fg: '.$tone['fg']);
|
|
}
|
|
|
|
foreach (iterator_to_array($document->querySelectorAll('ul[data-marker="symbol"]')) as $list) {
|
|
$symbol = self::symbol($list->getAttribute('data-symbol'));
|
|
|
|
if ($symbol === null) {
|
|
continue;
|
|
}
|
|
|
|
$list->setAttribute('class', 'text-list-symbol');
|
|
|
|
foreach ($list->childNodes as $item) {
|
|
if ($item->nodeType === XML_ELEMENT_NODE && strtolower($item->nodeName) === 'li') {
|
|
$item->insertBefore(self::symbolNode($document, $symbol, 'text-list-symbol__mark'), $item->firstChild);
|
|
}
|
|
}
|
|
}
|
|
|
|
return trim($document->body->innerHTML);
|
|
}
|
|
|
|
/**
|
|
* Die Kennungen der Palettenfarben, auf die ein Widget in seinen Texten verweist (je Farbe einmal).
|
|
*
|
|
* @param array<mixed> $data
|
|
* @return list<string>
|
|
*/
|
|
public static function tonesIn(array $data): array
|
|
{
|
|
$found = [];
|
|
|
|
array_walk_recursive($data, function (mixed $value) use (&$found): void {
|
|
if (is_string($value) && str_contains($value, 'data-tone') && preg_match_all('/data-tone="([a-z0-9]+)"/', $value, $matches) > 0) {
|
|
array_push($found, ...$matches[1]);
|
|
}
|
|
});
|
|
|
|
return array_values(array_unique($found));
|
|
}
|
|
|
|
private static function symbolNode(HTMLDocument $document, string $symbol, string $class): Node
|
|
{
|
|
$span = $document->createElement('span');
|
|
$span->setAttribute('class', $class);
|
|
$span->setAttribute('aria-hidden', 'true');
|
|
|
|
try {
|
|
$span->innerHTML = (string) Symbols::symbol($symbol);
|
|
} catch (Throwable) {
|
|
// Ein Symbol, das es nicht mehr gibt, lässt nur den Platz leer; der Text bleibt lesbar.
|
|
}
|
|
|
|
return $span;
|
|
}
|
|
}
|