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:
@@ -0,0 +1,104 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SlpModularCms.Core.Hosting.Website;
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The marker formats are a contract with the website build (see aidlc-docs/features/asf-host-injection/plan.md):
|
||||
/// <list type="bullet">
|
||||
/// <item><c><!--CMS:X-->…<!--/CMS:X--></c>: a region whose content the host replaces (the fallback in between is the baked content).</item>
|
||||
/// <item><c><!--CMS:DATA--></c> directly followed by the baked <c><script type="application/json" id="cms-data"></c> block.</item>
|
||||
/// <item><c><!--CMS:OFFER-JSON-LD slug="x"--></c> directly before a <c><script type="application/ld+json"></c> block.</item>
|
||||
/// <item><c><span data-cms-price="x">text</span></c>: a simple, not nested span whose text the host replaces.</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public static class HtmlMarkerEditor
|
||||
{
|
||||
private const RegexOptions Options = RegexOptions.Singleline | RegexOptions.CultureInvariant;
|
||||
private static readonly TimeSpan MatchTimeout = TimeSpan.FromSeconds(2);
|
||||
|
||||
/// <summary>Whether the page mentions any CMS marker at all (a cheap check before any real work).</summary>
|
||||
public static bool HasMarkers(string html) =>
|
||||
html.Contains("<!--CMS:", StringComparison.Ordinal)
|
||||
|| html.Contains("data-cms-price=", StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the content between <c><!--name--></c> and <c><!--/name--></c> (every pair on the page).
|
||||
/// The markers themselves stay. An opening marker without its closing marker is left alone.
|
||||
/// </summary>
|
||||
public static string ReplaceRegion(string html, string name, string content)
|
||||
{
|
||||
var pattern = "(?<open><!--" + Regex.Escape(name) + "-->)(?<inner>.*?)(?<close><!--/" + Regex.Escape(name) + "-->)";
|
||||
return Regex.Replace(html, pattern, m => m.Groups["open"].Value + content + m.Groups["close"].Value, Options, MatchTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces <c><!--markerName--></c> plus the baked <c><script … id="blockId">…</script></c> block that
|
||||
/// directly follows it by <paramref name="newBlock"/> (marker and old block both go). Without such a block, the
|
||||
/// marker alone is replaced.
|
||||
/// </summary>
|
||||
public static string ReplaceDataBlock(string html, string markerName, string blockId, string newBlock)
|
||||
{
|
||||
var pattern = "<!--" + Regex.Escape(markerName) + "-->(?:\\s*<script\\b[^>]*\\bid=\"" + Regex.Escape(blockId) + "\"[^>]*>.*?</script>)?";
|
||||
return Regex.Replace(html, pattern, _ => newBlock, Options, MatchTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For every <c><!--markerName slug="x"--></c> directly before a <c><script type="application/ld+json"></c>
|
||||
/// block: calls <paramref name="edit"/> 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).
|
||||
/// </summary>
|
||||
public static string EditJsonLd(string html, string markerName, Func<string, string, string?> edit)
|
||||
{
|
||||
var pattern = "<!--" + Regex.Escape(markerName) + "\\s+slug=\"(?<slug>[a-z0-9-]+)\"\\s*-->\\s*" +
|
||||
"(?<open><script\\b[^>]*type=\"application/ld\\+json\"[^>]*>)(?<json>.*?)</script>";
|
||||
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 + "</script>";
|
||||
}, Options, MatchTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the text of every simple <c><span {attribute}="slug">text</span></c> by what
|
||||
/// <paramref name="textFor"/> returns for the slug (already HTML-encoded by the caller); null leaves the span alone.
|
||||
/// </summary>
|
||||
public static string ReplaceInlineValues(string html, string attribute, Func<string, string?> textFor)
|
||||
{
|
||||
var pattern = "(?<open><span\\s+" + Regex.Escape(attribute) + "=\"(?<slug>[a-z0-9-]+)\">)(?<inner>[^<]*)(?<close></span>)";
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes JSON safe to put inside a <c><script></c> element: <c><</c>, <c>></c>, <c>&</c>, U+2028 and
|
||||
/// U+2029 become <c>\uXXXX</c> escapes (still the same JSON string values), so the text can never close the element
|
||||
/// or be read as markup.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user