Skip to content

Commit

Permalink
Standard element unassigned or null attributes are now correctly retu…
Browse files Browse the repository at this point in the history
…rned as null. Improved testability.
  • Loading branch information
RyanLamansky committed Aug 24, 2024
1 parent 6c260ac commit 50c5b2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions HtmlUtilities.Tests/Validated/Standardized/StyleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Buffers;
using System.Text;

namespace HtmlUtilities.Validated.Standardized;

public static class StyleTests
{
[Fact]
public static void StyleMedia()
{
Assert.Equal("<style media=test></style>", new Style { Media = "test" }.ToString());
}
}
17 changes: 15 additions & 2 deletions HtmlUtilities/Validated/StandardElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.CompilerServices;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Text;

namespace HtmlUtilities.Validated;

Expand All @@ -12,7 +14,7 @@ public abstract class StandardElement
private readonly Dictionary<string, ValidatedAttributeValue> attributes = [];

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

private protected void SetAttribute(ValidatedAttributeValue? value, [CallerMemberName] string name = null!)
{
Expand Down Expand Up @@ -58,4 +60,15 @@ private protected void Write(AttributeWriter writer)
writer.Write(" id"u8, Id);
writer.Write(" title"u8, Title);
}

/// <inheritdoc/>
public override string ToString()
{
var buffer = new ArrayBufferWriter<byte>();
var writer = new HtmlWriter(buffer);

Write(writer);

return Encoding.UTF8.GetString(buffer.WrittenSpan);
}
}

0 comments on commit 50c5b2c

Please sign in to comment.