Repository navigation
Description
Tested on Node.js 24.21.0 with @superdoc/sdk 2.33.0 (native host 0.56.0) and 2.26.0 (native host 0.49.0), docx 9.16.1 and jszip 3.10.2. These SDK versions correspond to the SuperDoc 2.36.0 and 2.29.0 release families.
One tracked structural insert of a Markdown heading and a body paragraph creates a paragraph-formatting revision on the new body paragraph. Its alleged previous style is Heading2, which the caller never requested. Rejecting just that revision, then accepting the remaining insertion, permanently changes the body paragraph into a heading.
Expected: The insertion should track the new heading and body paragraph with their intended styles. It should not offer an independent change to a preceding style that the body paragraph never had in the caller's edit history.
Actual: SDK 2.33 lists two review rows (text insertion and paragraph formatting) for the single insert, so a reviewer sees multiple changes for one edit. The new body paragraph has no explicit current style, but its nested w:pPrChange/w:pPr/w:pStyle records Heading2 as the prior style. SDK 2.26 lists one insertion and does not create this formatting revision.
Reproduction and observed output
Create a synthetic DOCX containing only the ordinary paragraph “Original”. With the pinned docx generator, the relevant style definitions are below; the script adds outline level 1 to Heading2.
Normal: <w:style w:type="paragraph" w:styleId="Normal" w:default="1"><w:name w:val="Normal"/><w:qFormat/></w:style>
Heading2: <w:style w:type="paragraph" w:styleId="Heading2"><w:name w:val="Heading 2"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:qFormat/><w:pPr><w:outlineLvl w:val="1"/></w:pPr><w:rPr><w:color w:val="2E74B5"/><w:sz w:val="26"/><w:szCs w:val="26"/></w:rPr></w:style>
Convert ## Heading\n\nFirst paragraph. with doc.markdownToFragment and insert that fragment once after Original with changeMode: 'tracked'. The sole input paragraph has no heading-style reference or revision, and the converted body paragraph has no heading-style reference or style-edit operation.
SDK 2.33 (output trimmed to the relevant lines; author/date/namespace metadata and generated paragraph/group IDs omitted):
Before [{"text":"Original","type":"paragraph","style":null}]
Changes: []
<w:p><w:r><w:t xml:space="preserve">Original</w:t></w:r></w:p>
Fragment: [{"kind":"heading","heading":{"level":2,"inlines":[{"kind":"run","run":{"text":"Heading"}}]}},{"kind":"paragraph","paragraph":{"inlines":[{"kind":"run","run":{"text":"First paragraph."}}]}}]
After insert [{"text":"Original","type":"paragraph","style":null},{"text":"Heading","type":"heading","style":"Heading2"},{"text":"First paragraph.","type":"paragraph","style":null}]
Changes: ["text-insertion","paragraph-formatting"]
<w:p><w:r><w:t xml:space="preserve">Original</w:t></w:r></w:p>
<w:p><w:pPr><w:pStyle w:val="Heading2" /><w:rPr><w:ins w:id="4"/></w:rPr></w:pPr><w:ins w:id="2"><w:r><w:t>Heading</w:t></w:r></w:ins></w:p>
<w:p><w:pPr><w:rPr><w:ins w:id="1"/></w:rPr><w:pPrChange w:id="3"><w:pPr><w:pStyle w:val="Heading2"/></w:pPr></w:pPrChange></w:pPr><w:ins w:id="5"><w:r><w:t>First paragraph.</w:t></w:r></w:ins></w:p>
Receipt tracked IDs: 2 cover every listed ID: true
Receipt inserted IDs match trackedChangeRefs: true
The outer properties of the first body paragraph contain the inserted paragraph-mark revision; the prior properties inside w:pPrChange claim it previously had Heading2. The false prior state is the defect, not the mere presence of a property revision alongside inserted text.
Save and reopen the pending DOCX, reject only the paragraph-formatting ID, then accept the remaining insertion. Save and reopen the settled file:
Reject only paragraph-formatting ID, then accept remaining insertion
Saved Heading2: <w:style w:type="paragraph" w:styleId="Heading2"><w:name w:val="Heading 2"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:qFormat/><w:pPr><w:outlineLvl w:val="1"/></w:pPr><w:rPr><w:color w:val="2E74B5"/><w:sz w:val="26"/><w:szCs w:val="26"/></w:rPr></w:style>
Saved settled body:
<w:p><w:r><w:t xml:space="preserve">Original</w:t></w:r></w:p>
<w:p><w:pPr><w:pStyle w:val="Heading2" /></w:pPr><w:r><w:t>Heading</w:t></w:r></w:p>
<w:p><w:pPr><w:pStyle w:val="Heading2" /></w:pPr><w:r><w:t>First paragraph.</w:t></w:r></w:p>
Settled after reopen [{"text":"Original","type":"paragraph","style":null},{"text":"Heading","type":"heading","style":"Heading2"},{"text":"First paragraph.","type":"heading","style":"Heading2"}]
Changes: []
Both the saved XML and the reopened SDK inventory show First paragraph as a heading with Heading2, whose saved definition still includes outline level 1. There are no pending changes. No Word application rendering or TOC recalculation was tested.
SDK 2.26, same fixture and insert (excerpt):
After insert [{"text":"Original","type":"paragraph","style":null},{"text":"Heading","type":"heading","style":"Heading2"},{"text":"First paragraph.","type":"paragraph","style":null}]
Changes: ["text-insertion"]
<w:p><w:r><w:t xml:space="preserve">Original</w:t></w:r></w:p>
<w:p><w:pPr><w:pStyle w:val="Heading2" /><w:rPr><w:ins w:id="1"/></w:rPr></w:pPr><w:ins w:id="2"><w:r><w:t>Heading</w:t></w:r></w:ins></w:p>
<w:p><w:pPr><w:rPr><w:ins w:id="3"/></w:rPr></w:pPr><w:ins w:id="4"><w:r><w:t>First paragraph.</w:t></w:r></w:ins></w:p>
Receipt tracked IDs: 1 cover every listed ID: true
accept all receipt IDs in one call: success, pending: 0
...
No separate paragraph-formatting revision to reject
Receipt coverage and whole-operation review: The 2.33 receipt returns all listed IDs in both trackedChangeRefs and inserted. The complete receipt ID array can be passed to one atomic trackChanges.decide call with target: {kind: 'ids', ids}. Accepting all IDs preserves the intended styles; rejecting all IDs restores the original body XML exactly. There is no missing atomic review capability; the extra review row is a symptom of the false style history.
Receipt tracked IDs: 2 cover every listed ID: true
Receipt inserted IDs match trackedChangeRefs: true
accept all receipt IDs in one call: success, pending: 0
Accepted all [{"text":"Original","type":"paragraph","style":null},{"text":"Heading","type":"heading","style":"Heading2"},{"text":"First paragraph.","type":"paragraph","style":null}]
reject all receipt IDs in one call: success, pending: 0
Body XML identical: true
Trigger controls
Reproduces when Heading2's next style is Normal and that style is defined. Removing only w:next prevents it; removing Normal's default flag does not. The output suggests that a transient heading style on the newly constructed following paragraph may be recorded when the style is normalized, rather than being treated as part of the insertion.
| SDK / input | Changes immediately after insert |
|---|---|
| 2.26, tracked | One text insertion |
| 2.33, tracked | One text insertion + paragraph formatting |
| 2.33, remove only Heading2's next-style reference | One text insertion |
| 2.33, remove only Normal's default flag | One text insertion + paragraph formatting |
| 2.33, direct mode | Zero; intended heading and body styles |
Related but different: #3780 concerned unresolvable Markdown insertion; #4158 concerns losing original formatting after rejecting a replacement of an existing paragraph; #4099 concerned tracked heading replacement marking an existing paragraph mark as inserted. Unlike #4078, the receipt here covers all reviewable IDs.
Standalone reproduction
Save as repro.mjs in an empty directory and run:
npm install --save-exact @superdoc/sdk@2.33.0 docx@9.16.1 jszip@3.10.2
node repro.mjs
node repro.mjs --remove-next
node repro.mjs --no-default
node repro.mjs --directFor the baseline, install @superdoc/sdk@2.26.0 instead and run node repro.mjs.
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { tmpdir } from 'node:os';
const require = createRequire(import.meta.url);
const { createSuperDocClient } = require('@superdoc/sdk');
const { Document, Packer, Paragraph } = require('docx');
const JSZip = require('jszip');
const version = JSON.parse(await readFile(join(dirname(require.resolve('@superdoc/sdk')), '..', 'package.json'), 'utf8')).version;
const flags = process.argv.slice(2);
const direct = flags.includes('--direct');
console.log('@superdoc/sdk', version, 'Node', process.version, 'case:', flags.join(' ') || 'tracked');
const dir = await mkdtemp(join(tmpdir(), 'superdoc-'));
const file = join(dir, 'input.docx');
const pending = join(dir, 'pending.docx');
const settled = join(dir, 'settled.docx');
const part = (zip, name) => zip.file(name).async('string');
const body = async zip => (await part(zip, 'word/document.xml')).match(/<w:body>(.*?)<\/w:body>/s)[1];
const style = (xml, id) => xml.match(new RegExp('<w:style\\b[^>]*w:styleId="' + id + '"[^>]*>.*?</w:style>', 's'))?.[0];
const zip = await JSZip.loadAsync(await Packer.toBuffer(new Document({
sections: [{ children: [new Paragraph('Original')] }],
})));
let styles = await part(zip, 'word/styles.xml');
assert.ok(style(styles, 'Normal')?.includes('w:default="1"'), 'Use docx 9.16.1');
assert.ok(style(styles, 'Heading2')?.includes('<w:next w:val="Normal"/>'));
styles = styles.replace(style(styles, 'Heading2'),
style(styles, 'Heading2').replace('<w:rPr>', '<w:pPr><w:outlineLvl w:val="1"/></w:pPr><w:rPr>'));
if (flags.includes('--remove-next')) {
styles = styles.replace(style(styles, 'Heading2'), style(styles, 'Heading2').replace('<w:next w:val="Normal"/>', ''));
}
if (flags.includes('--no-default')) {
styles = styles.replace(style(styles, 'Normal'), style(styles, 'Normal').replace(' w:default="1"', ''));
}
zip.file('word/styles.xml', styles);
console.log('Normal:', style(styles, 'Normal'));
console.log('Heading2:', style(styles, 'Heading2'));
const input = await body(zip);
assert.equal([...input.matchAll(/<w:p(?:\s|>)/g)].length, 1);
assert.match(input, /<w:t[^>]*>Original<\/w:t>/);
assert.doesNotMatch(input, /<w:(?:pStyle|pPrChange|ins|del)\b/);
await writeFile(file, await zip.generateAsync({ type: 'nodebuffer' }));
const exported = async doc => JSZip.loadAsync(Buffer.from((await doc.export.toDocx({})).contentBase64, 'base64'));
// Omit volatile IDs and author/date/namespace metadata; retain Word revision IDs.
const showXml = xml => {
for (const p of xml.matchAll(/<w:p\b[^>]*>.*?<\/w:p>/gs)) {
console.log(p[0].replace(/ (?:w14:paraId|w:rsidR|xmlns:[\w-]+|[\w-]+:Ignorable|w:author|w:date|sdtr\d*:[\w-]+)="[^"]*"/g, ''));
}
};
const rows = async doc => (await doc.trackChanges.list({})).items;
async function describe(doc, label) {
const { blocks } = await doc.blocks.list({ includeText: true });
const items = await rows(doc);
console.log(label, JSON.stringify(blocks.map(b => ({
text: b.text, type: b.nodeType, style: b.styleId ?? null,
}))));
console.log('Changes:', JSON.stringify(items.map(r => r.subtype ?? r.type)));
showXml(await body(await exported(doc)));
return items;
}
async function decide(doc, decision, ids) {
const result = await doc.trackChanges.decide({ decision, target: { kind: 'ids', ids } });
assert.equal(result.receipt?.success ?? result.success, true);
}
const client = createSuperDocClient({ user: { name: 'Reviewer', email: 'reviewer@example.test' }, stateDir: join(dir, 'state') });
await client.connect();
try {
const doc = await client.open({ doc: file });
const original = await body(await exported(doc));
await describe(doc, 'Before');
const [anchor] = (await doc.blocks.list({ includeText: true })).blocks;
const { fragment } = await doc.markdownToFragment({ markdown: '## Heading\n\nFirst paragraph.' });
assert.deepEqual(fragment, [
{ kind: 'heading', heading: { level: 2, inlines: [{ kind: 'run', run: { text: 'Heading' } }] } },
{ kind: 'paragraph', paragraph: { inlines: [{ kind: 'run', run: { text: 'First paragraph.' } }] } },
]);
console.log('Fragment:', JSON.stringify(fragment));
const { receipt } = await doc.insert({
target: { kind: 'block', nodeType: anchor.nodeType, nodeId: anchor.nodeId },
placement: 'after', content: fragment,
}, { changeMode: direct ? 'direct' : 'tracked' });
assert.equal(receipt.success, true);
const items = await describe(doc, 'After insert');
const refs = receipt.trackedChangeRefs ?? receipt.inserted ?? [];
const ids = refs.filter(r => r.entityType === 'trackedChange').map(r => r.entityId);
const coverage = ids.length === items.length && items.every(r => ids.includes(r.id));
console.log('Receipt tracked IDs:', ids.length, 'cover every listed ID:', coverage);
if (receipt.trackedChangeRefs) {
const other = (receipt.inserted ?? []).filter(r => r.entityType === 'trackedChange').map(r => r.entityId);
console.log('Receipt inserted IDs match trackedChangeRefs:', other.length === ids.length && ids.every(id => other.includes(id)));
}
await doc.save({ out: pending, force: true });
await doc.close({ discard: true });
if (!direct) {
// Each branch starts from the same saved pending document.
for (const decision of ['accept', 'reject']) {
const probe = await client.open({ doc: pending });
try {
await decide(probe, decision, ids);
console.log(decision + ' all receipt IDs in one call: success, pending:', (await rows(probe)).length);
if (decision === 'accept') await describe(probe, 'Accepted all');
else console.log('Body XML identical:', await body(await exported(probe)) === original);
} finally {
await probe.close({ discard: true });
}
}
const probe = await client.open({ doc: pending });
try {
const format = (await rows(probe)).find(r => r.subtype === 'paragraph-formatting');
if (format) {
console.log('Reject only paragraph-formatting ID, then accept remaining insertion');
await decide(probe, 'reject', [format.id]);
await decide(probe, 'accept', (await rows(probe)).map(r => r.id));
await probe.save({ out: settled, force: true });
} else {
console.log('No separate paragraph-formatting revision to reject');
}
} finally {
await probe.close({ discard: true });
}
if (items.some(r => r.subtype === 'paragraph-formatting')) {
const saved = await JSZip.loadAsync(await readFile(settled));
console.log('Saved Heading2:', style(await part(saved, 'word/styles.xml'), 'Heading2'));
console.log('Saved settled body:');
showXml(await body(saved));
const reopened = await client.open({ doc: settled });
try {
await describe(reopened, 'Settled after reopen');
} finally {
await reopened.close({ discard: true });
}
}
}
} finally {
await client.dispose();
await rm(dir, { recursive: true, force: true });
}
Activity