Skip to content

compare refuses a paragraph count change in a table cell, a hyperlink change on a final paragraph, and a bookmarked heading beside a hyperlink #453

Open

Description

@hadim

Found while redlining two versions of real documents (not shareable) with compare(edited, author, timestamp, granularity="word", ignore_fields=True, ignore_formatting=True), then checking that accept_all() gives the edited document and reject_all() the original. Three refusals, each reduced below to a synthetic pair. Seen on 0.16.4, and present on v0.17.0 (eb980e9) unless noted.

All scripts share this helper: it writes a minimal package from a body, compares, saves, and checks both resolutions story by story.

Helper (click)
import os, tempfile, zipfile
import rdocx

W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"

def docx(path, body):
    with zipfile.ZipFile(path, "w") as z:
        z.writestr("[Content_Types].xml", '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>')
        z.writestr("_rels/.rels", '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>')
        z.writestr("word/_rels/document.xml.rels", '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdLink" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com/" TargetMode="External"/></Relationships>')
        z.writestr("word/document.xml", f'<w:document xmlns:w="{W}" xmlns:r="{R}"><w:body>{body}<w:sectPr/></w:body></w:document>')

def p(*runs, ppr=""): return "<w:p>" + ppr + "".join(runs) + "</w:p>"
def t(text): return f'<w:r><w:t xml:space="preserve">{text}</w:t></w:r>'
def link(text): return f'<w:hyperlink r:id="rIdLink">{t(text)}</w:hyperlink>'
def cell(*ps): return '<w:tc><w:tcPr><w:tcW w:w="3000" w:type="dxa"/></w:tcPr>' + "".join(ps) + "</w:tc>"
def table(*cells): return "<w:tbl><w:tblPr/><w:tblGrid>" + '<w:gridCol w:w="3000"/>' * len(cells) + "</w:tblGrid><w:tr>" + "".join(cells) + "</w:tr></w:tbl>"
def heading(text, i, name): return p(f'<w:bookmarkStart w:id="{i}" w:name="{name}"/>', t(text), f'<w:bookmarkEnd w:id="{i}"/>', ppr='<w:pPr><w:pStyle w:val="Heading1"/></w:pPr>')

def stories(doc):
    out = {}
    for item in doc.story_items:
        out.setdefault((item.story.kind, item.story.part_name, item.story.owner_index), []).append(item.text or "")
    return out

def check(name, body_a, body_b):
    d = tempfile.mkdtemp()
    a_path, b_path, out = (os.path.join(d, n) for n in ("a.docx", "b.docx", "redline.docx"))
    docx(a_path, body_a); docx(b_path, body_b)
    a = rdocx.Document.open(a_path)
    try:
        a.compare(rdocx.Document.open(b_path), "Reviewer", "2026-11-16T00:00:00Z",
                  granularity="word", ignore_fields=True, ignore_formatting=True)
    except rdocx.RdocxError as error:
        print(f"{name}: compare refuses: {error}"); return
    a.save(out)
    acc, rej = rdocx.Document.open(out), rdocx.Document.open(out)
    acc.accept_all(); rej.reject_all()
    print(f"{name}: accept_all gives B: {stories(acc) == stories(rdocx.Document.open(b_path))}, "
          f"reject_all gives A: {stories(rej) == stories(rdocx.Document.open(a_path))}")

1. A table cell whose paragraphs merge, split, or are inserted or deleted

check("merge in a cell",
      p(t("Lead.")) + table(cell(p(t("Open the window,")), p(t("then wait a while.")), p(t("Close the door."))), cell(p(t("x")))) + p(t("Tail.")),
      p(t("Lead.")) + table(cell(p(t("Open the window.")), p(t("Close the door."))), cell(p(t("x")))) + p(t("Tail.")))
check("split in a cell",
      p(t("Lead.")) + table(cell(p(t("One two three.")), p(t("Four.")))) + p(t("Tail.")),
      p(t("Lead.")) + table(cell(p(t("One two")), p(t("three.")), p(t("Four.")))) + p(t("Tail.")))
merge in a cell: compare refuses: comparison cannot revise cell boundary structures at body/table[1]/row[0]/cell[0]
split in a cell: compare refuses: comparison cannot revise cell boundary structures at body/table[1]/row[0]/cell[0]

