From 8553f06b45b39ab864f75946261e84ccc34a0483 Mon Sep 17 00:00:00 2001 From: MARIUSZ KASZEWIAK Date: Thu, 24 Apr 2025 17:28:28 +0100 Subject: [PATCH 1/2] initial commit --- .../Schema/Wordprocessing/Table.cs | 42 ++++++++ .../Schema/Wordprocessing/TableRow.cs | 27 +++++ .../Wordprocessing/TableRowTest.cs | 32 ++++++ .../Wordprocessing/TableTests.cs | 100 ++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs create mode 100644 src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs create mode 100644 test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs create mode 100644 test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs new file mode 100644 index 000000000..e027914cc --- /dev/null +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public partial class Table + { + /// + /// Gets or sets the TableProperties. + /// + public TableProperties? TableProperties + { + get => GetElement(Wordprocessing.TableProperties.ElementType) as TableProperties; + set => SetElement(value, Wordprocessing.TableProperties.ElementType); + } + + /// + /// Gets or sets the TableGrid. + /// + public TableGrid? TableGrid + { + get => GetElement(Wordprocessing.TableGrid.ElementType) as TableGrid; + set => SetElement(value, Wordprocessing.TableGrid.ElementType); + } + + /// + /// Gets the collection of TableRow elements. + /// + public IEnumerable? TableRow + { + get + { + foreach (var element in Elements()) + { + yield return element; + } + } + } + } +} diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs new file mode 100644 index 000000000..61f484bbe --- /dev/null +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + /// + /// Represents a table row in a Wordprocessing document. + /// + public partial class TableRow + { + /// + /// Gets the collection of table cells within the table row. + /// + public IEnumerable? TableCell + { + get + { + foreach (var element in Elements()) + { + yield return element; + } + } + } + } +} diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs new file mode 100644 index 000000000..03544ce9a --- /dev/null +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Linq; +using Xunit; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public class TableRowTest + { + [Fact] + public void TableCellTest() + { + // Arrange + Table table = new Table(); + + // Create table rows and cells + TableRow tr = table.AppendChild(new TableRow()); + TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc1.AppendChild(new Paragraph(new Run(new Text("Text to test")))); + + // Assert + foreach (var cell in tr.TableCell.ToList()) + { + Assert.IsType(cell); + Assert.Equal(tc1, cell); + Assert.Equal("Text to test", cell.InnerText); + } + } + } +} diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs new file mode 100644 index 000000000..38c795ff9 --- /dev/null +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs @@ -0,0 +1,100 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public class TableTests + { + [Fact] + public void TablePropertiesGetterAndSetterTest() + { + // Arrange + var table = new Table(); + var tableProperties = new TableProperties(new TableBorders( + new TopBorder() + { + Val = + new EnumValue(BorderValues.Single), + Size = 24, + }, + new BottomBorder() + { + Val = + new EnumValue(BorderValues.Single), + Size = 24, + })); + + // Act + table.AppendChild(tableProperties); + + // Assert + Assert.NotNull(table.TableProperties); + Assert.Equal(tableProperties, table.TableProperties); + } + + [Fact] + public void TableGridsGetterAndSetterTest() + { + // Arrange + var table = new Table(); + var tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn()); + + // Act + table.AppendChild(tableGrid); + + // Assert + Assert.NotNull(table.TableGrid); + Assert.Equal(tableGrid, table.TableGrid); + } + + [Fact] + public void TableRowTest() + { + // Arrange + Table table = new Table(); + + // Create table rows and cells + TableRow tr = table.AppendChild(new TableRow()); + TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc1.AppendChild(new Paragraph(new Run(new Text("Text 1")))); + + // Specify the table cell content. + TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc2.AppendChild(new Paragraph(new Run(new Text("Text 2")))); + + // Create table rows and cells + TableRow tr2 = table.AppendChild(new TableRow()); + TableCell tc3 = tr2.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc3.AppendChild(new Paragraph(new Run(new Text("Text 3")))); + + // Specify the table cell content. + TableCell tc4 = tr2.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc4.AppendChild(new Paragraph(new Run(new Text("Text 4")))); + + // List to store table rows + List rows = [tr, tr2]; + + // Assert + Assert.NotNull(table.TableRow); + TableRow tableRow = new TableRow(); + Assert.Equal(rows.Count, table.TableRow.Count()); + int i = 0; + foreach (var row in table.TableRow.ToList()) + { + Console.WriteLine(row.InnerText); + Assert.IsType(row); + Assert.Equal(rows[i], row); + i++; + } + } + } +} From 33c831bfe48299cd3ad969dde6205792a6323846 Mon Sep 17 00:00:00 2001 From: MARIUSZ KASZEWIAK Date: Mon, 28 Apr 2025 13:46:21 +0100 Subject: [PATCH 2/2] comments applied --- .../Schema/Wordprocessing/Table.cs | 10 +++--- .../Schema/Wordprocessing/TableRow.cs | 8 ++--- .../Wordprocessing/TableRowTest.cs | 33 ++++++++++++++++--- .../Wordprocessing/TableTests.cs | 30 ++++++++++++----- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs index e027914cc..668f81fed 100644 --- a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs @@ -1,7 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using DocumentFormat.OpenXml.Framework; +using System; using System.Collections.Generic; +using System.Linq; namespace DocumentFormat.OpenXml.Wordprocessing { @@ -28,14 +31,11 @@ public TableGrid? TableGrid /// /// Gets the collection of TableRow elements. /// - public IEnumerable? TableRow + public IEnumerable TableRows { get { - foreach (var element in Elements()) - { - yield return element; - } + return Elements()?.OfType() ?? Enumerable.Empty(); } } } diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs index 61f484bbe..b305c3dbd 100644 --- a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; +using System.Linq; namespace DocumentFormat.OpenXml.Wordprocessing { @@ -13,14 +14,11 @@ public partial class TableRow /// /// Gets the collection of table cells within the table row. /// - public IEnumerable? TableCell + public IEnumerable TableCells { get { - foreach (var element in Elements()) - { - yield return element; - } + return Elements()?.OfType() ?? Enumerable.Empty(); } } } diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs index 03544ce9a..514601a09 100644 --- a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Collections.Generic; using System.Linq; using Xunit; @@ -9,7 +10,7 @@ namespace DocumentFormat.OpenXml.Wordprocessing public class TableRowTest { [Fact] - public void TableCellTest() + public void TableCellsTest() { // Arrange Table table = new Table(); @@ -18,15 +19,37 @@ public void TableCellTest() TableRow tr = table.AppendChild(new TableRow()); TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); - tc1.AppendChild(new Paragraph(new Run(new Text("Text to test")))); + tc1.AppendChild(new Paragraph(new Run(new Text("Text 0")))); + + TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc2.AppendChild(new Paragraph(new Run(new Text("Text 1")))); + + int i = 0; + + // List to store row cells + List cells = new List { tc1, tc2 }; // Assert - foreach (var cell in tr.TableCell.ToList()) + foreach (var cell in tr.TableCells.ToList()) { Assert.IsType(cell); - Assert.Equal(tc1, cell); - Assert.Equal("Text to test", cell.InnerText); + Assert.Equal(cells[i], cell); + Assert.Equal("Text " + i.ToString(), cell.InnerText); + i++; } } + + [Fact] + public void TableRowsShouldBeEmptyRowsAreAdded() + { + // Arrange + Table table = new Table(); + TableRow tr = table.AppendChild(new TableRow()); + + // Assert + Assert.NotNull(tr.TableCells); + Assert.Empty(tr.TableCells); + } } } diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs index 38c795ff9..8c781c7d2 100644 --- a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System; using System.Collections.Generic; using System.Linq; using Xunit; @@ -43,6 +42,7 @@ public void TableGridsGetterAndSetterTest() // Arrange var table = new Table(); var tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn()); + TableGrid tableGrid1 = new TableGrid(); // Act table.AppendChild(tableGrid); @@ -53,12 +53,12 @@ public void TableGridsGetterAndSetterTest() } [Fact] - public void TableRowTest() + public void TableRowsTest() { // Arrange Table table = new Table(); - // Create table rows and cells + // Create table row and cells TableRow tr = table.AppendChild(new TableRow()); TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); @@ -69,7 +69,7 @@ public void TableRowTest() new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); tc2.AppendChild(new Paragraph(new Run(new Text("Text 2")))); - // Create table rows and cells + // Create table row and cells TableRow tr2 = table.AppendChild(new TableRow()); TableCell tc3 = tr2.AppendChild(new TableCell(new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); @@ -82,19 +82,31 @@ public void TableRowTest() // List to store table rows List rows = [tr, tr2]; + table.TableRows.ToList().Append(new TableRow()); // Assert - Assert.NotNull(table.TableRow); - TableRow tableRow = new TableRow(); - Assert.Equal(rows.Count, table.TableRow.Count()); + Assert.NotNull(table.TableRows); + Assert.Equal(rows.Count, table.TableRows.Count()); int i = 0; - foreach (var row in table.TableRow.ToList()) + foreach (var row in table.TableRows.ToList()) { - Console.WriteLine(row.InnerText); Assert.IsType(row); Assert.Equal(rows[i], row); i++; } + + Assert.Equal(rows.Count, table.TableRows.Count()); + } + + [Fact] + public void TableRowsShouldBeEmptyWhenNoCellsAreAdded() + { + // Arrange + Table table = new Table(); + + // Assert + Assert.NotNull(table.TableRows); + Assert.Empty(table.TableRows); } } }