Adjusting clearText() to avoid leaving shapes with no paragraphs#1190
Open
dbunner2 wants to merge 2 commits into
Open
Adjusting clearText() to avoid leaving shapes with no paragraphs#1190dbunner2 wants to merge 2 commits into
dbunner2 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
XSLFTextShape.clearText() currently removes every paragraph from a text body, leaving zero <a:p> elements. Since the OOXML schema requires at least one paragraph, PowerPoint opens presentations saved after a clearText() call in repair mode.
This change brings clearText() in line with how setText() already behaves: instead of removing every paragraph, it keeps the first paragraph and clears its contents. That preserves a valid text body while still removing all text.
Making this change exposed two existing bugs that clearText() had never hit before:
XSLFTextParagraph.clearButKeepProperties() removed field elements (<a:fld>, such as slide number or date placeholders) before reading the last run's formatting. If the last run was a field, this resulted in an XmlValueDisconnectedException. It also used _runs.size() to determine how many <a:r> elements to remove, even though _runs includes field runs, which could lead to an IndexOutOfBoundsException. The fix captures the last run's formatting before removing anything and removes only the runs that actually exist in the XML.
XSLFTextShape.copy() called clearText() and then appended the source paragraphs, leaving an extra empty paragraph at the beginning and shifting all paragraph indices by one. The fix removes that placeholder paragraph before copying whenever the source contains paragraphs. If the source is empty, the single empty paragraph is left intact, which is already the correct state.
I also added a regression test that verifies both the in-memory state and a save/reopen round trip.
Fixes #919.