Author: fanningpj
Date: Fri Aug 18 08:48:18 2023
New Revision: 1911749

URL: http://svn.apache.org/viewvc?rev=1911749&view=rev
Log:
[bug-66988] Fully replace content of XWPFTableCell on setText. Thanks to Anton 
Oellerer. This closes #503

Added:
    poi/trunk/test-data/document/Bug66988.docx   (with props)
Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
    
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java?rev=1911749&r1=1911748&r2=1911749&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
 Fri Aug 18 08:48:18 2023
@@ -421,8 +421,29 @@ public class XWPFTableCell implements IB
         return text.toString();
     }
 
+    /**
+     * Set the text of the cell to the passed string, replacing previous 
content. Up until POI 5.2.3, this method appended the text, which is now done
+     * by {@link XWPFTableCell#appendText(String)}.
+     *
+     * @param text The text to replace the cell content with
+     */
     public void setText(String text) {
         XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : 
paragraphs.get(0);
+        while (!par.runsIsEmpty()) {
+            par.removeRun(0);
+        }
+        par.createRun().setText(text);
+    }
+
+    /**
+     * Append the passed string to the cell content.
+     * This was the behaviour of {@link XWPFTableCell#setText(String)} before 
POI 5.2.4
+     *
+     * @param text The text to append to the cells content.
+     * @since POI 5.2.4
+     */
+    public void appendText(String text) {
+        XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : 
paragraphs.get(0);
         par.createRun().setText(text);
     }
 

Modified: 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java?rev=1911749&r1=1911748&r2=1911749&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
 Fri Aug 18 08:48:18 2023
@@ -329,6 +329,17 @@ class TestXWPFBugs {
         }
     }
 
+    @Test
+    void bug66988() throws IOException {
+        try (XWPFDocument document = 
XWPFTestDataSamples.openSampleDocument("Bug66988.docx")) {
+            XWPFTableCell cell = 
document.getTableArray(0).getRow(0).getCell(0);
+            cell.appendText("World");
+            assertEquals("HelloWorld", cell.getText());
+            cell.setText("FooBar");
+            assertEquals("FooBar", cell.getText());
+        }
+    }
+
     private static void addNumberingWithAbstractId(XWPFNumbering 
documentNumbering, int id){
         // create a numbering scheme
         CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();

Added: poi/trunk/test-data/document/Bug66988.docx
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/document/Bug66988.docx?rev=1911749&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/Bug66988.docx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Fri Aug 18 08:48:18 2023
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.wordprocessingml.document



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org
For additional commands, e-mail: commits-h...@poi.apache.org

Reply via email to