Setze Symbole vor Absätze, Nummern in Kreise und Symbole statt Aufzählungspunkte im Text (ADR-69)
CI Feature / test (push) Successful in 7m17s
Deploy Test / test (push) Successful in 6m48s
Deploy Test / deploy (push) Successful in 41s

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>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-30 18:58:10 +01:00
1 parent 67764c3e4f
commit e904b500fb
24 files changed
+814 -14

No files matched your search

+6 -5
View File
@@ -12,7 +12,7 @@
final class RichText
{
/** Werkzeugleiste des Editors; muss zur Erlaubnisliste unten passen. */
public const TOOLBAR = [['bold', 'italic', 'link', 'interneSeite'], ['bulletList', 'orderedList'], ['undo', 'redo']];
public const TOOLBAR = [['bold', 'italic', 'link', 'interneSeite'], ['bulletList', 'orderedList'], ['absatzSymbol', 'listenStil'], ['undo', 'redo']];
private static ?HtmlSanitizer $sanitizer = null;
@@ -22,7 +22,8 @@ public static function sanitize(?string $html): string
return '';
}
return trim(self::sanitizer()->sanitize($html));
// Zeichen vor Absätzen und in Listen (ADR-69) bleiben nur mit bekannten Schlüsseln erhalten.
return TextMarkers::clean(trim(self::sanitizer()->sanitize($html)));
}
/**
@@ -60,14 +61,14 @@ private static function sanitizer(): HtmlSanitizer
{
return self::$sanitizer ??= new HtmlSanitizer(
(new HtmlSanitizerConfig)
->allowElement('p')
->allowElement('p', TextMarkers::PARAGRAPH_ATTRIBUTES)
->allowElement('br')
->allowElement('strong')
->allowElement('b')
->allowElement('em')
->allowElement('i')
->allowElement('ul')
->allowElement('ol')
->allowElement('ul', TextMarkers::LIST_ATTRIBUTES)
->allowElement('ol', TextMarkers::LIST_ATTRIBUTES)
->allowElement('li')
->allowElement('a', ['href'])
->allowLinkSchemes(['http', 'https', 'mailto'])
@@ -0,0 +1,221 @@
<?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;
}
}
@@ -9,11 +9,13 @@
use App\Domain\Cms\Widgets\Support\PremiumFields;
use App\Domain\Cms\Widgets\Support\PublicMedia;
use App\Domain\Cms\Widgets\Support\RichText;
use App\Domain\Cms\Widgets\Support\TextMarkers;
use App\Domain\Cms\Widgets\Support\WebsiteImage;
use App\Domain\Cms\Widgets\Support\WidgetAppearance;
use App\Domain\Cms\Widgets\WidgetErrors;
use App\Domain\Cms\Widgets\WidgetType;
use App\Filament\Support\InternalLinkPlugin;
use App\Filament\Support\TextMarksPlugin;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ViewField;
@@ -236,7 +238,7 @@ public function dataFields(): array
->label('Text')
->helperText('Leer lassen, wenn nur das Bild gezeigt werden soll. Ohne Text, Überschrift, Label und Schaltflächen füllt das Bild die ganze Breite.')
->live(onBlur: true)
->plugins([new InternalLinkPlugin])
->plugins([new InternalLinkPlugin, new TextMarksPlugin])
->toolbarButtons(RichText::TOOLBAR),
PremiumFields::ctaFields(),
PremiumFields::altField(),
@@ -348,7 +350,7 @@ public function resolve(array $widget, RenderContext $context): array
'video' => $video,
'hasCopy' => $hasCopy,
'imageShare' => self::imageShare($widget['settings'] ?? []),
'body' => RichText::sanitize(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : ''),
'body' => TextMarkers::decorate(RichText::sanitize(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : '')),
'actions' => PremiumFields::actions($widget['data']),
'focus' => PremiumFields::focus($widget['data'], 64, 43),
// Eine gewählte Farbe aus der Palette (ADR-68) hat Vorrang vor „Hintergrund des Widgets“.
@@ -6,6 +6,7 @@
use App\Domain\Cms\Widgets\RenderContext;
use App\Domain\Cms\Widgets\Support\HeadingAccent;
use App\Domain\Cms\Widgets\Support\RichText;
use App\Domain\Cms\Widgets\Support\TextMarkers;
use App\Domain\Cms\Widgets\WidgetErrors;
use App\Domain\Cms\Widgets\WidgetType;
use App\Filament\Support\RichField;
@@ -135,6 +136,9 @@ public static function render(mixed $body): string
{
$html = FormattedText::html(is_string($body) ? $body : '', null, true);
// Symbole vor Absätzen, Kreise um Nummern und Symbole statt Punkten setzt erst die Ausgabe ein (ADR-69).
$html = TextMarkers::decorate($html);
// Die Ausrichtung steht gespeichert als Stil (so liest der Editor sie); auf der Seite wird sie eine Klasse.
return str_replace(
['<table>', '</table>', ' style="text-align: center"', ' style="text-align: end"'],