adeu icon indicating copy to clipboard operation
adeu copied to clipboard

Deleting a paragraph with its separator merges the next paragraph into it; Reject All cannot restore the break

Open houfu opened this issue 2 months ago • 1 comments

Summary

A ModifyText whose target_text covers a whole paragraph and the separator after it ("Second paragraph.\n\n" → "") is applied, and the result cannot be undone by rejecting the changes. The runs of the following paragraph are moved into the deleted paragraph's <w:p>, and that move is not tracked. After Reject All the two paragraphs are one.

Reproduce

Python engine, 3.0.6 from PyPI, and main at 965736d2.

import io
from docx import Document
from adeu import ModifyText, RedlineEngine

doc = Document()
for text in ("First paragraph.", "Second paragraph.", "Third paragraph."):
    doc.add_paragraph(text)
buf = io.BytesIO(); doc.save(buf)

engine = RedlineEngine(io.BytesIO(buf.getvalue()), author="Tester")
stats = engine.process_batch([ModifyText(target_text="Second paragraph.\n\n", new_text="")])
print(stats["status"], stats["edits"][0]["status"])          # ok applied
out = engine.save_to_stream().getvalue()

def paragraphs(action):
    e = RedlineEngine(io.BytesIO(out), author="Tester")
    getattr(e, action)()
    return [p.text for p in Document(io.BytesIO(e.save_to_stream().getvalue())).paragraphs]

print(paragraphs("accept_all_revisions"))   # ['First paragraph.', 'Third paragraph.']
print(paragraphs("reject_all_revisions"))   # ['First paragraph.', 'Second paragraph.Third paragraph.']

The body after the edit (dates and the w16du namespace removed):

<w:p><w:r><w:t>First paragraph.</w:t></w:r></w:p>
<w:p>
  <w:pPr><w:rPr><w:del w:id="2" w:author="Tester"/></w:rPr></w:pPr>
  <w:del w:id="1" w:author="Tester"><w:r><w:delText>Second paragraph.</w:delText></w:r></w:del>
  <w:r><w:t>Third paragraph.</w:t></w:r>
</w:p>

The paragraph mark is correctly flagged as deleted, but "Third paragraph." now sits inside the same <w:p>. There is one paragraph element where there were two, so a reject has no second paragraph to give back.

Expected

Either of:

  • the following paragraph stays in its own <w:p> and only the deleted paragraph's mark carries <w:del>, which is how Word records a tracked paragraph deletion and which a reject restores; or
  • the edit is refused. docs/FIDELITY.md lists "unbalanced paragraph merges" among the things rejected with a BatchValidationError. I do not know whether this edit is meant to count as one; it is accepted and reported as applied.

Accept All gives the right result. Only the reject path loses the paragraph break.

Workaround in use

Delete the paragraph's text only ("Second paragraph." → "") and never include the separator in a deletion.

Drafted with Claude Code; the reproduction above was run as shown.

houfu avatar Oct 04 '26 00:10 houfu

Thank you @houfu! This is fixed in main and will be included in the next release.

Git-Uzair avatar Oct 05 '26 09:10 Git-Uzair