Skip to content
Merged
24 changes: 23 additions & 1 deletion src/DocumentFormat.OpenXml.Framework/OpenXmlCompositeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using DocumentFormat.OpenXml.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -534,6 +533,29 @@ public override void RemoveAllChildren()

#endregion

/// <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 override bool IsValidChild(OpenXmlElement element)
{
if (element is null)
{
return false;
}

foreach (var elem in Metadata.Children.Elements)
{
if (elem.Type.Name.Equals(element.Metadata.Type.Name))
{
return true;
}
}

return false;
}

/// <summary>
/// Saves all of the current node's children to the specified XmlWriter.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/DocumentFormat.OpenXml.Framework/OpenXmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,16 @@ 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 virtual bool IsValidChild(OpenXmlElement element)
Copy link
Member

Choose a reason for hiding this comment

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

do we need this method on here? Would having it on OpenXmlCompositeElement be enough?

Copy link
Collaborator Author

@mikeebowen mikeebowen May 13, 2025

Choose a reason for hiding this comment

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

I added the virtual method so it would be overridable, but I could make it virtual in OpenXmlCompositeElement

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@twsouthwick see the latest commit

{
return false;
}

private enum ElementOrder
{
Same, // same element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,5 @@ 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
virtual DocumentFormat.OpenXml.OpenXmlElement.IsValidChild(DocumentFormat.OpenXml.OpenXmlElement! element) -> bool
override DocumentFormat.OpenXml.OpenXmlCompositeElement.IsValidChild(DocumentFormat.OpenXml.OpenXmlElement! element) -> bool
72 changes: 56 additions & 16 deletions test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlElementTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.IO;
using System.Xml;
using Xunit;

namespace DocumentFormat.OpenXml.Tests
Expand Down Expand Up @@ -198,21 +197,6 @@ public void CanSetNullValue()
Assert.Null(cell.Child);
}

/// <summary>
/// A test for OpenXmlElement.GetOrAddFirstChild.
/// </summary>
[Fact]
public void GetOrAddFirstChildTest()
{
Paragraph p = new();
Run r = p.GetOrAddFirstChild<Run>();
Assert.NotNull(r);
Assert.Same(r, p.GetFirstChild<Run>());

var r2 = p.GetOrAddFirstChild<Run>();
Assert.Same(r, r2);
}

private class WithChildElement : OpenXmlCompositeElement
{
public ChildElement Child
Expand Down Expand Up @@ -240,5 +224,61 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
builder.SetSchema(ElementType);
}
}

/// <summary>
/// A test for OpenXmlElement.GetOrAddFirstChild.
/// </summary>
[Fact]
public void GetOrAddFirstChildTest()
{
Paragraph p = new();
Run r = p.GetOrAddFirstChild<Run>();
Assert.NotNull(r);
Assert.Same(r, p.GetFirstChild<Run>());

var r2 = p.GetOrAddFirstChild<Run>();
Assert.Same(r, r2);
}

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

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

// Assert
Assert.True(result);
}

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

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

// Assert
Assert.False(result);
}

[Fact]
public void IsValidChild_NullChild_ReturnsFalse()
{
// Arrange
Paragraph parentElement = new();

// Act
bool result = parentElement.IsValidChild(null);

// Assert
Assert.False(result);
}
}
}
Loading