File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed
src/DocumentFormat.OpenXml.Framework
test/DocumentFormat.OpenXml.Tests/ofapiTest Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -796,11 +796,30 @@ public void RemoveNamespaceDeclaration(string prefix)
796796 /// Finds the first child element in type T.
797797 /// </summary>
798798 /// <typeparam name="T">Type of element.</typeparam>
799- /// <returns></returns>
799+ /// <returns>The first child element of type T or null </returns>
800800 public T ? GetFirstChild < T > ( )
801801 where T : OpenXmlElement
802802 => ChildElements . First < T > ( ) ;
803803
804+ /// <summary>
805+ /// Finds the first child element of <typeparam ref="T"/> or adds a new element if it does not exist.
806+ /// </summary>
807+ /// <typeparam name="T">Type of element.</typeparam>
808+ /// <returns>The new or existing OpenXmlElement</returns>
809+ public T GetOrAddFirstChild < T > ( )
810+ where T : OpenXmlElement , new ( )
811+ {
812+ var child = GetFirstChild < T > ( ) ;
813+
814+ if ( child is null )
815+ {
816+ child = new T ( ) ;
817+ AppendChild ( child ) ;
818+ }
819+
820+ return child ;
821+ }
822+
804823 /// <summary>
805824 /// Gets the OpenXmlElement element that immediately precedes the current OpenXmlElement element.
806825 /// Returns null (Nothing in Visual Basic ) if there is no preceding OpenXmlElement element.
Original file line number Diff line number Diff line change @@ -270,6 +270,7 @@ DocumentFormat.OpenXml.OpenXmlElement.Features.get -> DocumentFormat.OpenXml.Fea
270270DocumentFormat.OpenXml.OpenXmlElement.GetAttribute(string! localName, string! namespaceUri) -> DocumentFormat.OpenXml.OpenXmlAttribute
271271DocumentFormat.OpenXml.OpenXmlElement.GetAttributes() -> System.Collections.Generic.IList<DocumentFormat.OpenXml.OpenXmlAttribute>!
272272DocumentFormat.OpenXml.OpenXmlElement.GetFirstChild<T>() -> T?
273+ DocumentFormat.OpenXml.OpenXmlElement.GetOrAddFirstChild<T>() -> T!
273274DocumentFormat.OpenXml.OpenXmlElement.HasAttributes.get -> bool
274275DocumentFormat.OpenXml.OpenXmlElement.InsertAfterSelf<T>(T! newElement) -> T!
275276DocumentFormat.OpenXml.OpenXmlElement.InsertBeforeSelf<T>(T! newElement) -> T!
Original file line number Diff line number Diff line change @@ -216,5 +216,20 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
216216 private class ChildElement : OpenXmlLeafElement
217217 {
218218 }
219+
220+ /// <summary>
221+ /// A test for OpenXmlElement.GetOrAddFirstChild.
222+ /// </summary>
223+ [ Fact ]
224+ public void GetOrAddFirstChildTest ( )
225+ {
226+ Paragraph p = new ( ) ;
227+ Run r = p . GetOrAddFirstChild < Run > ( ) ;
228+ Assert . NotNull ( r ) ;
229+ Assert . Same ( r , p . GetFirstChild < Run > ( ) ) ;
230+
231+ var r2 = p . GetOrAddFirstChild < Run > ( ) ;
232+ Assert . Same ( r , r2 ) ;
233+ }
219234 }
220235}
You can’t perform that action at this time.
0 commit comments