ASF-injectie 1/5: HtmlMarkerEditor (regio's, data-blok, ld+json, inline waarden, script-escaping) met tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 16:03:52 +01:00
co-authored by Claude Sonnet 5.5
parent ab50d7dfba
commit 7d66944019
2 changed files with 251 additions and 0 deletions
@@ -0,0 +1,147 @@
using FluentAssertions;
using SlpModularCms.Core.Hosting.Website;
using Xunit;
namespace SlpModularCms.Core.Tests.Hosting.Website;
public class HtmlMarkerEditorTests
{
[Theory]
[InlineData("<p>plain</p>", false)]
[InlineData("<!--SLP:OFFERINGS-HTML-->", false)]
[InlineData("<div><!--CMS:DATA--></div>", true)]
[InlineData("<span data-cms-price=\"newborn\">x</span>", true)]
public void HasMarkers_RecognisesOnlyTheCmsMarkers(string html, bool expected)
=> HtmlMarkerEditor.HasMarkers(html).Should().Be(expected);
// ---- regions ----
[Fact]
public void Region_ContentBetweenTheMarkersIsReplaced_AndTheMarkersStay()
{
var html = "<div><!--CMS:OFFERINGS-HTML--><ul><li>baked</li></ul><!--/CMS:OFFERINGS-HTML--></div>";
HtmlMarkerEditor.ReplaceRegion(html, "CMS:OFFERINGS-HTML", "<ul><li>live</li></ul>")
.Should().Be("<div><!--CMS:OFFERINGS-HTML--><ul><li>live</li></ul><!--/CMS:OFFERINGS-HTML--></div>");
}
[Fact]
public void Region_MayBeEmptied_AndEveryPairOnThePageIsReplaced()
{
var html = "<!--CMS:BANNER-TOP-HTML-->a<!--/CMS:BANNER-TOP-HTML-->|<!--CMS:BANNER-TOP-HTML-->b\nc<!--/CMS:BANNER-TOP-HTML-->";
HtmlMarkerEditor.ReplaceRegion(html, "CMS:BANNER-TOP-HTML", string.Empty)
.Should().Be("<!--CMS:BANNER-TOP-HTML--><!--/CMS:BANNER-TOP-HTML-->|<!--CMS:BANNER-TOP-HTML--><!--/CMS:BANNER-TOP-HTML-->");
}
[Fact]
public void Region_WithoutItsClosingMarker_IsLeftAlone()
{
var html = "<!--CMS:OFFERINGS-HTML-->baked";
HtmlMarkerEditor.ReplaceRegion(html, "CMS:OFFERINGS-HTML", "x").Should().Be(html);
}
[Fact]
public void Region_ReplacementIsLiteral_NoRegexSubstitutionIsApplied()
{
var html = "<!--CMS:A-->old<!--/CMS:A-->";
HtmlMarkerEditor.ReplaceRegion(html, "CMS:A", "$1 ${open} $& \\n").Should().Be("<!--CMS:A-->$1 ${open} $& \\n<!--/CMS:A-->");
}
// ---- data block ----
[Fact]
public void DataBlock_MarkerAndBakedBlockAreReplacedByTheLiveBlock()
{
var html = "<head><!--CMS:DATA-->\n <script type=\"application/json\" id=\"cms-data\">{\"a\":1}</script></head>";
HtmlMarkerEditor.ReplaceDataBlock(html, "CMS:DATA", "cms-data", "<script id=\"cms-data\">LIVE</script>")
.Should().Be("<head><script id=\"cms-data\">LIVE</script></head>");
}
[Fact]
public void DataBlock_WithoutABakedBlock_TheMarkerAloneIsReplaced()
{
HtmlMarkerEditor.ReplaceDataBlock("<body><!--CMS:DATA--><p>x</p></body>", "CMS:DATA", "cms-data", "<script>LIVE</script>")
.Should().Be("<body><script>LIVE</script><p>x</p></body>");
}
[Fact]
public void DataBlock_AScriptWithAnotherIdIsNotTouched()
{
var html = "<!--CMS:DATA--><script type=\"application/json\" id=\"other\">{}</script>";
HtmlMarkerEditor.ReplaceDataBlock(html, "CMS:DATA", "cms-data", "<script>LIVE</script>")
.Should().Be("<script>LIVE</script><script type=\"application/json\" id=\"other\">{}</script>");
}
// ---- ld+json ----
private const string Marker = "CMS:OFFER-JSON-LD";
[Fact]
public void JsonLd_TheEditedJsonReplacesTheBlock_AndTheMarkerGoes()
{
var html = "<head><!--CMS:OFFER-JSON-LD slug=\"newborn\"--><script type=\"application/ld+json\">{\"a\":1}</script></head>";
string? seen = null;
var result = HtmlMarkerEditor.EditJsonLd(html, Marker, (slug, json) => { seen = slug + "|" + json; return "{\"b\":2}"; });
seen.Should().Be("newborn|{\"a\":1}");
result.Should().Be("<head><script type=\"application/ld+json\">{\"b\":2}</script></head>");
}
[Fact]
public void JsonLd_WhenTheEditReturnsNull_MarkerAndBlockStayExactlyAsBaked()
{
var html = "<!--CMS:OFFER-JSON-LD slug=\"newborn\"--><script type=\"application/ld+json\">{\"a\":1}</script>";
HtmlMarkerEditor.EditJsonLd(html, Marker, (_, _) => null).Should().Be(html);
}
[Theory]
[InlineData("<!--CMS:OFFER-JSON-LD slug=\"New Born\"--><script type=\"application/ld+json\">{}</script>")]
[InlineData("<!--CMS:OFFER-JSON-LD slug=\"newborn\"--><p>no block</p><script type=\"application/ld+json\">{}</script>")]
[InlineData("<!--CMS:OFFER-JSON-LD slug=\"newborn\"--><script type=\"text/javascript\">{}</script>")]
public void JsonLd_AMarkerNotInTheAgreedShape_IsNeverCalled(string html)
{
var called = false;
HtmlMarkerEditor.EditJsonLd(html, Marker, (_, _) => { called = true; return "x"; }).Should().Be(html);
called.Should().BeFalse();
}
// ---- inline values ----
[Fact]
public void Inline_TheTextOfASimpleSpanIsReplaced()
{
var html = "<p>vanaf <span data-cms-price=\"newborn\">€ 185</span> en <span data-cms-price=\"gezin\">€ 175</span></p>";
HtmlMarkerEditor.ReplaceInlineValues(html, "data-cms-price", slug => slug == "newborn" ? "€&nbsp;190" : null)
.Should().Be("<p>vanaf <span data-cms-price=\"newborn\">€&nbsp;190</span> en <span data-cms-price=\"gezin\">€ 175</span></p>");
}
[Fact]
public void Inline_ANestedSpanOrAnotherAttributeIsNotTouched()
{
var html = "<span data-cms-price=\"a\"><b>€ 1</b></span><span class=\"x\" data-cms-price=\"b\">€ 2</span>";
HtmlMarkerEditor.ReplaceInlineValues(html, "data-cms-price", _ => "NEW").Should().Be(html);
}
// ---- escaping ----
[Fact]
public void EscapeForScript_CannotCloseTheElement_AndKeepsTheJsonValueIntact()
{
var json = "{\"t\":\"</script><b>&\u2028\u2029\"}";
var escaped = HtmlMarkerEditor.EscapeForScript(json);
escaped.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028").And.NotContain("\u2029");
System.Text.Json.JsonDocument.Parse(escaped).RootElement.GetProperty("t").GetString().Should().Be("</script><b>&\u2028\u2029");
}
}