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 c667046b3c Adjusting clearText() to avoid leaving shapes with no
paragraphs (#1190)
c667046b3c is described below
commit c667046b3c3d5eee37de8975f2fc95869c22a4b5
Author: David Bunner <[email protected]>
AuthorDate: Sat Aug 1 14:31:06 2026 -0400
Adjusting clearText() to avoid leaving shapes with no paragraphs (#1190)
* Adjusting clearText() to avoid leaving shapes with no paragraphs
* cleaning up logs
---------
Co-authored-by: David M. Bunner <[email protected]>
---
.../poi/xslf/usermodel/XSLFTextParagraph.java | 30 ++++++++++++--------
.../apache/poi/xslf/usermodel/XSLFTextShape.java | 33 ++++++++++++++++++++--
.../poi/xslf/usermodel/TestXSLFTextShape.java | 22 +++++++++++++++
3 files changed, 70 insertions(+), 15 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java
index 45febd4fac..f943fbbd56 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java
@@ -1002,15 +1002,14 @@ public class XSLFTextParagraph implements
TextParagraph<XSLFShape,XSLFTextParagr
*/
/* package */ void clearButKeepProperties() {
CTTextParagraph thisP = getXmlObject();
- for (int i=thisP.sizeOfBrArray(); i>0; i--) {
- thisP.removeBr(i-1);
- }
- for (int i=thisP.sizeOfFldArray(); i>0; i--) {
- thisP.removeFld(i-1);
- }
+
+ // Capture the last run's properties before removing anything - _runs
can
+ // include field-backed runs (<a:fld>, e.g. slide number/date
placeholders),
+ // and reading from one after its underlying XML has already been
removed
+ // throws XmlValueDisconnectedException. See
+ // https://github.com/apache/poi/issues/919
if (!_runs.isEmpty()) {
- int size = _runs.size();
- XSLFTextRun lastRun = _runs.get(size-1);
+ XSLFTextRun lastRun = _runs.get(_runs.size() - 1);
CTTextCharacterProperties cpOther = lastRun.getRPr(false);
if (cpOther != null) {
if (thisP.isSetEndParaRPr()) {
@@ -1019,11 +1018,18 @@ public class XSLFTextParagraph implements
TextParagraph<XSLFShape,XSLFTextParagr
CTTextCharacterProperties cp = thisP.addNewEndParaRPr();
cp.set(cpOther);
}
- for (int i=size; i>0; i--) {
- thisP.removeR(i-1);
- }
- _runs.clear();
}
+
+ for (int i=thisP.sizeOfBrArray(); i>0; i--) {
+ thisP.removeBr(i-1);
+ }
+ for (int i=thisP.sizeOfFldArray(); i>0; i--) {
+ thisP.removeFld(i-1);
+ }
+ for (int i=thisP.sizeOfRArray(); i>0; i--) {
+ thisP.removeR(i-1);
+ }
+ _runs.clear();
}
@Override
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextShape.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextShape.java
index feec0df6f8..7ccad8934f 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextShape.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextShape.java
@@ -114,9 +114,23 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
* unset text from this shape
*/
public void clearText() {
- _paragraphs.clear();
+ // Removing every paragraph leaves the text body with zero <a:p>
elements,
+ // which violates the OOXML schema requirement that a text body
contain at
+ // least one paragraph. Here we keep the first paragraph and clear its
+ // contents instead, similarly to the approach in setText(). See
+ // https://github.com/apache/poi/issues/919
+ if (_paragraphs.isEmpty()) {
+ return;
+ }
+
CTTextBody txBody = getTextBody(true);
- txBody.setPArray(null); // remove any existing paragraphs
+ int cntPs = txBody.sizeOfPArray();
+ for (int i = cntPs; i > 1; i--) {
+ txBody.removeP(i - 1);
+ _paragraphs.remove(i - 1);
+ }
+
+ _paragraphs.get(0).clearButKeepProperties();
}
@Override
@@ -743,7 +757,20 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
clearText();
- for (XSLFTextParagraph srcP : otherTS.getTextParagraphs()) {
+ // clearText() leaves one empty paragraph behind to satisfy the OOXML
+ // requirement of at least one paragraph. If the source has paragraphs
of
+ // its own to copy in, remove that leftover empty one first so it
doesn't
+ // end up as a stray leading paragraph in front of the real copied
content.
+ // If the source has no paragraphs either, leave it as that's already
the
+ // state clearText() is supposed to produce. See
+ // https://github.com/apache/poi/issues/919
+ List<XSLFTextParagraph> srcParagraphs = otherTS.getTextParagraphs();
+ if (!srcParagraphs.isEmpty()) {
+ getTextBody(true).removeP(0);
+ _paragraphs.remove(0);
+ }
+
+ for (XSLFTextParagraph srcP : srcParagraphs) {
XSLFTextParagraph tgtP = addNewTextParagraph();
tgtP.copy(srcP);
}
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTextShape.java
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTextShape.java
index 171b33c5ed..4566aadc6b 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTextShape.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTextShape.java
@@ -956,6 +956,28 @@ class TestXSLFTextShape {
}
}
+ // https://github.com/apache/poi/issues/919
+ @Test
+ void testClearTextLeavesOneEmptyParagraph() throws IOException {
+ try (XMLSlideShow ppt = new XMLSlideShow()) {
+ XSLFSlide slide = ppt.createSlide();
+ XSLFAutoShape shape = slide.createAutoShape();
+ XSLFTextParagraph p = shape.addNewTextParagraph();
+ XSLFTextRun r = p.addNewTextRun();
+ r.setText("some text");
+
+ shape.clearText();
+
+ assertEquals(1, shape.getTextParagraphs().size());
+
assertTrue(shape.getTextParagraphs().get(0).getTextRuns().isEmpty());
+
+ try (XMLSlideShow ppt2 =
XSLFTestDataSamples.writeOutAndReadBack(ppt)) {
+ XSLFTextShape reloaded = (XSLFTextShape)
ppt2.getSlides().get(0).getShapes().get(0);
+ assertEquals(1, reloaded.getTextParagraphs().size());
+ }
+ }
+ }
+
@Test
void metroBlob() throws IOException, ReflectiveOperationException {
assumeFalse(xslfOnly);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]