Skip to content
Merged
15 changes: 15 additions & 0 deletions src/DocumentFormat.OpenXml.Framework/OpenXmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,21 @@ public bool IsBefore(OpenXmlElement element)
return GetOrder(this, element) == ElementOrder.Before;
}

/// <summary>
/// Determines if the specified element is a valid child of the current element.
/// </summary>
/// <param name="element">The element to check.</param>
/// <returns>True if the specified element is a valid child; otherwise, false.</returns>
public bool IsValidChild(OpenXmlElement element)
{
if (element is null)
{
throw new ArgumentNullException(nameof(element));
}

return Metadata.Children.Elements.Any(el => el.Type.Name.Equals(element.Metadata.Type.Name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this only apply on OpenXmlCompositeElement? Should this be virtual and default to false and overriden on that type with this implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, updated in latest commit

}

private enum ElementOrder
{
Same, // same element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,4 @@ DocumentFormat.OpenXml.OpenXmlPartWriterSettings.Encoding.set -> void
DocumentFormat.OpenXml.OpenXmlPartWriterSettings.OpenXmlPartWriterSettings() -> void
DocumentFormat.OpenXml.OpenXmlPartWriter.OpenXmlPartWriter(DocumentFormat.OpenXml.Packaging.OpenXmlPart! openXmlPart, DocumentFormat.OpenXml.OpenXmlPartWriterSettings! settings) -> void
DocumentFormat.OpenXml.OpenXmlPartWriter.OpenXmlPartWriter(System.IO.Stream! partStream, DocumentFormat.OpenXml.OpenXmlPartWriterSettings! settings) -> void
DocumentFormat.OpenXml.OpenXmlElement.IsValidChild(DocumentFormat.OpenXml.OpenXmlElement! element) -> bool
38 changes: 38 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlElementTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,43 @@ public void GetOrAddFirstChildTest()
var r2 = p.GetOrAddFirstChild<Run>();
Assert.Same(r, r2);
}

[Fact]
public void IsValidChild_ValidChild_ReturnsTrue()
{
// Arrange
var parentElement = new Paragraph();
var validChild = new Run();

// Act
var result = parentElement.IsValidChild(validChild);

// Assert
Assert.True(result);
}

[Fact]
public void IsValidChild_InvalidChild_ReturnsFalse()
{
// Arrange
var parentElement = new Paragraph();
var invalidChild = new Table();

// Act
var result = parentElement.IsValidChild(invalidChild);

// Assert
Assert.False(result);
}

[Fact]
public void IsValidChild_NullChild_ThrowsArgumentNullException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this throw or should it just return false?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to return false in latest commit

{
// Arrange
var parentElement = new Paragraph();

// Act & Assert
Assert.Throws<ArgumentNullException>(() => parentElement.IsValidChild(null));
}
}
}
Loading