OfficeCLI icon indicating copy to clipboard operation
OfficeCLI copied to clipboard

[BUG] docx: a targeted edit renumbers `w14:paraId` on untouched paragraphs (mc:Choice / mc:Fallback textbox mirrors)

Open Flint8777 opened this issue 3 months ago • 2 comments

Summary

Any edit + save renumbers every w14:paraId value that occurs more than once in word/document.xml, including in parts of the document the command did not target.

Word writes a floating textbox twice — once under mc:Choice (wps:txbx) and once under mc:Fallback (v:textbox) — and gives the mirrored paragraphs in the two branches the same w14:paraId, because they represent the same logical paragraph. OfficeCLI treats those as duplicates and rewrites one side to a fresh sequential value.

The result is package churn on untouched content: on a Word-authored form with 9 floating textboxes, editing one body paragraph renumbered ~30 paragraph ids inside textboxes that the command never addressed. This is the docx counterpart of #267 (pptx: targeted edit re-serializes every untouched slide part).

Environment

  • OfficeCLI: 1.0.144
  • OS: Windows 11 (x64)
  • Source DOCX producer: Microsoft Office Word (Word for Microsoft 365)

Reproduction

  1. In Word, create a document with five paragraphs and one floating textbox containing two lines; save as base.docx. Word writes the textbox twice with identical paraIds.
  2. Edit a paragraph unrelated to the textbox:
cp base.docx after.docx
officecli set after.docx "/body/p[@paraId=5CC78554]" --prop text="Delta four. EDITED" --json
officecli close after.docx
  1. Compare the w14:paraId sequence in document order:
before: 6C8B0091 7DDA6D7B 14294270 7DDA6D7B 14294270 748B7281 40FF4FCB 5CC78554 5FE08DBB
after:  6C8B0091 7DDA6D7B 14294270 7DDA6D7C 7DDA6D7D 748B7281 40FF4FCB 5CC78554 5FE08DBB
                                   ^^^^^^^^ ^^^^^^^^ mc:Fallback mirrors renumbered

The number of renumbered ids tracks the number of duplicated values exactly, not the size of the edit: 1 duplicated pair → 1 renumbered id; 27 duplicated pairs → ~30 renumbered ids.

For contrast, the same visible text edit applied with python-docx leaves all nine ids byte-identical.

Impact (measured, so the blast radius is clear)

Verified not affected in this scenario — Word's Compare Documents on base.docx vs the edited file reports exactly 1 revision (the real text change) for both the OfficeCLI edit and the python-docx control, and comments plus their commentsExtended.xml paraId links survive intact (comment paraIds are unique, so they are never renumbered).

The concrete cost is diff churn: for version-controlled documents, and for review workflows where a human checks what an agent changed, a one-paragraph edit produces dozens of unrelated id changes that bury the real one.

Expected

w14:paraId values of paragraphs the command did not target are preserved byte-for-byte, even when Word's own output repeats a value across an mc:Choice / mc:Fallback pair.

Actual

One of each duplicated pair is rewritten to a new sequential value on every save that touches the package.

Flint8777 avatar Aug 21 '26 12:08 Flint8777

Fixed on main (next release) — excellent report, and the exact before/after paraId sequence made the cause obvious. A floating textbox is written twice (mc:Choice wps:txbx + mc:Fallback v:textbox) with the mirrored paragraphs deliberately sharing a w14:paraId; the uniqueness pass walked every paragraph and renumbered the second occurrence. It now skips paragraphs inside an mc:Fallback branch, so the Choice/Fallback mirror is preserved byte-for-byte — while a genuine duplicate paraId outside a Fallback is still deduped (covered by a regression test). Verified: editing one paragraph leaves the textbox ids untouched and validate passes.

goworm avatar Aug 24 '26 17:08 goworm

Verified fixed in v1.0.145 (commit c4cf59d) with my original repro — thanks!

After editing one body paragraph, all nine w14:paraId values — including the duplicated mc:Choice/mc:Fallback textbox mirrors — are preserved byte-for-byte. Closing.

Flint8777 avatar Aug 26 '26 14:08 Flint8777