Skip to content

Commit

Permalink
nullable AllowedAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Overrided committed Jan 30, 2025
1 parent 5caec57 commit 8b6c001
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
26 changes: 13 additions & 13 deletions Fb2.Document.Tests/ModelsTests/Fb2NodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task AddAttribute_NoAttributesAllowed_Throws(Fb2Node instance)
{
instance.Should().NotBe(null);

if (instance.AllowedAttributes.Any())
if (instance.HasAllowedAttributes)
return;

instance
Expand Down Expand Up @@ -212,7 +212,7 @@ public void AddAttribute_EmptyOrNull_Throws(Fb2Node instance)
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

instance
Expand Down Expand Up @@ -252,7 +252,7 @@ public void AddAttribute_InvalidAttribute_Throws(Fb2Node instance)
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

instance
Expand Down Expand Up @@ -293,7 +293,7 @@ public void AddAttribute_Whitespaces_Throws(Fb2Node instance)
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

var exc = instance // whitespace
Expand Down Expand Up @@ -336,7 +336,7 @@ public void AddAttribute_NotAllowedAttribute_Throws(Fb2Node instance)
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

instance
Expand All @@ -351,7 +351,7 @@ public void AddAttribute_AllowedAttribute_Works(Fb2Node instance)
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

var firstAlowedAttributeName = instance.AllowedAttributes.First();
Expand All @@ -369,7 +369,7 @@ public void AddAttribute_AllowedAttribute_EscapesAttributeValue_Works(Fb2Node in
{
instance.Should().NotBe(null);

if (!instance.AllowedAttributes.Any())
if (!instance.HasAllowedAttributes)
return;

var firstAlowedAttributeName = instance.AllowedAttributes.First();
Expand Down Expand Up @@ -485,7 +485,7 @@ public void Fb2Node_RemoveAttributes_ByPredicate()
[ClassData(typeof(Fb2NodeCollection))]
public void Fb2Node_RemoveAttribute_InvalidAttribute_Fails(Fb2Node instance)
{
if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance
Expand Down Expand Up @@ -518,7 +518,7 @@ public void Fb2Node_RemoveAttribute_InvalidAttribute_Fails(Fb2Node instance)
[ClassData(typeof(Fb2NodeCollection))]
public void Fb2Node_EmptyNode_RemoveAttribute_Ignored(Fb2Node instance)
{
if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance
Expand Down Expand Up @@ -566,7 +566,7 @@ public void Fb2Node_HasAttribute_NullAttribute_Throws(Fb2Node instance)
{
instance.HasAttributes.Should().BeFalse();

if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance
Expand All @@ -591,7 +591,7 @@ public void EmptyNode_HasAttribute_ReturnsFalse(Fb2Node instance)
{
instance.HasAttributes.Should().BeFalse();

if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance.Attributes.Should().BeEmpty();
Expand All @@ -607,7 +607,7 @@ public void EmptyNode_HasAttribute_ReturnsFalse(Fb2Node instance)
[ClassData(typeof(Fb2NodeCollection))]
public void EmptyNode_GetAttribute_ReturnsDefaultKeyValuePair(Fb2Node instance)
{
if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance.Attributes.Should().BeEmpty();
Expand All @@ -620,7 +620,7 @@ public void EmptyNode_GetAttribute_ReturnsDefaultKeyValuePair(Fb2Node instance)
[ClassData(typeof(Fb2NodeCollection))]
public void EmptyNode_TryGetAttribute_ReturnsFalse(Fb2Node instance)
{
if (instance.AllowedAttributes.Count == 0)
if (!instance.HasAllowedAttributes)
return;

instance.Attributes.Should().BeEmpty();
Expand Down
2 changes: 1 addition & 1 deletion Fb2.Document/Models/Base/Core/Fb2Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ private void EnsureContentInitialized(int capacity)
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity), "Should not be less then zero!");

if (content is not null)
if (HasContent)
return;

content = new List<Fb2Node>(capacity);
Expand Down
20 changes: 14 additions & 6 deletions Fb2.Document/Models/Base/Core/Fb2Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public abstract partial class Fb2Node : ICloneable
/// <summary>
/// List of allowed attribure names for particular element.
/// </summary>
public virtual ImmutableHashSet<string> AllowedAttributes => [];
public virtual ImmutableHashSet<string>? AllowedAttributes => null;

/// <summary>
/// Indicates if element has any AllowedAttibutes.
/// </summary>
public bool HasAllowedAttributes => AllowedAttributes is { Count: > 0 };

/// <summary>
/// Indicates if element sholud be inline or start from new line.
Expand Down Expand Up @@ -107,12 +112,12 @@ public virtual void Load(
NodeMetadata = new(defaultNodeNamespace, namespaceDeclarationAttributes);
}

if (AllowedAttributes.IsEmpty)
if (!HasAllowedAttributes)
return;

var allFilteredAttributes = allAttributes
.DistinctBy(a => a.Name.LocalName.ToLowerInvariant())
.Where(da => AllowedAttributes.Contains(da.Name.LocalName.ToLowerInvariant()))
.Where(da => AllowedAttributes!.Contains(da.Name.LocalName.ToLowerInvariant()))
.Select(attr =>
{
var allowedAttrName = attr.Name.LocalName.ToLowerInvariant();
Expand Down Expand Up @@ -353,12 +358,12 @@ public Fb2Node AddAttribute(Fb2Attribute fb2Attribute)
{
ArgumentNullException.ThrowIfNull(fb2Attribute, nameof(fb2Attribute));

if (AllowedAttributes.IsEmpty)
if (!HasAllowedAttributes)
throw new NoAttributesAllowedException(Name);

var key = fb2Attribute.Key;

if (!AllowedAttributes.Contains(key))
if (!AllowedAttributes!.Contains(key))
throw new UnexpectedAttributeException(Name, key);

EnsureAttributesInitialized(1);
Expand Down Expand Up @@ -534,6 +539,9 @@ private bool AreAttributesEqual(List<Fb2Attribute>? otherAttributes)

private void EnsureAttributesInitialized(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity), "Should not be less then zero!");

if (HasAttributes)
return;

Expand All @@ -560,7 +568,7 @@ protected static Fb2Node CloneNodeInternal(Fb2Node node)
cloneNode.IsUnsafe = node.IsUnsafe;

if (node.HasAttributes)
cloneNode.attributes = node.attributes
cloneNode.attributes = node.attributes!
.Select(a => new Fb2Attribute(a))
.ToList();

Expand Down

0 comments on commit 8b6c001

Please sign in to comment.