Skip to content

fix(raw): preserve insertbefore fragment order - #230

Open
Nagi-Inaba wants to merge 1 commit into
iOfficeAI:mainfrom
Nagi-Inaba:agent/fix-raw-set-insertbefore-order
Open

fix(raw): preserve insertbefore fragment order#230
Nagi-Inaba wants to merge 1 commit into
iOfficeAI:mainfrom
Nagi-Inaba:agent/fix-raw-set-insertbefore-order

Conversation

@Nagi-Inaba

Copy link
Copy Markdown

Summary

  • preserve the source order of multi-element XML fragments passed to raw-set --action insertbefore
  • remove the reverse iteration that caused A, B, C to be written as C, B, A

Root cause

XElement.AddBeforeSelf inserts each element immediately before the same anchor while retaining previously inserted siblings. Iterating the fragment in reverse therefore persisted the reverse of the caller-supplied order.

User impact

Raw XML edits using insertbefore no longer silently reorder the supplied fragment.

Validation

Built the current source with .NET SDK 10.0.302:

dotnet build src/officecli/officecli.csproj --no-restore --nologo
Build succeeded: 0 errors

Ran the same command-level reproduction before and after the change against a disposable DOCX:

officecli create issue222.docx
officecli raw-set issue222.docx /document \
  --xpath '//w:sectPr' \
  --action insertbefore \
  --xml '<w:p><w:r><w:t>ISSUE222_A</w:t></w:r></w:p><w:p><w:r><w:t>ISSUE222_B</w:t></w:r></w:p><w:p><w:r><w:t>ISSUE222_C</w:t></w:r></w:p>'
officecli close issue222.docx
unzip -p issue222.docx word/document.xml
Expected: ISSUE222_A,ISSUE222_B,ISSUE222_C
Before:   ISSUE222_C,ISSUE222_B,ISSUE222_A
After:    ISSUE222_A,ISSUE222_B,ISSUE222_C

The build still reports one pre-existing nullable warning in ExcelHandler.SheetShift.cs:538; this change introduces no new warning.

Fixes #222

@Nagi-Inaba
Nagi-Inaba marked this pull request as ready for review July 16, 2026 13:55
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.

# Issue 12: [BUG] raw-set insertbefore reverses the order of multi-element XML fragments

1 participant