From 8d0fb1c89d44e3f3191dfefd86b207bbaafa2f7e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 24 Apr 2015 01:29:10 +0000 Subject: [PATCH] #57829 Avoid XmlValueDisconnectedException when removing a XWPFRun from a XWPFParagraph by removing from IRuns as well git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1675738 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFParagraph.java | 6 +++++- .../apache/poi/xwpf/usermodel/TestXWPFBugs.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 7ed629c10a..c98143a5ae 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -1369,8 +1369,12 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public boolean removeRun(int pos){ if (pos >= 0 && pos < paragraph.sizeOfRArray()) { - getCTP().removeR(pos); + // Remove the run from our high level lists + XWPFRun run = runs.get(pos); runs.remove(pos); + iruns.remove(run); + // Remove the run from the low-level XML + getCTP().removeR(pos); return true; } return false; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java index 053b611e3c..8fd7f97e04 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java @@ -88,4 +88,19 @@ public class TestXWPFBugs { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("56392.docx"); assertNotNull(doc); } + + /** + * Removing a run needs to remove it from both Runs and IRuns + */ + @Test + public void test57829() throws Exception { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); + assertNotNull(doc); + assertEquals(3, doc.getParagraphs().size()); + + for (XWPFParagraph paragraph : doc.getParagraphs()) { + paragraph.removeRun(0); + assertNotNull(paragraph.getText()); + } + } }