Fix when bodyElements contain sdt, the inserted element is in the wrong position

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1906225 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bida Fan 2022-12-27 07:49:35 +00:00
parent a84c9e0be6
commit 98d51e4139
2 changed files with 17 additions and 1 deletions

View File

@ -842,7 +842,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
cursor.toCursor(newParaPos);
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
if (o instanceof CTP || o instanceof CTTbl || o instanceof CTSdtBlock) {
i++;
}
}

View File

@ -464,6 +464,22 @@ public final class TestXWPFDocument {
}
}
@Test
void testInsertNewParagraphWithSdt() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
doc.createTOC();
doc.createParagraph();
try (XWPFDocument docx = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
XWPFParagraph paragraph = docx.getParagraphArray(0);
XmlCursor xmlCursor = paragraph.getCTP().newCursor();
XWPFParagraph insertNewParagraph = docx.insertNewParagraph(xmlCursor);
assertEquals(insertNewParagraph, docx.getParagraphs().get(0));
assertEquals(insertNewParagraph, docx.getBodyElements().get(1));
}
}
}
@Test
@Disabled("XWPF should be able to write to a new Stream when opened Read-Only")
void testWriteFromReadOnlyOPC() throws Exception {