diff --git a/src/SlpModularCms.Core.Tests/Hosting/Website/HtmlMarkerEditorTests.cs b/src/SlpModularCms.Core.Tests/Hosting/Website/HtmlMarkerEditorTests.cs
new file mode 100644
index 0000000..bf1251a
--- /dev/null
+++ b/src/SlpModularCms.Core.Tests/Hosting/Website/HtmlMarkerEditorTests.cs
@@ -0,0 +1,147 @@
+using FluentAssertions;
+using SlpModularCms.Core.Hosting.Website;
+using Xunit;
+
+namespace SlpModularCms.Core.Tests.Hosting.Website;
+
+public class HtmlMarkerEditorTests
+{
+ [Theory]
+ [InlineData("
plain
", false)]
+ [InlineData("", false)]
+ [InlineData("", true)]
+ [InlineData("x", true)]
+ public void HasMarkers_RecognisesOnlyTheCmsMarkers(string html, bool expected)
+ => HtmlMarkerEditor.HasMarkers(html).Should().Be(expected);
+
+ // ---- regions ----
+
+ [Fact]
+ public void Region_ContentBetweenTheMarkersIsReplaced_AndTheMarkersStay()
+ {
+ var html = "";
+
+ HtmlMarkerEditor.ReplaceRegion(html, "CMS:OFFERINGS-HTML", "")
+ .Should().Be("");
+ }
+
+ [Fact]
+ public void Region_MayBeEmptied_AndEveryPairOnThePageIsReplaced()
+ {
+ var html = "a|b\nc";
+
+ HtmlMarkerEditor.ReplaceRegion(html, "CMS:BANNER-TOP-HTML", string.Empty)
+ .Should().Be("|");
+ }
+
+ [Fact]
+ public void Region_WithoutItsClosingMarker_IsLeftAlone()
+ {
+ var html = "baked";
+
+ HtmlMarkerEditor.ReplaceRegion(html, "CMS:OFFERINGS-HTML", "x").Should().Be(html);
+ }
+
+ [Fact]
+ public void Region_ReplacementIsLiteral_NoRegexSubstitutionIsApplied()
+ {
+ var html = "old";
+
+ HtmlMarkerEditor.ReplaceRegion(html, "CMS:A", "$1 ${open} $& \\n").Should().Be("$1 ${open} $& \\n");
+ }
+
+ // ---- data block ----
+
+ [Fact]
+ public void DataBlock_MarkerAndBakedBlockAreReplacedByTheLiveBlock()
+ {
+ var html = "\n ";
+
+ HtmlMarkerEditor.ReplaceDataBlock(html, "CMS:DATA", "cms-data", "")
+ .Should().Be("");
+ }
+
+ [Fact]
+ public void DataBlock_WithoutABakedBlock_TheMarkerAloneIsReplaced()
+ {
+ HtmlMarkerEditor.ReplaceDataBlock("x
", "CMS:DATA", "cms-data", "")
+ .Should().Be("x
");
+ }
+
+ [Fact]
+ public void DataBlock_AScriptWithAnotherIdIsNotTouched()
+ {
+ var html = "";
+
+ HtmlMarkerEditor.ReplaceDataBlock(html, "CMS:DATA", "cms-data", "")
+ .Should().Be("");
+ }
+
+ // ---- ld+json ----
+
+ private const string Marker = "CMS:OFFER-JSON-LD";
+
+ [Fact]
+ public void JsonLd_TheEditedJsonReplacesTheBlock_AndTheMarkerGoes()
+ {
+ var html = "";
+ 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("");
+ }
+
+ [Fact]
+ public void JsonLd_WhenTheEditReturnsNull_MarkerAndBlockStayExactlyAsBaked()
+ {
+ var html = "";
+
+ HtmlMarkerEditor.EditJsonLd(html, Marker, (_, _) => null).Should().Be(html);
+ }
+
+ [Theory]
+ [InlineData("")]
+ [InlineData("no block
")]
+ [InlineData("")]
+ 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 = "vanaf € 185 en € 175
";
+
+ HtmlMarkerEditor.ReplaceInlineValues(html, "data-cms-price", slug => slug == "newborn" ? "€ 190" : null)
+ .Should().Be("vanaf € 190 en € 175
");
+ }
+
+ [Fact]
+ public void Inline_ANestedSpanOrAnotherAttributeIsNotTouched()
+ {
+ var html = "€ 1€ 2";
+
+ HtmlMarkerEditor.ReplaceInlineValues(html, "data-cms-price", _ => "NEW").Should().Be(html);
+ }
+
+ // ---- escaping ----
+
+ [Fact]
+ public void EscapeForScript_CannotCloseTheElement_AndKeepsTheJsonValueIntact()
+ {
+ var json = "{\"t\":\"&\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("&\u2028\u2029");
+ }
+}
diff --git a/src/SlpModularCms.Core/Hosting/Website/HtmlMarkerEditor.cs b/src/SlpModularCms.Core/Hosting/Website/HtmlMarkerEditor.cs
new file mode 100644
index 0000000..a648a4b
--- /dev/null
+++ b/src/SlpModularCms.Core/Hosting/Website/HtmlMarkerEditor.cs
@@ -0,0 +1,104 @@
+using System.Text.RegularExpressions;
+
+namespace SlpModularCms.Core.Hosting.Website;
+
+///
+/// The small, strict text edits a website page accepts from the CMS host. Every edit finds its marker by an exact
+/// pattern and leaves the page alone when the pattern does not match: a marker that is missing, malformed or not in
+/// the agreed place is never "repaired". All methods are pure (text in, text out).
+///
+///
+/// The marker formats are a contract with the website build (see aidlc-docs/features/asf-host-injection/plan.md):
+///
+/// - <!--CMS:X-->…<!--/CMS:X-->: a region whose content the host replaces (the fallback in between is the baked content).
+/// - <!--CMS:DATA--> directly followed by the baked <script type="application/json" id="cms-data"> block.
+/// - <!--CMS:OFFER-JSON-LD slug="x"--> directly before a <script type="application/ld+json"> block.
+/// - <span data-cms-price="x">text</span>: a simple, not nested span whose text the host replaces.
+///
+///
+public static class HtmlMarkerEditor
+{
+ private const RegexOptions Options = RegexOptions.Singleline | RegexOptions.CultureInvariant;
+ private static readonly TimeSpan MatchTimeout = TimeSpan.FromSeconds(2);
+
+ /// Whether the page mentions any CMS marker at all (a cheap check before any real work).
+ public static bool HasMarkers(string html) =>
+ html.Contains(")(?.*?)(?)";
+ return Regex.Replace(html, pattern, m => m.Groups["open"].Value + content + m.Groups["close"].Value, Options, MatchTimeout);
+ }
+
+ ///
+ /// Replaces <!--markerName--> plus the baked <script … id="blockId">…</script> block that
+ /// directly follows it by (marker and old block both go). Without such a block, the
+ /// marker alone is replaced.
+ ///
+ public static string ReplaceDataBlock(string html, string markerName, string blockId, string newBlock)
+ {
+ var pattern = "(?:\\s*)?";
+ return Regex.Replace(html, pattern, _ => newBlock, Options, MatchTimeout);
+ }
+
+ ///
+ /// For every <!--markerName slug="x"--> directly before a <script type="application/ld+json">
+ /// block: calls with the slug and the JSON text. A returned text replaces the JSON and the
+ /// marker is removed; null leaves marker and block exactly as they were (used when the edit cannot be made, so the
+ /// baked page is served).
+ ///
+ public static string EditJsonLd(string html, string markerName, Func edit)
+ {
+ var pattern = "\\s*" +
+ "(?";
+ return Regex.Replace(html, pattern, m =>
+ {
+ var edited = edit(m.Groups["slug"].Value, m.Groups["json"].Value);
+ return edited is null ? m.Value : m.Groups["open"].Value + edited + "";
+ }, Options, MatchTimeout);
+ }
+
+ ///
+ /// Replaces the text of every simple <span {attribute}="slug">text</span> by what
+ /// returns for the slug (already HTML-encoded by the caller); null leaves the span alone.
+ ///
+ public static string ReplaceInlineValues(string html, string attribute, Func textFor)
+ {
+ var pattern = "(?[a-z0-9-]+)\">)(?[^<]*)(?)";
+ return Regex.Replace(html, pattern, m =>
+ {
+ var text = textFor(m.Groups["slug"].Value);
+ return text is null ? m.Value : m.Groups["open"].Value + text + m.Groups["close"].Value;
+ }, Options, MatchTimeout);
+ }
+
+ ///
+ /// Makes JSON safe to put inside a <script> element: <, >, &, U+2028 and
+ /// U+2029 become \uXXXX escapes (still the same JSON string values), so the text can never close the element
+ /// or be read as markup.
+ ///
+ public static string EscapeForScript(string json)
+ {
+ var builder = new System.Text.StringBuilder(json.Length + 16);
+ foreach (var c in json)
+ {
+ switch (c)
+ {
+ case '<': builder.Append("\\u003C"); break;
+ case '>': builder.Append("\\u003E"); break;
+ case '&': builder.Append("\\u0026"); break;
+ case '\u2028': builder.Append("\\u2028"); break;
+ case '\u2029': builder.Append("\\u2029"); break;
+ default: builder.Append(c); break;
+ }
+ }
+
+ return builder.ToString();
+ }
+}