From 6c260ac87d7c0b655dc14c81326cb67606e1fedf Mon Sep 17 00:00:00 2001 From: Ryan Lamansky <13633345+RyanLamansky@users.noreply.github.com> Date: Sat, 24 Aug 2024 09:31:10 -0500 Subject: [PATCH] ImportMap can now be used with ValidatedScript. --- .../Validated/Standardized/ImportMap.cs | 2 ++ HtmlUtilities/Validated/ValidatedScript.cs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/HtmlUtilities/Validated/Standardized/ImportMap.cs b/HtmlUtilities/Validated/Standardized/ImportMap.cs index a15862e..eb47a27 100644 --- a/HtmlUtilities/Validated/Standardized/ImportMap.cs +++ b/HtmlUtilities/Validated/Standardized/ImportMap.cs @@ -31,6 +31,8 @@ public sealed class ImportMap [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary>? Scopes { get; set; } + internal ReadOnlyMemory ToUtf8 => JsonSerializer.SerializeToUtf8Bytes(this, ImportMapSerializerContext.Default.ImportMap); + /// public override string ToString() => JsonSerializer.Serialize(this, ImportMapSerializerContext.Default.ImportMap); } diff --git a/HtmlUtilities/Validated/ValidatedScript.cs b/HtmlUtilities/Validated/ValidatedScript.cs index 3568ed1..9817759 100644 --- a/HtmlUtilities/Validated/ValidatedScript.cs +++ b/HtmlUtilities/Validated/ValidatedScript.cs @@ -39,7 +39,7 @@ public static ValidatedScript ForFileSource(ReadOnlySpan source, params Va /// Creates a validated script element that uses inline content. /// /// The validated script. - /// contains a potentially invalid character sequence. + /// contains a potentially invalid character sequence. public static ValidatedScript ForInlineSource(ReadOnlySpan script, params ValidatedAttribute[]? attributes) { var writer = new ArrayBuilder(script.Length); @@ -59,6 +59,20 @@ public static ValidatedScript ForInlineSource(ReadOnlySpan script, params } } + /// + /// Creates a validated script element that uses inline content from a . + /// + /// The validated script. + /// cannot be null. + /// contains a potentially invalid character sequence. + public static ValidatedScript ForInlineSource(Standardized.ImportMap importMap, params ValidatedAttribute[]? attributes) + { + ArgumentNullException.ThrowIfNull(importMap, nameof(importMap)); + + // Since importMap is guaranteed to be JSON-based, a fast path may be possible. + return ForInlineSource(importMap.ToString(), attributes); + } + internal static void Validate(ref ArrayBuilder writer, ReadOnlySpan script) { // See https://html.spec.whatwg.org/#restrictions-for-contents-of-script-elements for the official rules.