Hero: tekst-only regio alleen bij Hero:TextOnlyRegion (aan voor SLP, uit voor ASF: de ingebakken hero blijft)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,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 },
|
||||
"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;
|
||||
|
||||
@@ -59,6 +59,15 @@ public class SiteContractTests
|
||||
(configured ?? "SpaFallback").Should().Be(mode);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(SiteFiles.SlpSoftware, true)]
|
||||
[InlineData(SiteFiles.AndreaSlopFotografie, false)]
|
||||
public void TheHeroIsWrittenAsTextOnlyForTheSiteWithoutHeroImages(string site, bool textOnly)
|
||||
{
|
||||
// SLP Software's hero has no images, so the host writes it as text; ASF's baked hero is a picture and stays when the CMS hero has no slide.
|
||||
SiteFiles.Committed(site).GetValue<bool>("Hero:TextOnlyRegion").Should().Be(textOnly);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Sites))]
|
||||
public void WebsiteIsNoIndex_ByDefault(string site)
|
||||
|
||||
@@ -6,7 +6,10 @@ using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
/// <summary>A hero without slides (SLP Software): the host writes the title, the lead and the buttons into the first HTML.</summary>
|
||||
/// <summary>
|
||||
/// A hero without slides (SLP Software): when the site asks for it (<c>Hero:TextOnlyRegion</c>) the host writes the
|
||||
/// title, the lead and the buttons into the first HTML; otherwise (ASF) the baked page stays.
|
||||
/// </summary>
|
||||
public class HeroTextOnlyTests
|
||||
{
|
||||
private static PublicHeroDto TextOnly(string title = "Websites,\napps\n& meer.", string? tagline = null, string? lead = "Een lead.", params HeroButtonDto[] buttons)
|
||||
@@ -17,7 +20,7 @@ public class HeroTextOnlyTests
|
||||
{
|
||||
var hero = TextOnly(buttons: [new HeroButtonDto("Bekijk pakketten", "#pakketten"), new HeroButtonDto("Plan een gesprek", "#contact")]);
|
||||
|
||||
var injection = HeroPictureRenderer.Render(hero);
|
||||
var injection = HeroPictureRenderer.Render(hero, textOnlyRegion: true);
|
||||
|
||||
injection.Region.Should().Be(
|
||||
"<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-lines\">Websites,<br>apps<br>& meer.</span></h1>" +
|
||||
@@ -27,10 +30,19 @@ public class HeroTextOnlyTests
|
||||
injection.IsEmpty.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutTheSiteAskingForIt_AHeroWithoutSlides_LeavesTheBakedHero()
|
||||
{
|
||||
var hero = TextOnly(buttons: [new HeroButtonDto("Knop", "#x")]);
|
||||
|
||||
HeroPictureRenderer.Render(hero).IsEmpty.Should().BeTrue();
|
||||
HeroPictureRenderer.Render(hero, textOnlyRegion: false).IsEmpty.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutALead_OrButtons_ThosePartsAreLeftOut_AndATaglineComesFirst()
|
||||
{
|
||||
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null));
|
||||
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true);
|
||||
|
||||
injection.Region.Should().Be("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-tagline\">Label</span> <span class=\"cms-hero-title-lines\">Titel</span></h1>");
|
||||
}
|
||||
@@ -38,30 +50,41 @@ public class HeroTextOnlyTests
|
||||
[Fact]
|
||||
public void TheLead_KeepsItsLineBreaks_AndEverythingIsEncoded()
|
||||
{
|
||||
var injection = HeroPictureRenderer.Render(TextOnly("<b>x</b>", null, "a & b\nc \"d\"", new HeroButtonDto("<i>", "/p?a=1&b=2")));
|
||||
var injection = HeroPictureRenderer.Render(TextOnly("<b>x</b>", null, "a & b\nc \"d\"", new HeroButtonDto("<i>", "/p?a=1&b=2")), textOnlyRegion: true);
|
||||
|
||||
injection.Region.Should().Contain("<b>x</b>").And.Contain("a & b<br>c "d"").And.Contain("href=\"/p?a=1&b=2\"><i></a>");
|
||||
injection.Region.Should().NotContain("<b>").And.NotContain("<i>");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AHeroWithSlidesThatAreAllUnusable_StaysEmpty_AsForTheAsfSite()
|
||||
public void AHeroWithSlidesThatAreAllUnusable_StaysEmpty_EvenWhenTheSiteAsksForText()
|
||||
{
|
||||
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]);
|
||||
|
||||
HeroPictureRenderer.Render(hero).IsEmpty.Should().BeTrue();
|
||||
HeroPictureRenderer.Render(hero, textOnlyRegion: true).IsEmpty.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheTransform_FillsTheTextRegion_AndLeavesThePreloadRegionAlone()
|
||||
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)
|
||||
{
|
||||
var model = Substitute.For<IHeroPageModel>();
|
||||
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
|
||||
const string page = "<head><!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD--></head><body><!--CMS:HERO-HTML-->baked<!--/CMS:HERO-HTML--></body>";
|
||||
return new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = textOnly }));
|
||||
}
|
||||
|
||||
var html = await new HeroHtmlTransform(model).ApplyAsync(page, new HtmlTransformContext("https://x"), default);
|
||||
[Fact]
|
||||
public async Task TheTransform_FillsTheTextRegion_WhenTheSiteAsksForIt_AndLeavesThePreloadRegionAlone()
|
||||
{
|
||||
var html = await Transform(textOnly: true).ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
|
||||
|
||||
html.Should().Contain("<!--CMS:HERO-HTML--><h1 class=\"cms-hero-title\">").And.NotContain("baked");
|
||||
html.Should().Contain("<!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD-->");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheTransform_LeavesTheWholePageAlone_WhenTheSiteDoesNotAskForText()
|
||||
{
|
||||
(await Transform(textOnly: false).ApplyAsync(Page, new HtmlTransformContext("https://x"), default)).Should().Be(Page);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public sealed class HeroPageModel(IPublicHeroService publicHero, IMediaStore sto
|
||||
}
|
||||
|
||||
/// <summary>Fills the hero regions of a page: <c><!--CMS:HERO-HTML--></c> in the body and <c><!--CMS:HERO-PRELOAD--></c> in the head. Fail-open: no hero means the baked page stays.</summary>
|
||||
public sealed class HeroHtmlTransform(IHeroPageModel model) : IHtmlPageTransform
|
||||
public sealed class HeroHtmlTransform(IHeroPageModel model, Microsoft.Extensions.Options.IOptions<HeroOptions>? options = null) : IHtmlPageTransform
|
||||
{
|
||||
public const string HtmlRegion = "CMS:HERO-HTML";
|
||||
public const string PreloadRegion = "CMS:HERO-PRELOAD";
|
||||
@@ -57,7 +57,7 @@ public sealed class HeroHtmlTransform(IHeroPageModel model) : IHtmlPageTransform
|
||||
|
||||
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var rendered = HeroPictureRenderer.Render(await model.GetAsync(cancellationToken));
|
||||
var rendered = HeroPictureRenderer.Render(await model.GetAsync(cancellationToken), options?.Value.TextOnlyRegion ?? false);
|
||||
if (rendered.IsEmpty)
|
||||
{
|
||||
return html;
|
||||
|
||||
@@ -13,6 +13,13 @@ public sealed class HeroOptions
|
||||
/// carry more slides than this, so the first HTML cannot grow without bound. Everything above stays saved.
|
||||
/// </summary>
|
||||
public int MaxPublishedSlides { get; set; } = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Whether a hero without any slide is written into the page as text (title lines, lead, buttons). On for a site
|
||||
/// that has no images in its hero (SLP Software); off by default, so a site whose baked hero is a picture (ASF) keeps
|
||||
/// that baked hero when the CMS hero has no slide (fail open).
|
||||
/// </summary>
|
||||
public bool TextOnlyRegion { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>Turns stored entities into the shapes of the API and the data block. Pure.</summary>
|
||||
|
||||
@@ -29,18 +29,19 @@ public static class HeroPictureRenderer
|
||||
private const string Portrait = "(orientation: portrait)";
|
||||
private const string Landscape = "(orientation: landscape)";
|
||||
|
||||
public static HeroInjection Render(PublicHeroDto? hero)
|
||||
public static HeroInjection Render(PublicHeroDto? hero, bool textOnlyRegion = false)
|
||||
{
|
||||
if (hero is null || string.IsNullOrWhiteSpace(hero.Title))
|
||||
{
|
||||
return HeroInjection.None;
|
||||
}
|
||||
|
||||
// A hero without any slide is a text-only hero (SLP Software): the region holds the title lines, the lead and
|
||||
// the buttons, and there is nothing to preload. A hero WITH slides of which none is usable stays empty (ASF).
|
||||
// A hero without any slide is a text-only hero (SLP Software, when the site asks for it with
|
||||
// Hero:TextOnlyRegion): the region holds the title lines, the lead and the buttons, and there is nothing to
|
||||
// preload. Otherwise, and for a hero WITH slides of which none is usable, the region stays empty (ASF).
|
||||
if ((hero.Slides ?? []).Count == 0)
|
||||
{
|
||||
return new HeroInjection(TextOnlyRegion(hero), string.Empty);
|
||||
return textOnlyRegion ? new HeroInjection(TextOnlyRegion(hero), string.Empty) : HeroInjection.None;
|
||||
}
|
||||
|
||||
var slide = hero.Slides.FirstOrDefault(IsUsable);
|
||||
|
||||
Reference in New Issue
Block a user