You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/10/17 20:11:53 UTC
(poi) branch trunk updated: XWPFParagraph: keep inline SDT runs out of getRuns() (#1430)
This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 27afd38453 XWPFParagraph: keep inline SDT runs out of getRuns() (#1430)
27afd38453 is described below
commit 27afd3845314b2715bef1120a661b95d829b306a
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Sat Oct 17 21:11:47 2026 +0100
XWPFParagraph: keep inline SDT runs out of getRuns() (#1430)
* XWPFParagraph: keep inline SDT runs out of getRuns()
#971 (bug 66263) added runs from inside inline content controls to
XWPFParagraph.getRuns(), but only when they had rPr, and without adding
them to getIRuns(). That shifted getRuns() indexes for existing documents
and made insertNewRun(pos) return null when pos pointed at an SDT run.
The CTSdtBlock branch could never fire inside a w:p.
SDT runs stay reachable via the XWPFSDT in getIRuns() and
XWPFSDTContent.getBodyElements().
Also replace XWPFTable.hasSdtRows with CTTbl.sizeOfSdtArray().
Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
* XWPFTable.createRow: take the column count from the first row, including SDT rows
createRow() read the column count from ctTbl.getTrArray(0), so a table
whose rows are all wrapped in w:sdt got a new row with no cells.
Also use Stream.toList() in the TestXWPFParagraph helpers.
Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
* XWPFTable.removeRow: drop SDT row wrappers left empty
Removing the last row of an SDT-wrapped group left an empty
w:sdt/w:sdtContent in the table. Remove the SDT, and any enclosing SDT
rows that end up empty as a result.
Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
.../apache/poi/xwpf/usermodel/XWPFParagraph.java | 25 ----
.../org/apache/poi/xwpf/usermodel/XWPFTable.java | 56 +++++----
.../poi/xwpf/usermodel/TestXWPFParagraph.java | 133 ++++++++++-----------
.../apache/poi/xwpf/usermodel/TestXWPFTable.java | 70 +++++++++++
4 files changed, 166 insertions(+), 118 deletions(-)
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
index ca179c2d85..190545d7a2 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
@@ -143,21 +143,10 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
if (o instanceof CTSdtBlock block) {
XWPFSDT cc = new XWPFSDT(block, part);
iruns.add(cc);
- CTSdtContentBlock content = block.getSdtContent();
- if (content != null) {
- for (CTP ctp : content.getPArray()) {
- processCTRs(ctp.getRArray());
- }
- }
}
if (o instanceof CTSdtRun run) {
XWPFSDT cc = new XWPFSDT(run, part);
iruns.add(cc);
-
- CTSdtContentRun sdtContent = run.getSdtContent();
- if (sdtContent != null) {
- processCTRs(sdtContent.getRArray());
- }
}
if (o instanceof CTRunTrackChange parentRecord) {
for (CTR r : parentRecord.getRArray()) {
@@ -179,20 +168,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
}
}
- private void processCTRs(CTR[] ctrs) {
- if (ctrs == null) {
- return;
- }
- for (CTR ctr : ctrs) {
- // Only add runs that have formatting properties (RPr).
- // SDT content runs are added to the runs list for formatting access,
- // while the SDT element itself is in iruns for text extraction.
- if (ctr.getRPr() != null) {
- runs.add(new XWPFRun(ctr, (IRunBody) this));
- }
- }
- }
-
@Internal
public CTP getCTP() {
return paragraph;
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
index 00a9cca73f..68e0f25bac 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
@@ -169,7 +169,7 @@ public class XWPFTable implements IBodyElement, ISDTContents {
// is an empty table: I add one row and one column as default
// Check if table has any row-like content (either TR or SDT-wrapped rows)
- if (initRow && table.sizeOfTrArray() == 0 && !hasSdtRows(table)) {
+ if (initRow && table.sizeOfTrArray() == 0 && table.sizeOfSdtArray() == 0) {
createEmptyTable(table);
}
@@ -262,27 +262,6 @@ public class XWPFTable implements IBodyElement, ISDTContents {
//getRows();
}
- /**
- * Check if the table contains any SDT-wrapped rows.
- * This is needed to avoid adding an extra blank row to tables that
- * already have row-like content, even if it's wrapped in SDT elements.
- *
- * @param table the CTTbl to check
- * @return true if the table contains any SDT row elements
- */
- private boolean hasSdtRows(CTTbl table) {
- try (XmlCursor cursor = table.newCursor()) {
- cursor.selectPath("./*");
- while (cursor.toNextSelection()) {
- XmlObject xmlObject = cursor.getObject();
- if (xmlObject instanceof CTSdtRow) {
- return true;
- }
- }
- }
- return false;
- }
-
/**
* @return ctTbl object
*/
@@ -325,8 +304,7 @@ public class XWPFTable implements IBodyElement, ISDTContents {
* @return tableRow
*/
public XWPFTableRow createRow() {
- int sizeCol = ctTbl.sizeOfTrArray() > 0 ? ctTbl.getTrArray(0)
- .sizeOfTcArray() : 0;
+ int sizeCol = tableRows.isEmpty() ? 0 : tableRows.get(0).getCtRow().sizeOfTcArray();
XWPFTableRow tabRow = new XWPFTableRow(ctTbl.addNewTr(), this);
addColumn(tabRow, sizeCol);
tableRows.add(tabRow);
@@ -1332,6 +1310,7 @@ public class XWPFTable implements IBodyElement, ISDTContents {
int idx = trList.indexOf(ctRow);
if (idx >= 0) {
sdtContent.removeTr(idx);
+ removeEmptySdtRows(sdtContent);
}
} else {
// Fallback: try removing from the table directly
@@ -1351,6 +1330,35 @@ public class XWPFTable implements IBodyElement, ISDTContents {
return false;
}
+ /**
+ * Remove the SDT that holds sdtContent once it has no content left,
+ * and likewise any enclosing SDT rows that end up empty.
+ */
+ private static void removeEmptySdtRows(CTSdtContentRow sdtContent) {
+ XmlObject content = sdtContent;
+ while (content instanceof CTSdtContentRow) {
+ XmlObject sdt;
+ XmlObject parent;
+ try (XmlCursor cursor = content.newCursor()) {
+ if (cursor.toFirstChild()) {
+ return;
+ }
+ if (!cursor.toParent()) {
+ return;
+ }
+ sdt = cursor.getObject();
+ if (!cursor.toParent()) {
+ return;
+ }
+ parent = cursor.getObject();
+ }
+ try (XmlCursor cursor = sdt.newCursor()) {
+ cursor.removeXml();
+ }
+ content = parent;
+ }
+ }
+
public List<XWPFTableRow> getRows() {
return Collections.unmodifiableList(tableRows);
}
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
index 18f5e72596..e5281a0cf9 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
@@ -990,6 +990,11 @@ public final class TestXWPFParagraph {
}
}
+ /**
+ * Runs inside an inline SDT belong to the SDT, not to the paragraph:
+ * getRuns() only returns the paragraph's own runs, and the SDT runs
+ * are reachable via the XWPFSDT in getIRuns().
+ */
@Test
void testParagraphWithSdtRunAndContent() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
@@ -1016,22 +1021,14 @@ public final class TestXWPFParagraph {
XWPFParagraph newParagraph = new XWPFParagraph(ctp, doc);
- // getRuns() includes: normal runs (always) + SDT content runs with RPr
- // Before (1) + SDT Run Content (1) + More (1) + After (1) = 4 runs
- List<XWPFRun> runs = newParagraph.getRuns();
- assertEquals(4, runs.size(), "Should have 4 runs (2 normal + 2 SDT with RPr)");
+ assertEquals(List.of("Before ", " After"), runTexts(newParagraph.getRuns()));
- // getIRuns() includes SDT elements, but SDT runs from processCTRs are NOT in iruns
- // Before (1) + SDT (1) + After (1) = 3
List<IRunElement> iruns = newParagraph.getIRuns();
- assertEquals(3, iruns.size(), "Should have 3 elements (SDT runs are not in iruns)");
-
- // Verify text includes SDT content (text is also duplicated)
- String text = newParagraph.getText();
- assertTrue(text.contains("Before"), "Text should contain 'Before'");
- assertTrue(text.contains("SDT Run Content"), "Text should contain 'SDT Run Content'");
- assertTrue(text.contains(" More"), "Text should contain ' More'");
- assertTrue(text.contains("After"), "Text should contain 'After'");
+ assertEquals(3, iruns.size());
+ XWPFSDT sdt = assertInstanceOf(XWPFSDT.class, iruns.get(1));
+ assertEquals(List.of("SDT Run Content", " More"), sdtRunTexts(sdt));
+
+ assertEquals("Before SDT Run Content More After", newParagraph.getText());
}
}
@@ -1059,27 +1056,42 @@ public final class TestXWPFParagraph {
XWPFParagraph newParagraph = new XWPFParagraph(ctp, doc);
- // getRuns() includes: normal run (always) + SDT content runs with RPr
- // First (1) + Middle (1) + Second (1) = 3 runs
- List<XWPFRun> runs = newParagraph.getRuns();
- assertEquals(3, runs.size(), "Should have 3 runs (1 normal + 2 SDT with RPr)");
+ assertEquals(List.of(" Middle "), runTexts(newParagraph.getRuns()));
- // getIRuns() includes SDT elements, but SDT runs from processCTRs are NOT in iruns
- // SDT1 (1) + Middle (1) + SDT2 (1) = 3 (SDT runs are in runs, not iruns)
List<IRunElement> iruns = newParagraph.getIRuns();
- assertEquals(3, iruns.size(), "Should have 3 elements (SDT runs are in runs, not iruns)");
+ assertEquals(3, iruns.size());
+ assertEquals(List.of("First"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns.get(0))));
+ assertEquals(List.of("Second"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns.get(2))));
- // Verify text includes all content
- String text = newParagraph.getText();
- assertTrue(text.contains("First"), "Text should contain 'First' from SDT run");
- assertTrue(text.contains("Middle"), "Text should contain 'Middle' from normal run");
- assertTrue(text.contains("Second"), "Text should contain 'Second' from SDT run");
+ assertEquals("First Middle Second", newParagraph.getText());
+ }
+ }
+
+ /**
+ * Inserting a run by getRuns() index must not be affected by an inline SDT
+ * sitting between the paragraph's own runs.
+ */
+ @Test
+ void testInsertNewRunNextToSdtRun() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTP ctp = doc.createParagraph().getCTP();
+ ctp.addNewR().addNewT().setStringValue("Before ");
+ ctp.addNewSdt().addNewSdtContent().addNewR().addNewT().setStringValue("SDT");
+ ctp.addNewR().addNewT().setStringValue(" After");
+
+ XWPFParagraph p = new XWPFParagraph(ctp, doc);
+ XWPFRun inserted = p.insertNewRun(1);
+ assertNotNull(inserted);
+ inserted.setText("[new]");
+
+ assertEquals(List.of("Before ", "[new]", " After"), runTexts(p.getRuns()));
+ assertEquals("Before SDT[new] After", p.getText());
}
}
/**
- * Bug 66263 — Test SDT runs in paragraph using sample document.
- * Verifies that SDT runs with and without RPr are processed correctly.
+ * Bug 66263 — SDT runs in paragraphs from a sample document,
+ * with and without RPr: both are treated the same way.
*/
@Test
void testSdtRunsFromSampleDocument() throws IOException {
@@ -1088,56 +1100,39 @@ public final class TestXWPFParagraph {
// Paragraph 1: SDT Run with RPr
XWPFParagraph paragraph1 = paragraphs.get(0);
- String paragraph1Text = paragraph1.getText();
- assertTrue(paragraph1Text.contains("Before"), "Paragraph 1 should contain 'Before'");
- assertTrue(paragraph1Text.contains("SDT Run with RPr"), "Paragraph 1 should contain SDT run with RPr");
- assertTrue(paragraph1Text.contains("After"), "Paragraph 1 should contain 'After'");
-
- // getRuns() contains: 2 normal runs (always added) + 1 SDT run (added because it has RPr) = 3 runs
- List<XWPFRun> runs1 = paragraph1.getRuns();
- assertEquals(3, runs1.size(), "Paragraph 1 should have 3 runs (2 normal + 1 SDT with RPr)");
- assertEquals("Before ", runs1.get(0).toString(), "First run should be 'Before'");
- assertEquals("SDT Run with RPr", runs1.get(1).toString(), "Second run should be SDT content");
- assertEquals(" After", runs1.get(2).toString(), "Third run should be 'After'");
-
- // getIRuns() should contain all elements including SDT
+ assertEquals("Before SDT Run with RPr After", paragraph1.getText());
+ assertEquals(List.of("Before ", " After"), runTexts(paragraph1.getRuns()));
List<IRunElement> iruns1 = paragraph1.getIRuns();
- assertEquals(3, iruns1.size(), "Paragraph 1 should have 3 elements in getIRuns() (2 normal runs + 1 SDT)");
+ assertEquals(3, iruns1.size());
+ assertEquals(List.of("SDT Run with RPr"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns1.get(1))));
// Paragraph 2: SDT Run without RPr
XWPFParagraph paragraph2 = paragraphs.get(1);
- String paragraph2Text = paragraph2.getText();
- assertTrue(paragraph2Text.contains("Before No RPr"), "Paragraph 2 should contain 'Before No RPr'");
- assertTrue(paragraph2Text.contains("SDT Run without RPr"), "Paragraph 2 should contain SDT run without RPr");
- assertTrue(paragraph2Text.contains("After"), "Paragraph 2 should contain 'After'");
-
- // getRuns() contains: 2 normal runs (always added) + 0 SDT runs (no RPr) = 2 runs
- List<XWPFRun> runs2 = paragraph2.getRuns();
- assertEquals(2, runs2.size(), "Paragraph 2 should have 2 runs (normal runs only, no SDT runs without RPr)");
- assertEquals("Before No RPr ", runs2.get(0).toString(), "First run should be 'Before No RPr'");
- assertEquals(" After", runs2.get(1).toString(), "Second run should be 'After'");
-
- // getIRuns() should still contain all elements
+ assertEquals("Before No RPr SDT Run without RPr After", paragraph2.getText());
+ assertEquals(List.of("Before No RPr ", " After"), runTexts(paragraph2.getRuns()));
List<IRunElement> iruns2 = paragraph2.getIRuns();
- assertEquals(3, iruns2.size(), "Paragraph 2 should have 3 elements in getIRuns() (2 normal runs + 1 SDT)");
+ assertEquals(3, iruns2.size());
+ assertEquals(List.of("SDT Run without RPr"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns2.get(1))));
// Paragraph 3: Multiple SDT Runs
XWPFParagraph paragraph3 = paragraphs.get(2);
- String paragraph3Text = paragraph3.getText();
- assertTrue(paragraph3Text.contains("First"), "Paragraph 3 should contain 'First' from first SDT");
- assertTrue(paragraph3Text.contains("Middle"), "Paragraph 3 should contain 'Middle'");
- assertTrue(paragraph3Text.contains("Second"), "Paragraph 3 should contain 'Second' from second SDT");
-
- // getRuns() contains: 1 normal run (always added) + 2 SDT runs (both have RPr) = 3 runs
- List<XWPFRun> runs3 = paragraph3.getRuns();
- assertEquals(3, runs3.size(), "Paragraph 3 should have 3 runs (1 normal + 2 SDT runs with RPr)");
- assertTrue(runs3.get(0).toString().contains("First"), "First run should contain 'First'");
- assertEquals(" Middle ", runs3.get(1).toString(), "Second run should be 'Middle'");
- assertTrue(runs3.get(2).toString().contains("Second"), "Third run should contain 'Second'");
-
- // getIRuns() should contain 3 elements (2 SDTs + 1 normal run)
+ assertEquals("First Middle Second", paragraph3.getText());
+ assertEquals(List.of(" Middle "), runTexts(paragraph3.getRuns()));
List<IRunElement> iruns3 = paragraph3.getIRuns();
- assertEquals(3, iruns3.size(), "Paragraph 3 should have 3 elements in getIRuns() (2 SDTs + 1 normal run)");
+ assertEquals(3, iruns3.size());
+ assertEquals(List.of("First"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns3.get(0))));
+ assertEquals(List.of("Second"), sdtRunTexts(assertInstanceOf(XWPFSDT.class, iruns3.get(2))));
}
}
+
+ private static List<String> runTexts(List<XWPFRun> runs) {
+ return runs.stream().map(XWPFRun::text).toList();
+ }
+
+ private static List<String> sdtRunTexts(XWPFSDT sdt) {
+ XWPFSDTContent content = assertInstanceOf(XWPFSDTContent.class, sdt.getContent());
+ return content.getBodyElements().stream()
+ .map(e -> assertInstanceOf(XWPFRun.class, e).text())
+ .toList();
+ }
}
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
index 35a9a7bcb1..587f4c4788 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
@@ -756,6 +756,24 @@ class TestXWPFTable {
}
}
+ @Test
+ void testCreateRowInTableWithOnlySdtRows() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ CTRow innerRow = table.addNewSdt().addNewSdtContent().addNewTr();
+ innerRow.addNewTc().addNewP();
+ innerRow.addNewTc().addNewP();
+ innerRow.addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ XWPFTableRow created = xtab.createRow();
+
+ assertEquals(3, created.getTableCells().size());
+ assertEquals(2, xtab.getNumberOfRows());
+ assertSame(created, xtab.getRow(1));
+ }
+ }
+
@Test
void testTableWithMultipleSdtRows() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
@@ -851,6 +869,58 @@ class TestXWPFTable {
}
}
+ @Test
+ void testRemoveLastRowOfSdtRemovesSdt() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ table.addNewTr().addNewTc().addNewP();
+ table.addNewSdt().addNewSdtContent().addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ assertEquals(2, xtab.getNumberOfRows());
+
+ assertTrue(xtab.removeRow(1));
+ assertEquals(1, xtab.getNumberOfRows());
+ assertEquals(1, table.sizeOfTrArray());
+ assertEquals(0, table.sizeOfSdtArray());
+ }
+ }
+
+ @Test
+ void testRemoveLastRowOfNestedSdtRemovesBothSdts() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ table.addNewTr().addNewTc().addNewP();
+ CTSdtContentRow outerContent = table.addNewSdt().addNewSdtContent();
+ outerContent.addNewSdt().addNewSdtContent().addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ assertEquals(2, xtab.getNumberOfRows());
+
+ assertTrue(xtab.removeRow(1));
+ assertEquals(1, xtab.getNumberOfRows());
+ assertEquals(0, table.sizeOfSdtArray());
+ }
+ }
+
+ @Test
+ void testRemoveRowKeepsSdtWithOtherRows() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ CTSdtContentRow content = table.addNewSdt().addNewSdtContent();
+ content.addNewTr().addNewTc().addNewP();
+ content.addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ assertEquals(2, xtab.getNumberOfRows());
+
+ assertTrue(xtab.removeRow(0));
+ assertEquals(1, xtab.getNumberOfRows());
+ assertEquals(1, table.sizeOfSdtArray());
+ assertEquals(1, table.getSdtArray(0).getSdtContent().sizeOfTrArray());
+ }
+ }
+
@Test
void testRemoveSdtRow() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org