*/
public XWPFTableCell createCell() {
XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody());
+ ensureBlockLevelElement(tableCell);
tableCells.add(tableCell);
return tableCell;
}
public XWPFTableCell addNewTableCell() {
CTTc cell = ctRow.addNewTc();
XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody());
+ ensureBlockLevelElement(tableCell);
tableCells.add(tableCell);
return tableCell;
}
+ private void ensureBlockLevelElement(XWPFTableCell tableCell) {
+ // If a table cell does not include at least one block-level element,
+ // then this document shall be considered corrupt.
+ if (tableCell.getParagraphs().isEmpty()) {
+ tableCell.addParagraph();
+ }
+ }
+
/**
* This element specifies the height of the current table row within the
* current table. This height shall be used to determine the resulting
}
@Test
- void testCreateTable() throws Exception {
+ public void testCreateTable() throws Exception {
// open an empty document
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
// assert the table is empty
List<XWPFTableRow> rows = table.getRows();
assertEquals(noRows, rows.size(), "Table has less rows than requested.");
- int row = 0;
for (XWPFTableRow xwpfRow : rows) {
assertNotNull(xwpfRow);
- assertEquals(noCols, xwpfRow.getTableCells().size(),
- "Row has less columns than requested.");
for (int i = 0; i < 7; i++) {
XWPFTableCell xwpfCell = xwpfRow.getCell(i);
assertNotNull(xwpfCell);
- assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(),
- "Empty cells should not have one paragraph: " + i);
-
+ assertEquals(1, xwpfCell.getParagraphs().size(), "Empty cells should not have one paragraph.");
xwpfCell = xwpfRow.getCell(i);
- assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(),
- "Calling 'getCell' must not modify cells content: " + i);
+ assertEquals(1, xwpfCell.getParagraphs().size(), "Calling 'getCell' must not modify cells content.");
}
-
- row++;
}
doc.getPackage().revert();
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.IOException;
import java.util.List;
import org.apache.poi.xwpf.XWPFTestDataSamples;
XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell();
- // now paragraph or body element initially
- assertEquals(0, cell.getParagraphs().size());
- assertEquals(0, cell.getBodyElements().size());
-
- XWPFParagraph p = cell.addParagraph();
+ // cell have at least one paragraph by default
assertEquals(1, cell.getParagraphs().size());
assertEquals(1, cell.getBodyElements().size());
- assertSame(p, cell.getParagraphArray(0));
assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0));
+ XWPFParagraph p = cell.addParagraph();
+ assertEquals(2, cell.getParagraphs().size());
+ assertEquals(2, cell.getBodyElements().size());
+ assertEquals(p, cell.getParagraphArray(1));
+ assertEquals(cell.getParagraphArray(1), cell.getBodyElements().get(1));
+
doc.close();
}
XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell();
- // cell have no paragraph by default
- assertNull(cell.getParagraphArray(0));
-
- XWPFParagraph p0 = cell.addParagraph();
+ // cell have at least one paragraph by default
+ XWPFParagraph p0 = cell.getParagraphArray(0);
XWPFParagraph p1 = cell.addParagraph();
cell.addParagraph();
XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell();
- // cell should not have any elements by default
- assertTrue(cell.getParagraphs().isEmpty());
-
- XWPFParagraph p = cell.addParagraph();
- assertNotNull(p);
-
+ // cell have at least one paragraph by default
XWPFParagraph p0 = cell.getParagraphArray(0);
XmlCursor newCursor = p0.getCTP().newCursor();
cell.insertNewTbl(newCursor);
cell.setText("test text 2");
assertEquals("test text 1test text 2", cell.getText());
}
+
+ @Test
+ void test65292() throws IOException {
+ XWPFDocument doc = new XWPFDocument();
+ XWPFTable table = doc.createTable(1, 1);
+ XWPFTableCell cell = table.getRow(0).getCell(0);
+
+ // cell have at least one paragraph by default when creating a cell
+ assertEquals(1, cell.getParagraphs().size());
+
+ cell.removeParagraph(0);
+ assertEquals(0, cell.getParagraphs().size());
+
+ // not add a paragraph when loading an existing empty table cell
+ XWPFDocument readDoc = XWPFTestDataSamples.writeOutAndReadBack(doc);
+ XWPFTableCell readCell = readDoc.getTableArray(0).getRow(0).getCell(0);
+ assertEquals(0, readCell.getParagraphs().size());
+ }
}