Skip to content

Commit b1d49f3

Browse files
authored
Add OpenXmlElement.IsValidChild method (#1915)
- closes #1212
1 parent 172ae79 commit b1d49f3

File tree

3 files changed

+83
-17
lines changed

3 files changed

+83
-17
lines changed

src/DocumentFormat.OpenXml.Framework/OpenXmlCompositeElement.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using DocumentFormat.OpenXml.Framework;
55
using System;
6-
using System.Collections;
76
using System.Collections.Generic;
87
using System.Diagnostics;
98
using System.Diagnostics.CodeAnalysis;
@@ -534,6 +533,32 @@ public override void RemoveAllChildren()
534533

535534
#endregion
536535

536+
/// <summary>
537+
/// Determines whether the specified <see cref="OpenXmlElement"/> is a valid child element
538+
/// for this <see cref="OpenXmlCompositeElement"/> according to the schema definition.
539+
/// </summary>
540+
/// <param name="element">The <see cref="OpenXmlElement"/> to validate as a child.</param>
541+
/// <returns>
542+
/// <c>true</c> if the specified element is a valid child; otherwise, <c>false</c>.
543+
/// </returns>
544+
public virtual bool IsValidChild(OpenXmlElement element)
545+
{
546+
if (element is null)
547+
{
548+
return false;
549+
}
550+
551+
foreach (var elem in Metadata.Children.Elements)
552+
{
553+
if (elem.Type.Name.Equals(element.Metadata.Type.Name))
554+
{
555+
return true;
556+
}
557+
}
558+
559+
return false;
560+
}
561+
537562
/// <summary>
538563
/// Saves all of the current node's children to the specified XmlWriter.
539564
/// </summary>

src/DocumentFormat.OpenXml.Framework/PublicAPI/PublicAPI.Shipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,3 +1009,4 @@ DocumentFormat.OpenXml.OpenXmlPartWriterSettings.Encoding.set -> void
10091009
DocumentFormat.OpenXml.OpenXmlPartWriterSettings.OpenXmlPartWriterSettings() -> void
10101010
DocumentFormat.OpenXml.OpenXmlPartWriter.OpenXmlPartWriter(DocumentFormat.OpenXml.Packaging.OpenXmlPart! openXmlPart, DocumentFormat.OpenXml.OpenXmlPartWriterSettings! settings) -> void
10111011
DocumentFormat.OpenXml.OpenXmlPartWriter.OpenXmlPartWriter(System.IO.Stream! partStream, DocumentFormat.OpenXml.OpenXmlPartWriterSettings! settings) -> void
1012+
virtual DocumentFormat.OpenXml.OpenXmlCompositeElement.IsValidChild(DocumentFormat.OpenXml.OpenXmlElement! element) -> bool

test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlElementTest2.cs

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using DocumentFormat.OpenXml.Wordprocessing;
99
using System;
1010
using System.IO;
11-
using System.Xml;
1211
using Xunit;
1312

1413
namespace DocumentFormat.OpenXml.Tests
@@ -198,21 +197,6 @@ public void CanSetNullValue()
198197
Assert.Null(cell.Child);
199198
}
200199

201-
/// <summary>
202-
/// A test for OpenXmlElement.GetOrAddFirstChild.
203-
/// </summary>
204-
[Fact]
205-
public void GetOrAddFirstChildTest()
206-
{
207-
Paragraph p = new();
208-
Run r = p.GetOrAddFirstChild<Run>();
209-
Assert.NotNull(r);
210-
Assert.Same(r, p.GetFirstChild<Run>());
211-
212-
var r2 = p.GetOrAddFirstChild<Run>();
213-
Assert.Same(r, r2);
214-
}
215-
216200
private class WithChildElement : OpenXmlCompositeElement
217201
{
218202
public ChildElement Child
@@ -240,5 +224,61 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
240224
builder.SetSchema(ElementType);
241225
}
242226
}
227+
228+
/// <summary>
229+
/// A test for OpenXmlElement.GetOrAddFirstChild.
230+
/// </summary>
231+
[Fact]
232+
public void GetOrAddFirstChildTest()
233+
{
234+
Paragraph p = new();
235+
Run r = p.GetOrAddFirstChild<Run>();
236+
Assert.NotNull(r);
237+
Assert.Same(r, p.GetFirstChild<Run>());
238+
239+
var r2 = p.GetOrAddFirstChild<Run>();
240+
Assert.Same(r, r2);
241+
}
242+
243+
[Fact]
244+
public void IsValidChild_ValidChild_ReturnsTrue()
245+
{
246+
// Arrange
247+
Paragraph parentElement = new();
248+
Run validChild = new();
249+
250+
// Act
251+
bool result = parentElement.IsValidChild(validChild);
252+
253+
// Assert
254+
Assert.True(result);
255+
}
256+
257+
[Fact]
258+
public void IsValidChild_InvalidChild_ReturnsFalse()
259+
{
260+
// Arrange
261+
Paragraph parentElement = new();
262+
Table invalidChild = new();
263+
264+
// Act
265+
bool result = parentElement.IsValidChild(invalidChild);
266+
267+
// Assert
268+
Assert.False(result);
269+
}
270+
271+
[Fact]
272+
public void IsValidChild_NullChild_ReturnsFalse()
273+
{
274+
// Arrange
275+
Paragraph parentElement = new();
276+
277+
// Act
278+
bool result = parentElement.IsValidChild(null);
279+
280+
// Assert
281+
Assert.False(result);
282+
}
243283
}
244284
}

0 commit comments

Comments
 (0)