adeu icon indicating copy to clipboard operation
adeu copied to clipboard

A tab in a run that an edit splits is replaced by a space, untracked

Open houfu opened this issue 2 months ago • 1 comments

Summary

When an edit splits a run that also holds a tab, the tab comes back as a space. The change to the tab is not tracked, so neither Accept All nor Reject All restores it. In a contract this turns 1.1<tab>The Supplier … into 1.1 The Supplier … in a clause whose only requested edit was one word further along.

This is at odds with the second guarantee in docs/FIDELITY.md ("Text edits become tracked changes … never silent rewrites").

Reproduce

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

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

doc = Document()
doc.add_paragraph("1.1\tThe Supplier shall deliver the Goods.")  # one run: text, <w:tab/>, text
buf = io.BytesIO(); doc.save(buf)

def body(data):
    xml = zipfile.ZipFile(io.BytesIO(data)).read("word/document.xml").decode()
    return re.search(r"<w:body>(.*?)<w:sectPr", xml, re.S).group(1)

engine = RedlineEngine(io.BytesIO(buf.getvalue()), author="Tester")
engine.process_batch([ModifyText(target_text="deliver", new_text="supply")])
out = engine.save_to_stream().getvalue()
print(body(buf.getvalue()).count("<w:tab/>"), body(out).count("<w:tab/>"))   # 1 0

rejected = RedlineEngine(io.BytesIO(out), author="Tester")
rejected.reject_all_revisions()
print(repr(Document(io.BytesIO(rejected.save_to_stream().getvalue())).paragraphs[0].text))
# '1.1 The Supplier shall deliver the Goods.'   (was '1.1\tThe Supplier shall deliver the Goods.')

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

<w:p>
  <w:r><w:t xml:space="preserve">1.1 The Supplier shall </w:t></w:r>
  <w:del w:id="1" w:author="Tester"><w:r><w:delText>deliver</w:delText></w:r></w:del>
  <w:ins w:id="2" w:author="Tester"><w:r><w:t>supply</w:t></w:r></w:ins>
  <w:r><w:t xml:space="preserve"> the Goods.</w:t></w:r>
</w:p>

The leading run now holds a space where <w:tab/> was. It looks as if the run is rebuilt from its projected text, in which a tab reads as a space; I have not traced the code path.

Expected

The run is split around the edit with its <w:tab/> kept as it was.

Workaround in use

Before handing the document to the engine, move every <w:tab/> into a run of its own, copying the run properties. A run that holds only the tab is never split, and the tab survives.

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