compare_cell_from_xml pairs cell children index by index and refuses any change in their number. The same edit in the body, or in a block content control, compares fine: both align their children (align, pair_gaps_by_kind, replace_changed_paragraph_runs) and track an unmatched paragraph whole, mark included.

Expected: a redline whose accept_all() gives B and reject_all() gives A, as for the same paragraphs in the body.

2. A final paragraph that gains or loses a hyperlink, in a cell, the body or a content control

check("one-paragraph cell gains a link",
      p(t("Lead.")) + table(cell(p(t("Name"))), cell(p(t("Call the desk.")))) + p(t("Tail.")),
      p(t("Lead.")) + table(cell(p(t("Name"))), cell(p(t("Write to "), link("the desk")))) + p(t("Tail.")))
check("last body paragraph gains a link",
      p(t("Lead.")) + p(t("Call the desk.")),
      p(t("Lead.")) + p(t("Write to "), link("the desk")))
one-paragraph cell gains a link: compare refuses: cannot remove the final paragraph mark
last body paragraph gains a link: compare refuses: paragraph mark removal requires an adjacent paragraph

A pair whose hyperlink shells differ is replaced in place by compare_paragraph as a deleted paragraph followed by an inserted one. In the middle of a story that resolves both ways. On the last paragraph of a cell, a story or a content control, the inserted paragraph's mark is the terminator, so reject_all would have to remove it and the self-check refuses. The trailing-paragraph rule (mark_previous_paragraph and final_paragraph) already expresses a final deleted paragraph followed by a final inserted one, but the pair never reaches it.

Expected: a redline whose resolutions give back each input.

3. A paragraph that becomes, or stops being, a bookmarked heading in a changed run holding a hyperlink

check("paragraph becomes a bookmarked heading beside a link",
      p(t("Lead.")) + p(t("See "), link("the site"), t(" now.")) + p(t("Old text.")) + p(t("Tail.")),
      p(t("Lead.")) + p(t("Look at "), link("the site"), t(" now.")) + heading("New heading", 1, "_Toc1") + p(t("Tail.")))
paragraph becomes a bookmarked heading beside a link: compare refuses: comparison cannot revise paragraph boundary structures at body/paragraph[2]

The changed run pairs the plain paragraph with the new heading, whose bookmark differs. replace_changed_paragraph_runs would replace the run whole, but declines because the run holds a hyperlink outside any complex field (FieldBalance::Uncarried). The pair then reaches compare_paragraph, whose in-place replacement requires equal bookmarks, and the attributed path refuses the pair. The same pair without the hyperlink in the run compares fine.

Expected: a redline whose resolutions give back each input.

Kept as a refusal: bookmarks renumbered by their producer

A producer that names its heading bookmarks _Toc1, _Toc2, ... in document order renames them when a heading is inserted before others, so a name moves from one heading to another between the two versions. Tracked changes do not revise bookmarks, and a redline holding both versions' bookmarks would hold that name twice. This one stays refused (duplicate bookmark id 2 in authored XML on 0.16.4):

check("renumbered bookmarks",
      heading("Alpha", 1, "_Toc1") + p(t("First body.")) + heading("Beta", 2, "_Toc2") + p(t("Second body.")),
      heading("New", 1, "_Toc1") + p(t("New body.")) + heading("Alpha", 2, "_Toc2") + p(t("First body.")) + heading("Beta", 3, "_Toc3") + p(t("Second body.")))

The caller's workaround is to remove such generated bookmarks from both copies before comparing, or to keep their names stable across versions. rdocx's own TOC rebuild allocates _Toc{N} names in order when a heading has none, so documents rebuilt from scratch at each version meet this.

Also seen, already handled elsewhere

  • On v0.17.0 a word-granularity edit in a run whose w:t holds a character entity reference (&apos;, &amp;) refuses with split text has opaque mixed events. 0.16.4 compares it. Comparison: one run per stretch, one read per parent, references and resplit controls (#446 case 1) #450 fixes it (commit 30e6db2).
  • On 0.16.4 one real pair refused with comparison acceptance does not reproduce the edited stories when the last three paragraphs of the body changed style and text. It compares and resolves on v0.17.0. It could not be reduced to a synthetic pair.
  • With ignore_fields=True a field result that differs (a NUMPAGES in a footer) keeps the original's value, so accept_all gives the original's cached page count there. That is the documented left bias, noted only because a check of "accept gives B on every story" has to leave field results out.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions