Bug 64838: Do not populate cells with a paragraph when loading an existing document

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884958 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2020-12-30 21:40:06 +00:00
parent 42c4e7d89a
commit 99dc9ea7f3
4 changed files with 30 additions and 17 deletions

View File

@ -82,9 +82,7 @@ public class XWPFTableCell implements IBody, ICell {
this.ctTc = cell; this.ctTc = cell;
this.part = part; this.part = part;
this.tableRow = tableRow; this.tableRow = tableRow;
// NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt.
if (cell.sizeOfPArray() < 1)
cell.addNewP();
bodyElements = new ArrayList<>(); bodyElements = new ArrayList<>();
paragraphs = new ArrayList<>(); paragraphs = new ArrayList<>();
tables = new ArrayList<>(); tables = new ArrayList<>();

View File

@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;

View File

@ -554,15 +554,23 @@ public 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(1, xwpfCell.getParagraphs().size(), "Empty cells should not have one paragraph."); assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(),
"Empty cells should not have one paragraph: " + i);
xwpfCell = xwpfRow.getCell(i); xwpfCell = xwpfRow.getCell(i);
assertEquals(1, xwpfCell.getParagraphs().size(), "Calling 'getCell' must not modify cells content."); assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(),
"Calling 'getCell' must not modify cells content: " + i);
} }
row++;
} }
doc.getPackage().revert(); doc.getPackage().revert();
} }

View File

@ -22,6 +22,8 @@ package org.apache.poi.xwpf.usermodel;
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 org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -170,16 +172,15 @@ public class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// cell have at least one paragraph by default // now paragraph or body element initially
assertEquals(1, cell.getParagraphs().size()); assertEquals(0, cell.getParagraphs().size());
assertEquals(1, cell.getBodyElements().size()); assertEquals(0, cell.getBodyElements().size());
assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0));
XWPFParagraph p = cell.addParagraph(); XWPFParagraph p = cell.addParagraph();
assertEquals(2, cell.getParagraphs().size()); assertEquals(1, cell.getParagraphs().size());
assertEquals(2, cell.getBodyElements().size()); assertEquals(1, cell.getBodyElements().size());
assertEquals(p, cell.getParagraphArray(1)); assertSame(p, cell.getParagraphArray(0));
assertEquals(cell.getParagraphArray(1), cell.getBodyElements().get(1)); assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0));
doc.close(); doc.close();
} }
@ -191,8 +192,10 @@ public class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// cell have at least one paragraph by default // cell have no paragraph by default
XWPFParagraph p0 = cell.getParagraphArray(0); assertNull(cell.getParagraphArray(0));
XWPFParagraph p0 = cell.addParagraph();
XWPFParagraph p1 = cell.addParagraph(); XWPFParagraph p1 = cell.addParagraph();
cell.addParagraph(); cell.addParagraph();
@ -222,7 +225,12 @@ public class TestXWPFTableCell {
XWPFTableRow tr = table.createRow(); XWPFTableRow tr = table.createRow();
XWPFTableCell cell = tr.addNewTableCell(); XWPFTableCell cell = tr.addNewTableCell();
// cell have at least one paragraph by default // cell should not have any elements 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);