Skip to content

blocks.deleteRange cannot delete across a section break, or a range ending on the paragraph that holds one #4140

Open

Description

@ohcedar

Summary

blocks.deleteRange cannot delete a range that crosses a section break. It also refuses a range inside one section when the range's last paragraph holds the w:sectPr (tracked mode accepts that only when the range is that one paragraph). Both fail with text-range-cross-section-boundary. Deleting the blocks one at a time with blocks.delete gets past the refusal, but once the sectPr paragraph is gone the text before it falls under the next section's page setup.

So there is no single supported call that removes a range spanning a section break and keeps the retained text's page setup. The use case is removing a duplicated tail from a document while keeping the first part and its layout. The fixture below is synthetic.

Versions

@superdoc/sdk 2.26.0 (createSuperDocClient / client.open, nothing else in between), Node 24.21.0, macOS arm64. The fixture is built with docx 9.7.1, which puts each non-final section's sectPr on its own empty paragraph; the block indices below depend on that.

Observed

Three sections of two paragraphs each, with left margins of 1800, 1000 and 720 twips. Blocks: 0 A1, 1 A2, 2 (empty, sectPr 1800), 3 B1, 4 B2, 5 (empty, sectPr 1000), 6 C1, 7 C2.

blocks.deleteRange direct tracked
C2..C2 (one paragraph in the final section) succeeds succeeds
B1..B2 (inside the middle section) succeeds succeeds
[2]..[2] (the empty paragraph holding section 1's sectPr) text-range-cross-section-boundary succeeds, recording a section-break deletion suggestion
A2..[2] (same section, ends on the sectPr paragraph) text-range-cross-section-boundary text-range-cross-section-boundary
A2..B1 (crosses the break) text-range-cross-section-boundary text-range-cross-section-boundary
C1..C2 (every paragraph of the final section) succeeds not in the script

Checked separately from the script below: moving each sectPr onto the text paragraph before it (A2, B2) instead of an empty paragraph gives the same refusals.

Other observations from the same script:

  1. blocks.delete on the sectPr paragraph succeeds in direct mode, and A1 and A2 then sit under the next section's margin (1000 instead of 1800).
  2. In direct mode, after deleting every paragraph of the final section, sections.list reports the ranges {3..5} and {5..5}.
  3. On the untouched document sections.list gives the final section startParagraphIndex == endParagraphIndex ({6..6}) although it has two paragraphs. I can't tell from the contract whether that is intended.
  4. blocks.split on a sectPr paragraph refuses with paragraph-sectpr-boundary.

Workaround, for this fixture only

A caller can get the intended result with public calls: first set the final section's left margin to the retained section's value with sections.setPageMargins, then delete backward section by section, deleting each sectPr paragraph on its own (the last case in the output). That only carries over the one property this fixture varies; every other differing section property needs its own call.

Request

A supported range delete across a section break that keeps the retained text's page setup, headers and footers. It would also help to know whether the direct and tracked difference on the lone sectPr paragraph is intended.

Repro script and output
import { mkdtemp, writeFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { createSuperDocClient } from "@superdoc/sdk";
import { Document, Packer, Paragraph } from "docx";
import JSZip from "jszip";

// Three sections. `docx` puts each non-final section's sectPr on an extra empty paragraph.
const margin = (left) => ({ page: { margin: { left } } });
const bytes = await Packer.toBuffer(new Document({ sections: [
  { properties: margin(1800), children: [new Paragraph("A1"), new Paragraph("A2")] },
  { properties: margin(1000), children: [new Paragraph("B1"), new Paragraph("B2")] },
  { properties: margin(720), children: [new Paragraph("C1"), new Paragraph("C2")] },
] }));
const dir = await mkdtemp(path.join(tmpdir(), "section-delete-"));
const file = path.join(dir, "fixture.docx");
await writeFile(file, bytes);
const client = createSuperDocClient({ stateDir: path.join(dir, "state"), user: { name: "Fixture Reviewer", email: "fixture@example.test" } });
await client.connect();

const body = async (doc) => {
  const zip = await JSZip.loadAsync(Buffer.from((await doc.export.toDocx({})).contentBase64, "base64"));
  const xml = await zip.file("word/document.xml").async("string");
  return [...xml.matchAll(/<w:p[ >].*?<\/w:p>|<w:sectPr.*?<\/w:sectPr>/gs)].map(([el]) => {
    const text = [...el.matchAll(/<w:t[^>]*>([^<]*)</g)].map((m) => m[1]).join("");
    const left = el.match(/w:left="(\d+)"/)?.[1];
    return (el.startsWith("<w:sectPr") ? "body" : JSON.stringify(text)) + (left ? `[sectPr left=${left}]` : "");
  }).join(" ");
};
async function run(label, work) {
  const doc = await client.open({ doc: file });
  try {
    const blocks = (await doc.blocks.list({ includeText: true })).blocks;
    const at = (i) => ({ kind: "block", nodeType: blocks[i].nodeType, nodeId: blocks[i].nodeId });
    let outcome;
    try { outcome = JSON.stringify(await work(doc, at)); } catch (error) { outcome = `THROWS ${error.code}: ${error.message}`; }
    console.log(`${label}\n  -> ${outcome}\n  body: ${await body(doc)}`);
  } finally { await doc.close({ discard: true }); }
}
const brief = (r) => ({ success: r.success, deletedCount: r.deletedCount, failure: r.failure });
// blocks: 0 A1, 1 A2, 2 (empty, sectPr 1800), 3 B1, 4 B2, 5 (empty, sectPr 1000), 6 C1, 7 C2
for (const changeMode of ["direct", "tracked"]) {
  await run(`${changeMode} deleteRange C2..C2 (one paragraph in the final section)`, async (doc, at) => brief(await doc.blocks.deleteRange({ start: at(7), end: at(7), changeMode })));
  await run(`${changeMode} deleteRange B1..B2 (inside the middle section)`, async (doc, at) => brief(await doc.blocks.deleteRange({ start: at(3), end: at(4), changeMode })));
  await run(`${changeMode} deleteRange [2]..[2] (the empty paragraph that holds section 1's sectPr)`, async (doc, at) => brief(await doc.blocks.deleteRange({ start: at(2), end: at(2), changeMode })));
  await run(`${changeMode} deleteRange A2..[2] (same section, ends on the sectPr paragraph)`, async (doc, at) => brief(await doc.blocks.deleteRange({ start: at(1), end: at(2), changeMode })));
  await run(`${changeMode} deleteRange A2..B1 (crosses the break)`, async (doc, at) => brief(await doc.blocks.deleteRange({ start: at(1), end: at(3), changeMode })));
}
await run("direct blocks.delete [2] (the sectPr paragraph)", async (doc, at) => brief(await doc.blocks.delete({ target: at(2), changeMode: "direct" })));
await run("direct deleteRange C1..C2 (every paragraph of the final section)", async (doc, at) => {
  const result = brief(await doc.blocks.deleteRange({ start: at(6), end: at(7), changeMode: "direct" }));
  return { ...result, sections: (await doc.sections.list({})).items.map((s) => s.range) };
});
await run("blocks.split on the sectPr paragraph", async (doc, at) => brief(await doc.blocks.split({ target: at(2), offset: 0, changeMode: "direct" })));
await run("sections.list ranges, untouched document", async (doc) => (await doc.sections.list({})).items.map((s) => s.range));
await run("workaround: delete A2..C2 per section, sectPr paragraphs one by one, then restore the retained section's margin", async (doc, at) => {
  await doc.sections.setPageMargins({ target: { kind: "section", sectionId: "section-2" }, left: 1800 / 1440, changeMode: "direct" });
  await doc.blocks.deleteRange({ start: at(6), end: at(7), changeMode: "direct" });
  await doc.blocks.delete({ target: at(5), changeMode: "direct" });
  await doc.blocks.deleteRange({ start: at(3), end: at(4), changeMode: "direct" });
  await doc.blocks.delete({ target: at(2), changeMode: "direct" });
  return brief(await doc.blocks.deleteRange({ start: at(1), end: at(1), changeMode: "direct" }));
});
await client.dispose();
await rm(dir, { recursive: true, force: true });

Output:

direct deleteRange C2..C2 (one paragraph in the final section)
  -> {"success":true,"deletedCount":1}
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" body[sectPr left=720]
direct deleteRange B1..B2 (inside the middle section)
  -> {"success":true,"deletedCount":2}
  body: "A1" "A2" ""[sectPr left=1800] ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
direct deleteRange [2]..[2] (the empty paragraph that holds section 1's sectPr)
  -> THROWS COMMAND_FAILED: text-range-cross-section-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
direct deleteRange A2..[2] (same section, ends on the sectPr paragraph)
  -> THROWS COMMAND_FAILED: text-range-cross-section-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
direct deleteRange A2..B1 (crosses the break)
  -> THROWS COMMAND_FAILED: text-range-cross-section-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
tracked deleteRange C2..C2 (one paragraph in the final section)
  -> {"success":true,"deletedCount":1}
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "" body[sectPr left=720]
tracked deleteRange B1..B2 (inside the middle section)
  -> {"success":true,"deletedCount":2}
  body: "A1" "A2" ""[sectPr left=1800] "" "" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
tracked deleteRange [2]..[2] (the empty paragraph that holds section 1's sectPr)
  -> {"success":true,"deletedCount":1}
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
tracked deleteRange A2..[2] (same section, ends on the sectPr paragraph)
  -> THROWS COMMAND_FAILED: text-range-cross-section-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
tracked deleteRange A2..B1 (crosses the break)
  -> THROWS COMMAND_FAILED: text-range-cross-section-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
direct blocks.delete [2] (the sectPr paragraph)
  -> {"success":true}
  body: "A1" "A2" "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
direct deleteRange C1..C2 (every paragraph of the final section)
  -> {"success":true,"deletedCount":2,"sections":[{"startParagraphIndex":0,"endParagraphIndex":2},{"startParagraphIndex":3,"endParagraphIndex":5},{"startParagraphIndex":5,"endParagraphIndex":5}]}
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] body[sectPr left=720]
blocks.split on the sectPr paragraph
  -> THROWS COMMAND_FAILED: paragraph-sectpr-boundary
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
sections.list ranges, untouched document
  -> [{"startParagraphIndex":0,"endParagraphIndex":2},{"startParagraphIndex":3,"endParagraphIndex":5},{"startParagraphIndex":6,"endParagraphIndex":6}]
  body: "A1" "A2" ""[sectPr left=1800] "B1" "B2" ""[sectPr left=1000] "C1" "C2" body[sectPr left=720]
workaround: delete A2..C2 per section, sectPr paragraphs one by one, then restore the retained section's margin
  -> {"success":true,"deletedCount":1}
  body: "A1" body[sectPr left=1800]

Activity

  1. added
    status: triage
    on Nov 8, 2026
  2. added theissue type on Nov 8, 2026
  3. added
    status: investigating
    and removed
    status: triage
    on Nov 9, 2026
  4. linear-code commented on Nov 9, 2026

    @linear-code
  5. added
    status: queued
    and removed
    status: investigating
    on Nov 9, 2026
  6. changed the title [-]blocks.deleteRange rejects a same-block deletion in the last section of a multi-section DOCX[/-] [+]blocks.deleteRange cannot delete across a section break, or a range ending on the paragraph that holds one[/+] on Nov 10, 2026
  7. vivienne-sd commented on Nov 23, 2026

    @vivienne-sd
    Collaborator

    Hi @ohcedar , realized I missed the update to you here! We did reproduce the section-break deletion failure: blocks.deleteRange refuses the paragraph carrying the section break in direct mode, and deleting that paragraph individually makes the retained text inherit the following section's margin.

    We're working on the range-deletion issue and have included your requirement to preserve the retained content's page setup. We'll update you here when a fix is live.

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

Metadata

Metadata

Assignees

Labels

status: queued

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions