Skip to content

Commit

Permalink
Used a more memory-efficient technique to store the sparsely-used att…
Browse files Browse the repository at this point in the history
…ributes of standardized elements.
  • Loading branch information
RyanLamansky committed Aug 5, 2024
1 parent 21449b2 commit 541eddd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Examples/AspNetPageEmitter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class TestDocument : IHtmlDocument
Task IHtmlDocument.WriteBodyContentsAsync(HtmlWriter writer, CancellationToken cancellationToken)
{
writer.WriteElement("p", null, children => children.WriteText("Test bytes."));
writer.WriteElement("p", null, children =>
{
children.WriteElement("a", attributes => attributes.Write("href", "/"), children => children.WriteText("Test link."));
});
writer.WriteScript(ValidatedScript.ForInlineSource("console.log('test')"));
return Task.CompletedTask;
}
Expand Down
30 changes: 27 additions & 3 deletions HtmlUtilities/Validated/StandardElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace HtmlUtilities.Validated;
using System.Runtime.CompilerServices;

namespace HtmlUtilities.Validated;

/// <summary>
/// Provides friendly syntax for use of standardized HTML elements.
Expand All @@ -7,17 +9,38 @@ public abstract class StandardElement
{
// Global attribute list from https://html.spec.whatwg.org/#global-attributes.

private readonly Dictionary<string, ValidatedAttributeValue> attributes = [];

private protected ValidatedAttributeValue? GetAttribute([CallerMemberName] string name = null!)
=> attributes.TryGetValue(name, out var value) ? value : null;

private protected void SetAttribute(ValidatedAttributeValue? value, [CallerMemberName] string name = null!)
{
if (value is null)
return;

attributes[name] = value.GetValueOrDefault();
}

/// <summary>
/// All HTML elements may have the accesskey content attribute set
/// The accesskey attribute's value is used by the user agent as a guide for creating a keyboard shortcut that activates or focuses the element.
/// If specified, the value must be an ordered set of unique space-separated tokens none of which are identical to another token and each of which must be exactly one code point in length.
/// Source: https://html.spec.whatwg.org/#the-accesskey-attribute
/// </summary>
public ValidatedAttributeValue? AccessKey { get => GetAttribute(); set => SetAttribute(value); }

/// <summary>
/// Uniquely identifies an element.
/// Source: https://dom.spec.whatwg.org/#concept-id
/// </summary>
public ValidatedAttributeValue? Id { get; set; }
public ValidatedAttributeValue? Id { get => GetAttribute(); set => SetAttribute(value); }

/// <summary>
/// The title attribute represents advisory information for the element, such as would be appropriate for a tooltip.
/// Source: https://html.spec.whatwg.org/#attr-title
/// </summary>
public ValidatedAttributeValue? Title { get; set; }
public ValidatedAttributeValue? Title { get => GetAttribute(); set => SetAttribute(value); }

private protected StandardElement()
{
Expand All @@ -31,6 +54,7 @@ private protected StandardElement()
/// <param name="writer">Receives the attributes.</param>
private protected void Write(AttributeWriter writer)
{
writer.Write(" accesskey"u8, AccessKey);
writer.Write(" id"u8, Id);
writer.Write(" title"u8, Title);
}
Expand Down
4 changes: 2 additions & 2 deletions HtmlUtilities/Validated/Standardized/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class Link : StandardElement
/// <summary>
/// Relationship between the document containing the hyperlink and the destination resource.
/// </summary>
public string? Rel { get; set; }
public ValidatedAttributeValue? Rel { get => GetAttribute(); set => SetAttribute(value); }

/// <summary>
/// Address of the hyperlink.
/// </summary>
public string? Href { get; set; }
public ValidatedAttributeValue? Href { get => GetAttribute(); set => SetAttribute(value); }

internal sealed override void Write(HtmlWriter writer)
{
Expand Down
2 changes: 1 addition & 1 deletion HtmlUtilities/Validated/Standardized/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Style : StandardElement
/// <summary>
/// Applicable media.
/// </summary>
public ValidatedAttributeValue? Media { get; set; }
public ValidatedAttributeValue? Media { get => GetAttribute(); set => SetAttribute(value); }

internal sealed override void Write(HtmlWriter writer)
{
Expand Down

0 comments on commit 541eddd

Please sign in to comment.