Skip to content

Commit

Permalink
Added a helper class for import maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLamansky committed Aug 24, 2024
1 parent 6468b01 commit 64fffe2
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions HtmlUtilities/Validated/Standardized/ImportMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HtmlUtilities.Validated.Standardized;

/// <summary>
/// Contents for a specialized "script" element that allows developers to control how the browser resolves module specifiers when importing JavaScript modules.
/// Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap
/// </summary>
public sealed class ImportMap
{
/// <summary>
/// Replaces module specifiers in matching import statements.
/// Effects can be overridden by a match within <see cref="Scopes"/>.
/// </summary>
[JsonPropertyName("imports")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IDictionary<string, string>? Imports { get; set; }

/// <summary>
/// Provides integrity metadata for module sources.
/// </summary>
[JsonPropertyName("integrity")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IDictionary<string, string>? Integrity { get; set; }

/// <summary>
/// Path-specific module specifier maps.
/// </summary>
[JsonPropertyName("scopes")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IDictionary<string, IDictionary<string, string>>? Scopes { get; set; }

/// <inheritdoc />
public override string ToString() => JsonSerializer.Serialize(this, ImportMapSerializerContext.Default.ImportMap);
}

[JsonSerializable(typeof(ImportMap))]
internal sealed partial class ImportMapSerializerContext : JsonSerializerContext
{
}

0 comments on commit 64fffe2

Please sign in to comment.