diff --git a/app/Domain/Cms/Sections/PageSections.php b/app/Domain/Cms/Sections/PageSections.php index 5fe2883..16446e0 100644 --- a/app/Domain/Cms/Sections/PageSections.php +++ b/app/Domain/Cms/Sections/PageSections.php @@ -18,6 +18,13 @@ * Version 2 (Abstände): oben, unten und Widget-Abstand sind drei unabhängige Werte. Hüllen der Version 1 werden beim * Lesen übersetzt (SectionStyle::migrateSpacing), ohne die Darstellung zu ändern; gespeicherte Revisionen bleiben. * + * Optionale Felder des Abschnitts (abwärtskompatibel, fehlen in älteren Revisionen): + * - name: interner Name zur Orientierung im Seitenaufbau (nie öffentlich), höchstens 80 Zeichen; + * - anchor: Sprungmarke, wird als Element-Kennung des Abschnitts ausgegeben (Link „#marke“ oder „/pfad#marke“), + * je Seite eindeutig, nur Kleinbuchstaben, Ziffern und Bindestriche; + * - hidden: true blendet den Abschnitt auf der Website aus; er bleibt im Entwurf und in den Revisionen erhalten. + * Ältere Programmstände kennen „hidden“ nicht und würden einen ausgeblendeten Abschnitt zeigen (siehe Release-Vorbereitung). + * * Jeder Slot enthält höchstens ein Widget aus dem zentralen Widget-Register. Abschnitte sind nicht verschachtelbar. * Revisionen ohne Abschnitte (Bestand vor dieser Fassung) werden beim Lesen als einspaltige Abschnitte gelesen. */ @@ -29,6 +36,20 @@ class PageSections public const MAX_WIDGETS = 60; + public const MAX_NAME = 80; + + public const MAX_ANCHOR = 60; + + /** + * Kennungen, die im Seitengerüst schon vergeben sind (Hauptbereich, Navigation, Fuß) oder als Vorgabe dienen. + * Eine Sprungmarke darf sie nicht wiederholen. + * + * @var list + */ + public const RESERVED_ANCHORS = ['inhalt', 'main', 'top', 'site-nav', 'fuss-themen', 'mb-account-menu']; + + public const ANCHOR_PATTERN = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/'; + public function __construct(private readonly WidgetRegistry $widgets) {} /** @@ -36,7 +57,7 @@ public function __construct(private readonly WidgetRegistry $widgets) {} * * @param array|null $sections gespeicherte Abschnitte (Spalte sections) * @param array|null $blocks flache Bausteinliste (Spalte blocks), nur ohne Abschnitte verwendet - * @return list, slots: list|null>}> + * @return list, slots: list|null>}> */ public function read(?array $sections, ?array $blocks = null): array { @@ -59,7 +80,7 @@ public function read(?array $sections, ?array $blocks = null): array * „sections.N.layout“, „sections.N.style.image“, „sections.N.slots“, „sections.N.slots.M.data.feld“. * * @param array $content - * @return list, slots: list|null>}> + * @return list, slots: list|null>}> */ public function normalize(array $content): array { @@ -75,6 +96,7 @@ public function normalize(array $content): array $sections = []; $seenSectionIds = []; $seenWidgetIds = []; + $seenAnchors = []; $widgetCount = 0; // Eingaben mit Abständen der Version 1 (zum Beispiel ältere Aufrufer) werden wie beim Lesen übersetzt. @@ -96,7 +118,7 @@ public function normalize(array $content): array $item['style'] = $migrated[$index]['style'] ?? ($item['style'] ?? []); } - $sections[] = $this->normalizeSection($item, $index, $errors, $seenSectionIds, $seenWidgetIds, $widgetCount); + $sections[] = $this->normalizeSection($item, $index, $errors, $seenSectionIds, $seenWidgetIds, $widgetCount, $seenAnchors); } if ($widgetCount > self::MAX_WIDGETS) { @@ -164,12 +186,14 @@ public function mediaPaths(array $sections): array */ public function pageTitle(array $sections): ?string { - return $this->widgets->pageTitle($this->flatten($sections)); + // Ein ausgeblendeter Abschnitt liefert keinen Seitentitel; seine Medien bleiben dagegen erfasst (mediaPaths), + // damit ihre Bilder nicht als ungenutzt gelten. + return $this->widgets->pageTitle($this->flatten(array_values(array_filter($sections, fn (array $section): bool => ! ($section['hidden'] ?? false))))); } /** * @param array $blocks - * @return list, slots: list|null>}> + * @return list, slots: list|null>}> */ private function wrapWidgets(array $blocks): array { @@ -179,6 +203,9 @@ private function wrapWidgets(array $blocks): array $sections[] = [ 'id' => null, 'v' => self::VERSION, + 'name' => null, + 'anchor' => null, + 'hidden' => false, 'layout' => SectionLayouts::DEFAULT, // Bestand vor dem Raster stand in einem durchgehenden Blatt: kompakte Abstände, zwischen den Widgets // nur einmal (oben kein Abstand ab dem zweiten), wie bisher. @@ -192,7 +219,7 @@ private function wrapWidgets(array $blocks): array /** * @param array $section - * @return array{id: ?string, v: int, layout: string, style: array, slots: list|null>} + * @return array{id: ?string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array, slots: list|null>} */ private function upcastSection(array $section): array { @@ -210,6 +237,9 @@ private function upcastSection(array $section): array return [ 'id' => is_string($section['id'] ?? null) ? $section['id'] : null, 'v' => self::VERSION, + 'name' => self::cleanName($section['name'] ?? null), + 'anchor' => self::cleanAnchor($section['anchor'] ?? null), + 'hidden' => self::isHidden($section['hidden'] ?? false), 'layout' => $layout, 'style' => SectionStyle::fromStored($section['style'] ?? []), 'slots' => $slots, @@ -221,9 +251,9 @@ private function upcastSection(array $section): array * @param array> $errors * @param array $seenSectionIds * @param array $seenWidgetIds - * @return array{id: string, v: int, layout: string, style: array, slots: list|null>} + * @return array{id: string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array, slots: list|null>} */ - private function normalizeSection(array $section, int $index, array &$errors, array &$seenSectionIds, array &$seenWidgetIds, int &$widgetCount): array + private function normalizeSection(array $section, int $index, array &$errors, array &$seenSectionIds, array &$seenWidgetIds, int &$widgetCount, array &$seenAnchors): array { $prefix = "sections.$index"; @@ -294,15 +324,90 @@ private function normalizeSection(array $section, int $index, array &$errors, ar } $seenSectionIds[$id] = true; + [$name, $anchor] = $this->normalizeLabels($section, $prefix, $errors, $seenAnchors); + return [ 'id' => $id, 'v' => self::VERSION, + 'name' => $name, + 'anchor' => $anchor, + 'hidden' => self::isHidden($section['hidden'] ?? false), 'layout' => $layout, 'style' => $style, 'slots' => $slots, ]; } + /** + * Name und Sprungmarke eines Abschnitts. Fehler: „sections.N.name“ und „sections.N.anchor“. + * + * @param array $section + * @param array> $errors + * @param array $seenAnchors + * @return array{0: ?string, 1: ?string} + */ + private function normalizeLabels(array $section, string $prefix, array &$errors, array &$seenAnchors): array + { + $name = null; + $rawName = $section['name'] ?? null; + + if (is_string($rawName) && trim($rawName) !== '') { + $name = self::cleanName($rawName); + + if ($name === null) { + $errors["$prefix.name"] = ['Der Name darf höchstens '.self::MAX_NAME.' Zeichen lang sein.']; + } + } + + $anchor = null; + $rawAnchor = $section['anchor'] ?? null; + + if (is_string($rawAnchor) && trim($rawAnchor) !== '') { + $candidate = mb_strtolower(trim($rawAnchor)); + + if (strlen($candidate) > self::MAX_ANCHOR || preg_match(self::ANCHOR_PATTERN, $candidate) !== 1) { + $errors["$prefix.anchor"] = ['Eine Sprungmarke besteht aus Kleinbuchstaben, Ziffern und einzelnen Bindestrichen (höchstens '.self::MAX_ANCHOR.' Zeichen), zum Beispiel „anmeldung“.']; + } elseif (in_array($candidate, self::RESERVED_ANCHORS, true) || str_starts_with($candidate, 'bereich-')) { + $errors["$prefix.anchor"] = ['Diese Sprungmarke ist im Seitengerüst schon vergeben. Bitte eine andere wählen.']; + } elseif (isset($seenAnchors[$candidate])) { + $errors["$prefix.anchor"] = ['Diese Sprungmarke gibt es auf der Seite schon. Jede Sprungmarke darf nur einmal vorkommen.']; + } else { + $seenAnchors[$candidate] = true; + $anchor = $candidate; + } + } elseif ($rawAnchor !== null && ! is_string($rawAnchor)) { + $errors["$prefix.anchor"] = ['Die Sprungmarke ist ungültig.']; + } + + return [$name, $anchor]; + } + + /** Interner Name: Leerraum vereinheitlicht, ohne Steuerzeichen; leer oder über 80 Zeichen ergibt null. */ + public static function cleanName(mixed $value): ?string + { + if (! is_string($value)) { + return null; + } + + $name = trim((string) preg_replace('/\s+/u', ' ', (string) preg_replace('/[\x00-\x1F\x7F]/u', ' ', $value))); + + return $name === '' || mb_strlen($name) > self::MAX_NAME ? null : $name; + } + + /** Lesepfad: eine gespeicherte Sprungmarke nur, wenn sie der Form entspricht. */ + public static function cleanAnchor(mixed $value): ?string + { + return is_string($value) && strlen($value) <= self::MAX_ANCHOR && preg_match(self::ANCHOR_PATTERN, $value) === 1 + && ! in_array($value, self::RESERVED_ANCHORS, true) && ! str_starts_with($value, 'bereich-') + ? $value + : null; + } + + public static function isHidden(mixed $value): bool + { + return $value === true || $value === 1 || $value === '1' || $value === 'true'; + } + /** * @param array $style * @param array> $errors diff --git a/app/Domain/Cms/Sections/SectionRenderer.php b/app/Domain/Cms/Sections/SectionRenderer.php index 69f6189..2ae5bd0 100644 --- a/app/Domain/Cms/Sections/SectionRenderer.php +++ b/app/Domain/Cms/Sections/SectionRenderer.php @@ -17,6 +17,9 @@ * - Ein Abschnitt ohne darstellbares Widget entfällt samt Hintergrund und Trenner. * - Das erste dargestellte Widget der Seite ist der Seiteneinstieg (Überschrift erster Ordnung). * - Trenner stehen zwischen Abschnitten, nie nach dem letzten. + * - Ein ausgeblendeter Abschnitt („hidden“) erscheint nie auf der Website und zählt weder für das erste Widget noch für + * Trenner. Nur die Vorschau zeigt an seiner Stelle einen schmalen Hinweis (ohne Inhalt). + * - Eine Sprungmarke („anchor“) ist die Element-Kennung des Abschnitts; ohne Sprungmarke gilt „bereich-“. */ class SectionRenderer { @@ -31,6 +34,14 @@ public function renderAll(array $sections, RenderContext $context): HtmlString $first = true; foreach ($sections as $section) { + if ($section['hidden'] ?? false) { + if ($context->preview) { + $rendered[] = ['note' => $this->hiddenNote($section)]; + } + + continue; + } + $cells = []; $shown = false; $units = SectionLayouts::units($section['layout']); @@ -54,13 +65,28 @@ public function renderAll(array $sections, RenderContext $context): HtmlString } $html = ''; - $last = count($rendered) - 1; + $last = -1; + + foreach ($rendered as $position => $item) { + if (isset($item['section'])) { + $last = $position; + } + } + + $index = 0; + + foreach ($rendered as $position => $item) { + if (! isset($item['section'])) { + $html .= $item['note']; + + continue; + } - foreach ($rendered as $index => $item) { $html .= $this->renderSection($item['section'], $item['cells'], $index, $context); + $index++; $divider = $item['section']['style']['divider'] ?? 'none'; - if ($index < $last && in_array($divider, ['line', 'accent'], true)) { + if ($position < $last && in_array($divider, ['line', 'accent'], true)) { $html .= ''; } } @@ -68,6 +94,18 @@ public function renderAll(array $sections, RenderContext $context): HtmlString return new HtmlString($html); } + /** + * Hinweis der Vorschau an der Stelle eines ausgeblendeten Abschnitts. + * + * @param array $section + */ + private function hiddenNote(array $section): string + { + $name = is_string($section['name'] ?? null) && $section['name'] !== '' ? '„'.$section['name'].'“ ' : ''; + + return '
Abschnitt '.e($name).'ist für die Website ausgeblendet und erscheint hier nur als Hinweis.
'; + } + /** * @param array $section * @param list $cells alle Plätze des Rasters in ihrer Reihenfolge; html '' = leer @@ -106,7 +144,11 @@ private function renderSection(array $section, array $cells, int $index, RenderC $classes[] = $scope; } - $id = is_string($section['id'] ?? null) ? ' id="bereich-'.e($section['id']).'"' : ''; + // Die Sprungmarke ist die Kennung, die ein Link „#marke“ anspringt; ohne sie bleibt die bisherige Kennung. + $anchor = PageSections::cleanAnchor($section['anchor'] ?? null); + $id = $anchor !== null + ? ' id="'.e($anchor).'"' + : (is_string($section['id'] ?? null) ? ' id="bereich-'.e($section['id']).'"' : ''); $html = '
'; if ($hasImage) { diff --git a/app/Domain/Cms/Widgets/Support/SafeUrl.php b/app/Domain/Cms/Widgets/Support/SafeUrl.php index a385792..1d0875c 100644 --- a/app/Domain/Cms/Widgets/Support/SafeUrl.php +++ b/app/Domain/Cms/Widgets/Support/SafeUrl.php @@ -6,7 +6,8 @@ use Illuminate\Contracts\Validation\ValidationRule; /** - * Zulässige Linkziele in redaktionellen Inhalten: Seitenpfade ("/...") oder http(s)-Adressen. + * Zulässige Linkziele in redaktionellen Inhalten: Seitenpfade ("/...", auch mit "#marke"), Sprungmarken auf der + * eigenen Seite ("#marke") oder http(s)-Adressen. Eine Sprungmarke besteht aus Buchstaben, Ziffern, Bindestrich und Unterstrich. */ final class SafeUrl implements ValidationRule { @@ -16,6 +17,10 @@ public static function isSafe(mixed $url): bool return false; } + if (str_starts_with($url, '#')) { + return preg_match('/^#[A-Za-z0-9_-]{1,80}$/', $url) === 1; + } + $isRelative = str_starts_with($url, '/') && ! str_starts_with($url, '//') && ! str_starts_with($url, '/\\'); $isAbsolute = preg_match('#^https?://[^/\s]+#i', $url) === 1; @@ -24,7 +29,7 @@ public static function isSafe(mixed $url): bool public static function message(): string { - return 'Erlaubt sind Seitenpfade (beginnend mit /) oder http(s)-Adressen.'; + return 'Erlaubt sind Seitenpfade (beginnend mit /), Sprungmarken (#marke) oder http(s)-Adressen.'; } /** diff --git a/resources/css/sections.css b/resources/css/sections.css index 3fdb041..a5198fa 100644 --- a/resources/css/sections.css +++ b/resources/css/sections.css @@ -200,3 +200,14 @@ .section-divider-accent span { border-radius: 5px; background: var(--lf-yellow); } + +/* Vorschau: Platzhalter für einen ausgeblendeten Abschnitt (die Website zeigt ihn nie). */ +.section-hidden-note { + margin: var(--space-3, 0.75rem) auto; + max-inline-size: 60rem; + padding: 0.5rem 0.875rem; + border: 1px dashed currentColor; + border-radius: 0.5rem; + font-size: 0.875rem; + opacity: 0.7; +} diff --git a/tests/Feature/CmsSectionLabelsTest.php b/tests/Feature/CmsSectionLabelsTest.php new file mode 100644 index 0000000..310c044 --- /dev/null +++ b/tests/Feature/CmsSectionLabelsTest.php @@ -0,0 +1,214 @@ + $extra + * @return array + */ + private function section(string $heading, array $extra = []): array + { + return $extra + [ + 'layout' => 'u4', + 'style' => [], + 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => $heading]]], + ]; + } + + private function sections(): PageSections + { + return app(PageSections::class); + } + + /** + * @param list> $sections + */ + private function html(array $sections, bool $preview = false): string + { + $read = $this->sections()->read($this->sections()->normalize($sections)); + + return app(SectionRenderer::class)->renderAll($read, $preview ? RenderContext::preview() : RenderContext::publicPage())->toHtml(); + } + + public function test_name_anchor_and_hidden_are_saved_and_read_back(): void + { + $saved = $this->sections()->normalize([ + $this->section('Kurse', ['name' => ' Kurs Übersicht ', 'anchor' => ' Kurse-2026 ', 'hidden' => true]), + $this->section('Kontakt'), + ]); + + $this->assertSame('Kurs Übersicht', $saved[0]['name']); + $this->assertSame('kurse-2026', $saved[0]['anchor']); + $this->assertTrue($saved[0]['hidden']); + $this->assertNull($saved[1]['name']); + $this->assertNull($saved[1]['anchor']); + $this->assertFalse($saved[1]['hidden']); + + $read = $this->sections()->read($saved); + $this->assertSame(['Kurs Übersicht', 'kurse-2026', true], [$read[0]['name'], $read[0]['anchor'], $read[0]['hidden']]); + } + + public function test_sections_saved_before_these_fields_read_with_defaults(): void + { + $read = $this->sections()->read([['id' => 'a1', 'v' => 2, 'layout' => 'u4', 'style' => [], 'slots' => [null]]]); + + $this->assertSame([null, null, false], [$read[0]['name'], $read[0]['anchor'], $read[0]['hidden']]); + } + + public function test_an_invalid_stored_anchor_is_not_trusted_when_reading(): void + { + $read = $this->sections()->read([['id' => 'a1', 'layout' => 'u4', 'anchor' => 'Evil">