Bug65292: Manual revert r1884958, Add a paragraph by default when creating a cell and not add a paragraph when loading an existing table cell

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1890042 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sayi 2021-05-20 09:57:32 +00:00
parent 974da9f981
commit cecab7f573
4 changed files with 57 additions and 29 deletions

View File

@ -63,6 +63,7 @@ public class XWPFTableRow {
*/ */
public XWPFTableCell createCell() { public XWPFTableCell createCell() {
XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody()); XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody());
ensureBlockLevelElement(tableCell);
tableCells.add(tableCell); tableCells.add(tableCell);
return tableCell; return tableCell;
} }
@ -87,10 +88,19 @@ public class XWPFTableRow {
public XWPFTableCell addNewTableCell() { public XWPFTableCell addNewTableCell() {
CTTc cell = ctRow.addNewTc(); CTTc cell = ctRow.addNewTc();
XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody()); XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody());
ensureBlockLevelElement(tableCell);
tableCells.add(tableCell); tableCells.add(tableCell);
return 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 * This element specifies the height of the current table row within the
* current table. This height shall be used to determine the resulting * current table. This height shall be used to determine the resulting

View File

@ -542,7 +542,7 @@ class TestXWPFTable {
} }
@Test @Test
void testCreateTable() throws Exception { public void testCreateTable() throws Exception {
// open an empty document // open an empty document
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) { try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
@ -554,23 +554,15 @@ class TestXWPFTable {
// assert the table is empty // assert the table is empty
List<XWPFTableRow> rows = table.getRows(); List<XWPFTableRow> rows = table.getRows();
assertEquals(noRows, rows.size(), "Table has less rows than requested."); assertEquals(noRows, rows.size(), "Table has less rows than requested.");
int row = 0;
for (XWPFTableRow xwpfRow : rows) { for (XWPFTableRow xwpfRow : rows) {
assertNotNull(xwpfRow); assertNotNull(xwpfRow);
assertEquals(noCols, xwpfRow.getTableCells().size(),
"Row has less columns than requested.");
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
XWPFTableCell xwpfCell = xwpfRow.getCell(i); XWPFTableCell xwpfCell = xwpfRow.getCell(i);
assertNotNull(xwpfCell); assertNotNull(xwpfCell);
assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), assertEquals(1, xwpfCell.getParagraphs().size(), "Empty cells should not have one paragraph.");
"Empty cells should not have one paragraph: " + i);
xwpfCell = xwpfRow.getCell(i); xwpfCell = xwpfRow.getCell(i);
assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), assertEquals(1, xwpfCell.getParagraphs().size(), "Calling 'getCell' must not modify cells content.");
"Calling 'getCell' must not modify cells content: " + i);
} }
row++;
} }
doc.getPackage().revert(); doc.getPackage().revert();
} }

View File

@ -23,9 +23,8 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; 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 java.util.List;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
@ -169,16 +168,17 @@ class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// now paragraph or body element initially // cell have at least one paragraph by default
assertEquals(0, cell.getParagraphs().size());
assertEquals(0, cell.getBodyElements().size());
XWPFParagraph p = cell.addParagraph();
assertEquals(1, cell.getParagraphs().size()); assertEquals(1, cell.getParagraphs().size());
assertEquals(1, cell.getBodyElements().size()); assertEquals(1, cell.getBodyElements().size());
assertSame(p, cell.getParagraphArray(0));
assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(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(); doc.close();
} }
@ -189,10 +189,8 @@ class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// cell have no paragraph by default // cell have at least one paragraph by default
assertNull(cell.getParagraphArray(0)); XWPFParagraph p0 = cell.getParagraphArray(0);
XWPFParagraph p0 = cell.addParagraph();
XWPFParagraph p1 = cell.addParagraph(); XWPFParagraph p1 = cell.addParagraph();
cell.addParagraph(); cell.addParagraph();
@ -222,12 +220,7 @@ class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// cell should not have any elements by default // cell have at least one paragraph by default
assertTrue(cell.getParagraphs().isEmpty());
XWPFParagraph p = cell.addParagraph();
assertNotNull(p);
XWPFParagraph p0 = cell.getParagraphArray(0); XWPFParagraph p0 = cell.getParagraphArray(0);
XmlCursor newCursor = p0.getCTP().newCursor(); XmlCursor newCursor = p0.getCTP().newCursor();
cell.insertNewTbl(newCursor); cell.insertNewTbl(newCursor);
@ -273,4 +266,22 @@ class TestXWPFTableCell {
cell.setText("test text 2"); cell.setText("test text 2");
assertEquals("test text 1test text 2", cell.getText()); 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());
}
} }

View File

@ -42,6 +42,21 @@ class TestXWPFTableRow {
} }
} }
@Test
void testCreateCell() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFTable table = doc.createTable(1, 2);
XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.createCell();
assertNotNull(cell);
assertTrue(cell.getParagraphs().size() >= 1);
cell = tr.addNewTableCell();
assertNotNull(cell);
assertTrue(cell.getParagraphs().size() >= 1);
}
}
@Test @Test
void testSetGetCantSplitRow() throws IOException { void testSetGetCantSplitRow() throws IOException {
// create a table // create a table