Skip to content

Adjusting clearText() to avoid leaving shapes with no paragraphs#1190

Open
dbunner2 wants to merge 2 commits into
apache:trunkfrom
dbunner2:fix/clear-text-invalid-ooxml
Open

Adjusting clearText() to avoid leaving shapes with no paragraphs#1190
dbunner2 wants to merge 2 commits into
apache:trunkfrom
dbunner2:fix/clear-text-invalid-ooxml

Conversation

@dbunner2

Copy link
Copy Markdown

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:

  1. 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.

  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Shape with empty paragraph & textrun generates invalid XML

2 participants