Hero: instelling Hero:TitlePrefix (verborgen merkvoorvoegsel in de h1 van de tekst-only-regio)

SLP-composite: 'SLP Software: '; ASF leeg (golden files ongewijzigd). Tests en contract bijgewerkt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 20:42:49 +01:00
co-authored by Claude Sonnet 5.5
parent 4c7787caf2
commit 969bb14d86
8 changed files with 70 additions and 7 deletions
@@ -10,7 +10,8 @@
},
"AllowedHosts": "*",
// The SLP hero has no images: the host writes its title, lead and buttons as text into the first HTML.
"Hero": { "TextOnlyRegion": true },
// TitlePrefix: visually hidden brand text before the title lines in the h1 (for crawlers and screen readers).
"Hero": { "TextOnlyRegion": true, "TitlePrefix": "SLP Software: " },
"Website": {
// Secure default: every response carries noindex and /robots.txt disallows all. A production
// instance must set Website__NoIndex=false and Website__PublicBaseUrl=https://slpsoftware.nl explicitly;
@@ -68,6 +68,14 @@ public class SiteContractTests
SiteFiles.Committed(site).GetValue<bool>("Hero:TextOnlyRegion").Should().Be(textOnly);
}
[Theory]
[InlineData(SiteFiles.SlpSoftware, "SLP Software: ")]
[InlineData(SiteFiles.AndreaSlopFotografie, "")]
public void TheHeroTitlePrefix_IsSetForSlpSoftware_AndEmptyForAsf(string site, string prefix)
{
(SiteFiles.Committed(site)["Hero:TitlePrefix"] ?? string.Empty).Should().Be(prefix);
}
[Theory]
[MemberData(nameof(Sites))]
public void WebsiteIsNoIndex_ByDefault(string site)
@@ -64,7 +64,48 @@ public class HeroTextOnlyTests
HeroPictureRenderer.Render(hero, textOnlyRegion: true).IsEmpty.Should().BeTrue();
}
private const string Page = "<head><!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD--></head><body><!--CMS:HERO-HTML-->baked<!--/CMS:HERO-HTML--></body>";
[Fact]
public void ThePrefix_IsTheFirstHiddenPartOfTheH1_AndIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true, titlePrefix: "SLP & Co: ");
injection.Region.Should().Be("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-prefix\">SLP &amp; Co: </span><span class=\"cms-hero-tagline\">Label</span> <span class=\"cms-hero-title-lines\">Titel</span></h1>");
}
[Theory]
[InlineData("")]
[InlineData(" ")]
public void AnEmptyPrefix_ChangesNothing(string prefix)
{
var hero = TextOnly();
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: prefix).Region
.Should().Be(HeroPictureRenderer.Render(hero, textOnlyRegion: true).Region).And.NotContain("title-prefix");
}
[Fact]
public void APictureHero_NeverGetsThePrefix()
{
var variant = new HeroVariantDto("wide", 640, 427, "image/webp", "/media/hero/0123456789abcdef/wide-640.webp");
var image = new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-640.webp", [variant]);
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("alt", "", false, image)]);
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: "SLP: ").Region.Should().NotContain("title-prefix");
}
[Fact]
public async Task TheTransform_PassesTheConfiguredPrefix()
{
var model = Substitute.For<IHeroPageModel>();
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
var transform = new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = true, TitlePrefix = "SLP Software: " }));
var html = await transform.ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
html.Should().Contain("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-prefix\">SLP Software: </span><span class=\"cms-hero-title-lines\">");
}
private const string Page ="<head><!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD--></head><body><!--CMS:HERO-HTML-->baked<!--/CMS:HERO-HTML--></body>";
private static HeroHtmlTransform Transform(bool textOnly)
{
@@ -57,7 +57,7 @@ public sealed class HeroHtmlTransform(IHeroPageModel model, Microsoft.Extensions
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
{
var rendered = HeroPictureRenderer.Render(await model.GetAsync(cancellationToken), options?.Value.TextOnlyRegion ?? false);
var rendered = HeroPictureRenderer.Render(await model.GetAsync(cancellationToken), options?.Value.TextOnlyRegion ?? false, options?.Value.TitlePrefix ?? string.Empty);
if (rendered.IsEmpty)
{
return html;
@@ -20,6 +20,12 @@ public sealed class HeroOptions
/// that baked hero when the CMS hero has no slide (fail open).
/// </summary>
public bool TextOnlyRegion { get; set; }
/// <summary>
/// Brand text written as the first, visually hidden part of the h1 of the text-only region (SLP Software:
/// "SLP Software: "), so crawlers and screen readers read the full title. Empty (default) writes nothing.
/// </summary>
public string TitlePrefix { get; set; } = string.Empty;
}
/// <summary>Turns stored entities into the shapes of the API and the data block. Pure.</summary>
@@ -29,7 +29,7 @@ public static class HeroPictureRenderer
private const string Portrait = "(orientation: portrait)";
private const string Landscape = "(orientation: landscape)";
public static HeroInjection Render(PublicHeroDto? hero, bool textOnlyRegion = false)
public static HeroInjection Render(PublicHeroDto? hero, bool textOnlyRegion = false, string titlePrefix = "")
{
if (hero is null || string.IsNullOrWhiteSpace(hero.Title))
{
@@ -41,7 +41,7 @@ public static class HeroPictureRenderer
// preload. Otherwise, and for a hero WITH slides of which none is usable, the region stays empty (ASF).
if ((hero.Slides ?? []).Count == 0)
{
return textOnlyRegion ? new HeroInjection(TextOnlyRegion(hero), string.Empty) : HeroInjection.None;
return textOnlyRegion ? new HeroInjection(TextOnlyRegion(hero, titlePrefix), string.Empty) : HeroInjection.None;
}
var slide = (hero.Slides ?? []).FirstOrDefault(IsUsable);
@@ -99,10 +99,16 @@ public static class HeroPictureRenderer
/// <c>span.cms-hero-title-lines</c> separated by <c>&lt;br&gt;</c> (no brand prefix: the site adds it to its own
/// screen-reader title), then <c>p.cms-hero-lead</c> and the buttons row.
/// </summary>
private static string TextOnlyRegion(PublicHeroDto hero)
private static string TextOnlyRegion(PublicHeroDto hero, string titlePrefix)
{
var html = new StringBuilder();
html.Append("<h1 class=\"cms-hero-title\">");
// The brand prefix (Hero:TitlePrefix) is text for crawlers and screen readers; the site styles it visually hidden.
if (!string.IsNullOrWhiteSpace(titlePrefix))
{
html.Append("<span class=\"cms-hero-title-prefix\">").Append(Encode(titlePrefix)).Append("</span>");
}
if (!string.IsNullOrWhiteSpace(hero.Tagline))
{
html.Append("<span class=\"cms-hero-tagline\">").Append(Encode(hero.Tagline)).Append("</span> ");