Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Fix review findings in the Office CLI

Fei24111e5f08a2d449
diff --git a/README.md b/README.md
index 68fac6e1..4cb018b4 100644
--- a/README.md
+++ b/README.md
@@ -100,7 +100,7 @@ In **Settings** → **Usage**, you can see token usage by time, by bot and by mo
### Office files
-Bots work with Office documents offline. They can create and edit .docx, .pptx and .xlsx, read a document's structure with stable ids, check a package for the defects Office would refuse, make targeted edits (comments, tracked changes, fields, table cells), reorder or duplicate slides, list workbook errors, render pages to images and review them on a contact sheet — nothing needs to be installed, the desktop app includes everything needed.
+Bots work with Office documents. They can create and edit .docx, .pptx and .xlsx, read a document's structure with stable ids, check a package for the defects Office would refuse, make targeted edits (comments, tracked changes, fields, table cells), reorder or duplicate slides, list workbook errors, and review rendered pages on a contact sheet. The desktop app includes everything needed; in DSH Desktop Bots use DSH's Office runtime plus DS Bot's commands; in DSH Web the inspect, check and edit commands work, while creating files from scratch and rendering pages need the desktop app or DSH Desktop.
## Data
diff --git a/README.zh.md b/README.zh.md
index 943a4365..c3799832 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -100,7 +100,7 @@ ds bot 拥有群聊功能。群聊是非常好的多 bot 协作方式,但它
### Office 文件
-Bot 可以离线处理 Office 文档:创建和编辑 .docx、.pptx、.xlsx;按稳定 id 读文档结构;检查会让 Office 拒开的包缺陷;定点编辑(批注、修订、域、表格单元格);幻灯片重排与复制;列出工作簿错误;渲染成图片并按拼图检查——不用安装任何东西,桌面应用已全部带上。
+Bot 可以处理 Office 文档:创建和编辑 .docx、.pptx、.xlsx;按稳定 id 读文档结构;检查会让 Office 拒开的包缺陷;定点编辑(批注、修订、域、表格单元格);幻灯片重排与复制;列出工作簿错误;渲染成图片并按拼图检查。桌面应用已全部带上;在 DSH Desktop 里用 DSH 自己的 Office 运行时加 DS Bot 的命令;在 DSH Web 里检查、校验、编辑类命令可用,从头建文件和渲染页面需要桌面应用或 DSH Desktop。
## 数据
diff --git a/docs/plans/2026-10-27-office-documents.md b/docs/plans/2026-10-27-office-documents.md
index bf4a514a..6da728c1 100644
--- a/docs/plans/2026-10-27-office-documents.md
+++ b/docs/plans/2026-10-27-office-documents.md
@@ -20,7 +20,7 @@ Bot 要能直接处理 .docx/.pptx/.xlsx:打开看结构、检查包健康、
## ds-bot 自己的命令行(`office.js`)
-打进 npm 包的单文件 CLI,只依赖 Node 内建,所有运行面通用(桌面应用、DSH Desktop、DSH Web)。退出码:0 成功、1 仍有 finding、2 用法或失败;`--json` 时输出 `{"ok":true,"data":…}` / `{"ok":false,"error":{…}}`。编辑默认另存(`-o`),`--in-place` 才原地写。
+打进 npm 包的单文件 CLI,依赖 Node 内建加打包进去的 XML 解析库,所有运行面通用(桌面应用、DSH Desktop、DSH Web)。退出码:0 成功、1 仍有 finding、2 用法或失败;`--json` 时输出 `{"ok":true,"data":…}` / `{"ok":false,"error":{…}}`(finding 在 `data.findings` 里,ok 仍为 true)。编辑默认另存(`-o`),`--in-place` 才原地写。
- `unpack`/`pack`:把包解成可读 XML(`pack` 还原排版空白并跑 `check --against`)
- `check [--against] [--fix]`:只查会让 Office 拒开或弹修复的问题——XML 良构、内容类型完整、内部关系可达、r:id 引用存在、pptx 形状/幻灯片 id、docx docPr/paraId、`xml:space`、批注 id 一致。`--against` 只看新引入的问题;`--fix` 修安全的几类
@@ -45,14 +45,15 @@ DSH 的六个格式/操作技能(office-docx/pptx/xlsx、office-render、offic
## 各运行面的运行时
-- 桌面应用:payload 的 Node(host 注入 `DS_BOT_OFFICE_NODE`),office.js 走 `app.asar.unpacked`
-- DSH Desktop / Web 插件:宿主是纯 Node 时用宿主解释器;否则技能文本指示调 `load_workspace_dependencies` 取 payload node
+- 桌面应用:全部可用——payload 的 Python/Node/LibreOffice、DSH 的 Office 技能、`office.js` 全部命令(host 注入 `DS_BOT_OFFICE_NODE`,office.js 走 `app.asar.unpacked`)
+- DSH Desktop:DSH 自带的 Python 和渲染器照常,外加我们的 `office.js` 命令(纯 Node 宿主用宿主解释器;否则技能文本指示调 `load_workspace_dependencies` 取 node)
+- DSH Web:只有 `office.js` 命令——没有随包的 Python(建新文件受限),没有渲染器(除非用户自己装 DSH 提供的 Office sidecar)
## 不做的
- 不做模板/母题生成;排版规则写成技能文本
- 不做 XSD 校验——`check` 只覆盖「Office 拒开/弹修复」这一档
-- docx 的复杂修订细节(字段内修订、编号变更等)按出现再补
+- `accept_changes`/`reject_changes` 不处理嵌套在字段指令里的修订,也不处理 `w:numberingChange` 的内容回滚(编号变更只去标记不还原编号)
## 升 DSH 版本时
diff --git a/ds-bot/dev/office-fixtures.py b/ds-bot/dev/office-fixtures.py
index d4d35ea3..81725cbb 100644
--- a/ds-bot/dev/office-fixtures.py
+++ b/ds-bot/dev/office-fixtures.py
@@ -60,15 +60,16 @@ def touch_docx(path):
data = zin.read(item)
if item == "word/document.xml":
text = data.decode("utf8")
- # A complex PAGE field in the second-to-last paragraph.
- text = text.replace(
- "</w:body>",
- '<w:p><w:r><w:fldChar w:fldCharType="begin"/></w:r>'
- '<w:r><w:instrText xml:space="preserve"> PAGE </w:instrText></w:r>'
- '<w:r><w:fldChar w:fldCharType="separate"/></w:r>'
- '<w:r><w:t>1</w:t></w:r>'
- '<w:r><w:fldChar w:fldCharType="end"/></w:r></w:p>'
- "</w:body>", 1)
+ # A complex PAGE field as the last paragraph — inside the body
+ # before its closing sectPr, never after it.
+ sect = text.rfind("<w:sectPr")
+ text = (text[:sect]
+ + '<w:p><w:r><w:fldChar w:fldCharType="begin"/></w:r>'
+ '<w:r><w:instrText xml:space="preserve"> PAGE </w:instrText></w:r>'
+ '<w:r><w:fldChar w:fldCharType="separate"/></w:r>'
+ '<w:r><w:t>1</w:t></w:r>'
+ '<w:r><w:fldChar w:fldCharType="end"/></w:r></w:p>'
+ + text[sect:])
# Tracked changes in the first real paragraph: inserted word,
# deleted word, moved text, a run-property change, and a
# paragraph whose mark is deleted (merges on accept). Run-level
diff --git a/ds-bot/office.js b/ds-bot/office.js
index bf2efe16..ab2df87b 100644
--- a/ds-bot/office.js
+++ b/ds-bot/office.js
@@ -6519,7 +6519,7 @@ import { join as join2 } from "node:path";
import { parseArgs } from "node:util";
// src/office/package.js
-import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
+import { existsSync, mkdirSync, readFileSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join, posix } from "node:path";
// src/office/xml.js
@@ -6774,6 +6774,7 @@ var absTarget = (base, target) => {
if (target.startsWith("/")) return target.slice(1);
return posix.normalize(posix.join(posix.dirname(base), target));
};
+var relSig = (map) => [...map.values()].map((r) => `${r.id}${r.type}${r.target}${r.mode}`).join("");
var Package = class _Package {
constructor() {
this.parts = /* @__PURE__ */ new Map();
@@ -6837,6 +6838,8 @@ var Package = class _Package {
}
this.rels.set(owner, map);
}
+ this.relSigs = new Map([...this.rels].map(([owner, map]) => [owner, relSig(map)]));
+ this.ctSig = JSON.stringify([...this.defaults].sort().concat([...this.overrides].filter(([n]) => this.parts.has(n)).sort()));
}
/** @returns {Buffer|undefined} part bytes */
get(name) {
@@ -6883,7 +6886,8 @@ var Package = class _Package {
relsName(owner) {
return owner === "" ? "_rels/.rels" : `${posix.dirname(owner)}/_rels/${posix.basename(owner)}.rels`;
}
- /** Write .rels and [Content_Types].xml back into parts from the model. */
+ /** Write .rels and [Content_Types].xml back into parts from the model — only
+ * when the model changed, so untouched members keep their original bytes. */
flush() {
for (const name of [...this.parts.keys()]) if (name.endsWith(".rels") && !this.#ownerStillHasRels(name)) this.parts.delete(name);
for (const [owner, map] of this.rels) {
@@ -6896,6 +6900,8 @@ var Package = class _Package {
this.parts.delete(name);
continue;
}
+ const sig = relSig(map);
+ if (sig === (this.relSigs ??= /* @__PURE__ */ new Map()).get(owner) && this.parts.has(name)) continue;
const doc = parseXml('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>');
for (const rel of map.values()) {
const el = doc.createElement("Relationship");
@@ -6906,22 +6912,27 @@ var Package = class _Package {
doc.documentElement.appendChild(el);
}
this.putXml(name, doc);
+ this.relSigs.set(owner, sig);
+ }
+ const ctSig = JSON.stringify([...this.defaults].sort().concat([...this.overrides].filter(([n]) => this.parts.has(n)).sort()));
+ if (ctSig !== this.ctSig || !this.parts.has("[Content_Types].xml")) {
+ const ct = parseXml('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>');
+ for (const [ext2, type] of this.defaults) {
+ const el = ct.createElement("Default");
+ el.setAttribute("Extension", ext2);
+ el.setAttribute("ContentType", type);
+ ct.documentElement.appendChild(el);
+ }
+ for (const [name, type] of this.overrides) {
+ if (!this.parts.has(name)) continue;
+ const el = ct.createElement("Override");
+ el.setAttribute("PartName", `/${name}`);
+ el.setAttribute("ContentType", type);
+ ct.documentElement.appendChild(el);
+ }
+ this.putXml("[Content_Types].xml", ct);
+ this.ctSig = ctSig;
}
- const ct = parseXml('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>');
- for (const [ext2, type] of this.defaults) {
- const el = ct.createElement("Default");
- el.setAttribute("Extension", ext2);
- el.setAttribute("ContentType", type);
- ct.documentElement.appendChild(el);
- }
- for (const [name, type] of this.overrides) {
- if (!this.parts.has(name)) continue;
- const el = ct.createElement("Override");
- el.setAttribute("PartName", `/${name}`);
- el.setAttribute("ContentType", type);
- ct.documentElement.appendChild(el);
- }
- this.putXml("[Content_Types].xml", ct);
this.parts.delete("");
}
#ownerStillHasRels(name) {
@@ -6933,11 +6944,12 @@ var Package = class _Package {
this.flush();
return writeZip([...this.parts.entries()].map(([name, part]) => ({ name, ...part })));
}
- /** Write an `unpack` directory tree. `dir` must be new or the output of a previous unpack. */
- unpack(dir, previous) {
- if (existsSync(dir)) {
- const names = readdirSync(dir);
- if (names.length > 0 && !(previous && previous === dir)) fail2(`unpack: ${dir} exists and is not an earlier unpack`);
+ /** Write an `unpack` directory tree. An existing non-empty dir must hold an
+ * earlier unpack ([Content_Types].xml at its root); files the package no
+ * longer has are removed. */
+ unpack(dir) {
+ if (existsSync(dir) && readdirSync(dir).length > 0 && !existsSync(join(dir, "[Content_Types].xml"))) {
+ fail2(`unpack: ${dir} exists and is not an earlier unpack`);
}
mkdirSync(dir, { recursive: true });
const keep = /* @__PURE__ */ new Set();
@@ -6955,6 +6967,17 @@ var Package = class _Package {
}
writeFileSync(file, data);
}
+ const sweep = (rel) => {
+ for (const entry of readdirSync(join(dir, rel))) {
+ const path = rel === "" ? entry : `${rel}/${entry}`;
+ const full = join(dir, path);
+ if (statSync(full).isDirectory()) {
+ sweep(path);
+ if (readdirSync(full).length === 0) rmdirSync(full);
+ } else if (!keep.has(full)) unlinkSync(full);
+ }
+ };
+ sweep("");
}
};
function prettyForUnpack(data) {
@@ -7040,17 +7063,70 @@ function runRules(pkg) {
if (name.endsWith(".rels")) continue;
const rels = pkg.relsOf(name);
for (const el of elementsOf(doc.documentElement)) {
- for (const attr of ["r:id", "r:embed", "r:link", "r:dm", "r:lo", "r:qs", "r:cs", "o:id"]) {
+ for (const attr of ["r:id", "r:embed", "r:link", "r:dm", "r:lo", "r:qs", "r:cs"]) {
const value = el.getAttribute?.(attr);
if (value && !rels.has(value)) out.push(finding("unknown-rel-ref", name, `${el.tagName} ${attr}="${value}" has no relationship`));
}
}
}
+ contentTypeRules(pkg, out);
const isPptx = pkg.parts.has("ppt/presentation.xml"), isDocx = pkg.parts.has("word/document.xml");
if (isPptx) pptxRules(pkg, xmlDocs, out);
if (isDocx) docxRules(pkg, xmlDocs, out);
return out;
}
+var CT_BASE = "application/vnd.openxmlformats-officedocument.";
+var pkgKind = (pkg) => pkg.parts.has("word/document.xml") ? "docx" : pkg.parts.has("ppt/presentation.xml") ? "pptx" : pkg.parts.has("xl/workbook.xml") ? "xlsx" : void 0;
+var MAIN_CT = { docx: CT_BASE + "wordprocessingml.document.main+xml", pptx: CT_BASE + "presentationml.presentation.main+xml", xlsx: CT_BASE + "spreadsheetml.sheet.main+xml" };
+var REL_TYPE_CT = {
+ officeDocument: MAIN_CT,
+ document: MAIN_CT,
+ styles: { docx: CT_BASE + "wordprocessingml.styles+xml", xlsx: CT_BASE + "spreadsheetml.styles+xml" },
+ numbering: CT_BASE + "wordprocessingml.numbering+xml",
+ settings: CT_BASE + "wordprocessingml.settings+xml",
+ comments: CT_BASE + "wordprocessingml.comments+xml",
+ footnotes: CT_BASE + "wordprocessingml.footnotes+xml",
+ endnotes: CT_BASE + "wordprocessingml.endnotes+xml",
+ header: CT_BASE + "wordprocessingml.header+xml",
+ footer: CT_BASE + "wordprocessingml.footer+xml",
+ fontTable: CT_BASE + "wordprocessingml.fontTable+xml",
+ theme: CT_BASE + "theme+xml",
+ slide: CT_BASE + "presentationml.slide+xml",
+ slideLayout: CT_BASE + "presentationml.slideLayout+xml",
+ slideMaster: CT_BASE + "presentationml.slideMaster+xml",
+ notesSlide: CT_BASE + "presentationml.notesSlide+xml",
+ notesMaster: CT_BASE + "presentationml.notesMaster+xml",
+ chart: CT_BASE + "drawingml.chart+xml",
+ worksheet: CT_BASE + "spreadsheetml.worksheet+xml",
+ sharedStrings: CT_BASE + "spreadsheetml.sharedStrings+xml",
+ coreProperties: "application/vnd.openxmlformats-package.core-properties+xml",
+ "core-properties": "application/vnd.openxmlformats-package.core-properties+xml",
+ extendedProperties: CT_BASE + "extended-properties+xml",
+ "extended-properties": CT_BASE + "extended-properties+xml"
+};
+function expectedContentType(pkg, relType) {
+ const suffix = relType.split("/").pop();
+ const rule = REL_TYPE_CT[suffix];
+ if (rule === void 0) return void 0;
+ if (typeof rule === "string") return rule;
+ const kind = pkgKind(pkg);
+ return kind ? rule[kind] : void 0;
+}
+function contentTypeRules(pkg, out) {
+ for (const [owner, map] of pkg.rels) {
+ for (const rel of map.values()) {
+ if (rel.mode === "External") continue;
+ const expected = expectedContentType(pkg, rel.type);
+ if (expected === void 0) continue;
+ const target = pkg.targetOf(owner, rel.target);
+ if (!pkg.parts.has(target)) continue;
+ const actual = pkg.contentType(target);
+ if (actual !== expected) {
+ out.push(finding("wrong-content-type", target, `${target} is ${actual ?? "untyped"}, expected ${expected} for ${rel.type.split("/").pop()}`));
+ }
+ }
+ }
+}
function uniqueIds(elements, attr, part, rule, out) {
const seen = /* @__PURE__ */ new Set();
for (const el of elements) {
@@ -7155,6 +7231,14 @@ function fixPackage(pkg) {
fixed.push(name);
}
}
+ const paraIds = /* @__PURE__ */ new Set();
+ for (const name of pkg.parts.keys()) {
+ if (!/^word\//.test(name) || !name.endsWith(".xml")) continue;
+ try {
+ for (const p of elementsOf(parseXml(pkg.get(name)).documentElement, "w:p")) paraIds.add(p.getAttribute("w14:paraId"));
+ } catch {
+ }
+ }
for (const name of pkg.parts.keys()) {
if (!/^word\//.test(name) || !name.endsWith(".xml")) continue;
let doc;
@@ -7169,8 +7253,11 @@ function fixPackage(pkg) {
const id = p.getAttribute("w14:paraId");
if (!id) continue;
if (seen.has(id) || parseInt(id, 16) >= 2147483648) {
- const next = Math.floor(Math.random() * 2147483647).toString(16).padStart(8, "0");
- p.setAttribute("w14:paraId", seen.has(next) ? (parseInt(next, 16) ^ 74565).toString(16) : next);
+ let next = 1;
+ while (paraIds.has(next.toString(16).padStart(8, "0")) || paraIds.has(next.toString(16))) next++;
+ const fresh = next.toString(16).padStart(8, "0");
+ p.setAttribute("w14:paraId", fresh);
+ paraIds.add(fresh);
dirty = true;
}
seen.add(p.getAttribute("w14:paraId"));
@@ -7180,6 +7267,18 @@ function fixPackage(pkg) {
fixed.push(name);
}
}
+ for (const [owner, map] of pkg.rels) {
+ for (const rel of map.values()) {
+ if (rel.mode === "External") continue;
+ const expected = expectedContentType(pkg, rel.type);
+ if (expected === void 0) continue;
+ const target = pkg.targetOf(owner, rel.target);
+ if (pkg.parts.has(target) && pkg.contentType(target) !== expected) {
+ pkg.override(target, expected);
+ fixed.push(target);
+ }
+ }
+ }
for (const part of [...pkg.overrides.keys()]) {
if (!pkg.parts.has(part)) {
pkg.overrides.delete(part);
@@ -7467,19 +7566,23 @@ function applyOp(pkg, getDoc, changed, op) {
if (op.set_field) {
const { match, value } = op.set_field;
const re = new RegExp(match);
- let done = false;
+ let any = false;
for (const name of EDIT_PARTS(pkg, false)) {
const doc = getDoc(name);
+ let hit = false;
for (const fld of elementsOf(doc.documentElement, "w:fldSimple")) {
if (re.test(fld.getAttribute("w:instr") ?? "")) {
setFieldResult(doc, fld, String(value));
- done = true;
+ hit = true;
}
}
- if (setComplexFieldResult(doc, re, String(value))) done = true;
- if (done) changed.add(name);
+ if (setComplexFieldResult(doc, re, String(value))) hit = true;
+ if (hit) {
+ changed.add(name);
+ any = true;
+ }
}
- if (!done) throw new Error(`no field instruction matches /${match}/`);
+ if (!any) throw new Error(`no field instruction matches /${match}/`);
return;
}
if (op.insert) {
@@ -7535,19 +7638,21 @@ function applyOp(pkg, getDoc, changed, op) {
function escapeRe(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
+var expand = (m, withText) => withText.replace(/\$(\$|&|[1-9]\d?)/g, (_, g) => g === "$" ? "$" : g === "&" ? m[0] : m[Number(g)] ?? "");
+var nearestPara = (el) => {
+ for (let n = el.parentNode; n; n = n.parentNode) if (n.tagName === "w:p") return n;
+ return null;
+};
function replaceInDoc(doc, re, withText) {
let total = 0;
- const once = () => new RegExp(re.source, re.flags.replace("g", ""));
+ const global = new RegExp(re.source, re.flags.includes("g") ? re.flags : re.flags + "g");
for (const p of elementsOf(doc.documentElement, "w:p")) {
- let guard = 0;
- while (guard++ < 200) {
- const runs = elementsOf(p, "w:r").filter((r) => !isDeleted(p, r));
- const texts = runs.map((r) => elementsOf(r, "w:t").map((t) => t.textContent).join(""));
- const joined = texts.join("");
- const m = once().exec(joined);
- if (!m) break;
+ const runs = elementsOf(p, "w:r").filter((r) => !isDeleted(p, r) && nearestPara(r) === p);
+ const texts = runs.map((r) => elementsOf(r, "w:t").map((t) => t.textContent).join(""));
+ const joined = texts.join("");
+ for (const m of [...joined.matchAll(global)].reverse()) {
let cursor = 0, first = -1, last = -1;
- for (const [i] of runs.entries()) {
+ for (let i = 0; i < runs.length; i++) {
const end = cursor + texts[i].length;
if (first < 0 && m.index < end) first = i;
if (m.index + m[0].length <= end) {
@@ -7556,9 +7661,8 @@ function replaceInDoc(doc, re, withText) {
}
cursor = end;
}
- if (first < 0) break;
+ if (first < 0) continue;
if (last < 0) last = runs.length - 1;
- const replacement = m[0].replace(new RegExp(re.source), withText);
const startRun = runs[first];
const preStart = texts[first].slice(0, m.index - sumTo(texts, first));
const postEnd = texts[last].slice(m.index + m[0].length - sumTo(texts, last));
@@ -7572,7 +7676,7 @@ function replaceInDoc(doc, re, withText) {
return clone;
};
setRunText(startRun, preStart);
- const repl = makeRun(replacement);
+ const repl = makeRun(expand(m, withText));
startRun.parentNode.insertBefore(repl, startRun.nextSibling);
if (first === last) {
if (postEnd !== "") startRun.parentNode.insertBefore(makeRun(postEnd), repl.nextSibling);
@@ -7594,9 +7698,15 @@ function setRunText(run, text) {
for (const extra of ts.slice(1)) extra.textContent = "";
}
function setCellText(doc, tc, text) {
- for (const p2 of childrenOf(tc, "w:p")) tc.removeChild(p2);
+ const paras = childrenOf(tc, "w:p");
+ const pPr = paras[0] && childrenOf(paras[0], "w:pPr")[0];
+ const firstRun = paras[0] && elementsOf(paras[0], "w:r")[0];
+ const rPr = firstRun && childrenOf(firstRun, "w:rPr")[0];
+ for (const p2 of paras) tc.removeChild(p2);
const p = doc.createElement("w:p");
+ if (pPr) p.appendChild(pPr);
const r = doc.createElement("w:r");
+ if (rPr) r.appendChild(rPr);
const t = doc.createElement("w:t");
t.setAttribute("xml:space", "preserve");
t.appendChild(doc.createTextNode(text));
@@ -7605,8 +7715,10 @@ function setCellText(doc, tc, text) {
tc.appendChild(p);
}
function setFieldResult(doc, fld, value) {
+ const rPr = elementsOf(fld, "w:r")[0] && childrenOf(elementsOf(fld, "w:r")[0], "w:rPr")[0];
for (const child2 of [...fld.childNodes]) fld.removeChild(child2);
const r = doc.createElement("w:r");
+ if (rPr) r.appendChild(rPr.cloneNode(true));
const t = doc.createElement("w:t");
t.setAttribute("xml:space", "preserve");
t.appendChild(doc.createTextNode(value));
@@ -7614,32 +7726,81 @@ function setFieldResult(doc, fld, value) {
fld.appendChild(r);
}
function setComplexFieldResult(doc, re, value) {
- let done = false;
- const nodes = elementsOf(doc.documentElement);
- for (let i = 0; i < nodes.length; i++) {
- if (nodes[i].tagName === "w:instrText" && re.test(nodes[i].textContent)) {
- const p = ancestor(nodes[i], "w:p");
- if (!p) continue;
- let phase = "pre", resultRun = null;
- for (const el of elementsOf(p)) {
- if (el.tagName === "w:fldChar") {
- const type = el.getAttribute("w:fldCharType");
- if (type === "separate") phase = "result";
- else if (type === "end") phase = "post";
- } else if (phase === "result" && el.tagName === "w:r" && resultRun === null) resultRun = el;
- }
- if (resultRun) {
- setRunText(resultRun, value);
- done = true;
+ let hit = false;
+ let depth = 0, inField = false, inResult = false, first = true;
+ for (const el of elementsOf(doc.documentElement)) {
+ if (el.tagName === "w:instrText") {
+ if (!inField && re.test(el.textContent)) {
+ inField = true;
+ depth = 1;
+ inResult = false;
+ first = true;
}
+ continue;
+ }
+ if (!inField) continue;
+ if (el.tagName === "w:fldChar") {
+ const type = el.getAttribute("w:fldCharType");
+ if (type === "begin") depth++;
+ else if (type === "end") {
+ depth--;
+ if (depth === 0) inField = false;
+ } else if (type === "separate" && depth === 1) inResult = true;
+ continue;
+ }
+ if (inResult && el.tagName === "w:r") {
+ if (first) {
+ setRunText(el, value);
+ first = false;
+ } else setRunText(el, "");
+ hit = true;
}
}
- return done;
+ return hit;
}
var ancestor = (node, tag) => {
for (let n = node.parentNode; n; n = n.parentNode) if (n.tagName === tag) return n;
return null;
};
+function splitRunAt(doc, run, offset) {
+ const text = elementsOf(run, "w:t").map((t2) => t2.textContent).join("");
+ const clone = run.cloneNode(false);
+ while (clone.firstChild && clone.firstChild.tagName !== "w:rPr") clone.removeChild(clone.firstChild);
+ const t = doc.createElement("w:t");
+ t.setAttribute("xml:space", "preserve");
+ t.appendChild(doc.createTextNode(text.slice(offset)));
+ clone.appendChild(t);
+ setRunText(run, text.slice(0, offset));
+ run.parentNode.insertBefore(clone, run.nextSibling);
+ return clone;
+}
+function phraseRuns(doc, p, anchor) {
+ const runs = elementsOf(p, "w:r").filter((r) => !isDeleted(p, r) && nearestPara(r) === p);
+ const texts = runs.map((r) => elementsOf(r, "w:t").map((t) => t.textContent).join(""));
+ const joined = texts.join("");
+ const start = joined.indexOf(anchor);
+ if (start < 0) return void 0;
+ const end = start + anchor.length;
+ const locate = (pos) => {
+ let c = 0;
+ for (let i = 0; i < runs.length; i++) {
+ const e2 = c + texts[i].length;
+ if (pos < e2 || pos === e2 && i === runs.length - 1) return { i, off: pos - c };
+ c = e2;
+ }
+ return { i: runs.length - 1, off: texts.at(-1).length };
+ };
+ const e = locate(end), s = locate(start);
+ let lastRun;
+ if (e.off === texts[e.i].length) lastRun = runs[e.i];
+ else if (e.off === 0) lastRun = runs[e.i - 1];
+ else {
+ splitRunAt(doc, runs[e.i], e.off);
+ lastRun = runs[e.i];
+ }
+ const firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off);
+ return { firstRun, lastRun };
+}
function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = "Bot", initials = "B" }) {
const doc = getDoc("word/document.xml");
const p = blockIndex(doc).get(paragraph);
@@ -7677,11 +7838,15 @@ function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = "B
const refEl = doc.createElement("w:commentReference");
refEl.setAttribute("w:id", id);
ref.appendChild(refEl);
+ const pPr = childrenOf(p, "w:pPr")[0];
if (anchor) {
- p.insertBefore(rangeStart, p.firstChild);
- p.appendChild(rangeEnd);
- p.appendChild(ref);
+ const span = phraseRuns(doc, p, anchor);
+ if (!span) throw new Error(`anchor "${anchor}" not found in ${paragraph}`);
+ p.insertBefore(rangeStart, span.firstRun);
+ span.lastRun.parentNode.insertBefore(rangeEnd, span.lastRun.nextSibling);
+ span.lastRun.parentNode.insertBefore(ref, rangeEnd.nextSibling);
} else {
+ p.insertBefore(rangeStart, pPr ? pPr.nextSibling : p.firstChild);
p.appendChild(rangeEnd);
p.appendChild(ref);
}
@@ -7693,6 +7858,47 @@ function resolveChanges(doc, accept) {
while (el.firstChild) el.parentNode.insertBefore(el.firstChild, el);
el.parentNode.removeChild(el);
};
+ const mergeIntoNext = (p, pPr) => {
+ const next = p.nextSibling;
+ if (!next || next.tagName !== "w:p") return false;
+ const nextPPr = childrenOf(next, "w:pPr")[0];
+ const anchor = nextPPr ? nextPPr.nextSibling : next.firstChild;
+ for (const node of [...p.childNodes]) {
+ if (node === pPr) continue;
+ next.insertBefore(node, anchor);
+ }
+ p.parentNode.removeChild(p);
+ return true;
+ };
+ for (const p of elementsOf(doc.documentElement, "w:p")) {
+ const pPr = childrenOf(p, "w:pPr")[0];
+ const rPr = pPr && childrenOf(pPr, "w:rPr")[0];
+ if (!rPr) continue;
+ const del = childrenOf(rPr, "w:del")[0];
+ const ins = childrenOf(rPr, "w:ins")[0];
+ if (del) {
+ dirty = true;
+ if (!accept || !mergeIntoNext(p, pPr)) rPr.removeChild(del);
+ } else if (ins) {
+ dirty = true;
+ if (accept || !mergeIntoNext(p, pPr)) rPr.removeChild(ins);
+ }
+ }
+ for (const tr of elementsOf(doc.documentElement, "w:tr")) {
+ const trPr = childrenOf(tr, "w:trPr")[0];
+ if (!trPr) continue;
+ const ins = childrenOf(trPr, "w:ins")[0];
+ const del = childrenOf(trPr, "w:del")[0];
+ if (ins) {
+ dirty = true;
+ if (accept) trPr.removeChild(ins);
+ else tr.parentNode.removeChild(tr);
+ } else if (del) {
+ dirty = true;
+ if (accept) tr.parentNode.removeChild(tr);
+ else trPr.removeChild(del);
+ }
+ }
for (const el of elementsOf(doc.documentElement)) {
const tag = el.tagName;
if (tag === "w:moveFromRangeStart" || tag === "w:moveFromRangeEnd" || tag === "w:moveToRangeStart" || tag === "w:moveToRangeEnd" || tag === "w:customXmlInsRangeStart" || tag === "w:customXmlInsRangeEnd" || tag === "w:customXmlDelRangeStart" || tag === "w:customXmlDelRangeEnd" || tag === "w:customXmlMoveFromRangeStart" || tag === "w:customXmlMoveFromRangeEnd" || tag === "w:customXmlMoveToRangeStart" || tag === "w:customXmlMoveToRangeEnd") {
@@ -7717,18 +7923,33 @@ function resolveChanges(doc, accept) {
lift(el);
dirty = true;
}
- } else if (/^w:(pPrChange|rPrChange|tblPrChange|tcPrChange|trPrChange|sectPrChange|numberingChange)$/.test(tag)) {
+ } else if (tag === "w:numberingChange") {
+ el.parentNode.removeChild(el);
+ dirty = true;
+ } else if (/^w:(pPrChange|rPrChange|tblPrChange|tcPrChange|trPrChange|sectPrChange)$/.test(tag)) {
+ const parent = el.parentNode;
if (accept) {
- el.parentNode.removeChild(el);
+ parent.removeChild(el);
dirty = true;
} else {
- const parent = el.parentNode;
const stored = childrenOf(el)[0];
- if (stored) parent.replaceChild(stored.cloneNode(true), el);
- else parent.removeChild(el);
+ const keep = /* @__PURE__ */ new Set();
+ if (parent.tagName === "w:pPr") {
+ keep.add("w:rPr");
+ keep.add("w:sectPr");
+ }
+ if (parent.tagName === "w:sectPr") {
+ keep.add("w:headerReference");
+ keep.add("w:footerReference");
+ }
+ for (const c of [...parent.childNodes]) if (!keep.has(c.tagName) && c !== el) parent.removeChild(c);
+ if (stored) {
+ const anchor = [...parent.childNodes].find((c) => keep.has(c.tagName)) ?? null;
+ for (const s of [...stored.childNodes]) parent.insertBefore(s, anchor);
+ }
+ parent.removeChild(el);
dirty = true;
}
- } else if (tag === "w:del" && el.parentNode?.tagName === "w:rPr" && el.parentNode.parentNode?.tagName === "w:pPr") {
} else if (tag === "w:cellIns" || tag === "w:cellDel") {
const tc = ancestor(el, "w:tc");
if (tc) {
@@ -7739,24 +7960,6 @@ function resolveChanges(doc, accept) {
}
}
}
- for (const p of elementsOf(doc.documentElement, "w:p")) {
- const pPr = firstOf(p, "w:pPr");
- const rPr = pPr && firstOf(pPr, "w:rPr");
- const del = rPr && firstOf(rPr, "w:del");
- if (!del) continue;
- dirty = true;
- if (accept) {
- const next = p.nextSibling;
- if (next && next.tagName === "w:p") {
- for (const child2 of [...p.childNodes]) if (child2 !== pPr) next.insertBefore(child2, next.firstChild);
- p.parentNode.removeChild(p);
- } else {
- rPr.removeChild(del);
- }
- } else {
- rPr.removeChild(del);
- }
- }
return dirty;
}
@@ -7964,7 +8167,8 @@ function pptxLayout(pkg, slidesFilter, { autofit = false } = {}) {
const old = firstOf(bodyPr, tag);
if (old) bodyPr.removeChild(old);
}
- bodyPr.appendChild(doc.createElement("a:normAutofit"));
+ const anchor = childrenOf(bodyPr).find((c) => ["a:scene3d", "a:sp3d", "a:flatTx", "a:extLst"].includes(c.tagName));
+ bodyPr.insertBefore(doc.createElement("a:normAutofit"), anchor ?? null);
fitted.push(`${slide.position}:${shapeName(el)}`);
}
}
@@ -7994,6 +8198,8 @@ function pptxSlides(pkg, order) {
const dup = String(nextIds.slide++);
const newPart = `ppt/slides/slide${dup}.xml`;
pkg.parts.set(newPart, { data: pkg.get(source.part) });
+ if (pkg.overrides.has(source.part)) pkg.overrides.set(newPart, pkg.overrides.get(source.part));
+ const mapped = /* @__PURE__ */ new Map([[source.part, newPart]]);
const rid = `rId${nextIds.rid++}`;
presRels.set(rid, { id: rid, type: presRels.get(source.rid)?.type ?? "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", target: `slides/slide${dup}.xml`, mode: "Internal" });
const sid = String(nextIds.sldId++);
@@ -8005,11 +8211,20 @@ function pptxSlides(pkg, order) {
}
const target = pkg.targetOf(source.part, rel.target);
if (/notesSlide|chart|package|oleObject|embeddings/i.test(rel.type) && pkg.parts.has(target)) {
- const newTarget = copyPart(pkg, target);
+ const newTarget = copyPart(pkg, target, mapped);
slideRels.set(id, { ...rel, target: posix3.relative(posix3.dirname(newPart), newTarget) });
} else slideRels.set(id, { ...rel });
}
pkg.rels.set(newPart, slideRels);
+ for (const copy of mapped.values()) {
+ const map = pkg.rels.get(copy);
+ if (!map) continue;
+ for (const rel of map.values()) {
+ if (rel.mode === "External") continue;
+ const abs = pkg.targetOf(copy, rel.target);
+ if (mapped.has(abs)) rel.target = posix3.relative(posix3.dirname(copy), mapped.get(abs));
+ }
+ }
newEntries.push({ sldId: sid, rid, part: newPart });
duplicated.push(pos);
}
@@ -8036,12 +8251,13 @@ var nextRid = (map) => {
while (map.has(`rId${i}`)) i++;
return i;
};
-function copyPart(pkg, part) {
+function copyPart(pkg, part, mapped = /* @__PURE__ */ new Map()) {
const dir = posix3.dirname(part), base = posix3.basename(part);
const stem = base.replace(/\d+(\.\w+)$/, ""), extn = base.match(/\.\w+$/)[0];
const n = maxNum([...pkg.parts.keys()].filter((k) => k.startsWith(dir + "/")), new RegExp(`${stem}(\\d+)${extn.replace(".", "\\.")}$`)) + 1;
const target = `${dir}/${stem}${n}${extn}`;
pkg.parts.set(target, { data: pkg.get(part) });
+ mapped.set(part, target);
if (pkg.overrides.has(part)) pkg.overrides.set(target, pkg.overrides.get(part));
const map = /* @__PURE__ */ new Map();
for (const [id, rel] of pkg.relsOf(part)) {
@@ -8051,14 +8267,23 @@ function copyPart(pkg, part) {
}
const t = pkg.targetOf(part, rel.target);
if (/package|oleObject|workbook|embeddings|xlsx/i.test(rel.type) && pkg.parts.has(t)) {
- map.set(id, { ...rel, target: posix3.relative(dir, copyPart(pkg, t)) });
+ map.set(id, { ...rel, target: posix3.relative(dir, copyPart(pkg, t, mapped)) });
} else map.set(id, { ...rel });
}
if (map.size > 0) pkg.rels.set(target, map);
return target;
}
+var CONTENT_REL = /\/(image|media|video|audio|chart|oleObject|package|diagramData|diagramLayout|diagramColors|diagramQuickStyle|drawing|hyperlink)$/;
function pptxClean(pkg, { dryRun = false } = {}) {
const removed = [];
+ for (const [owner, map] of [...pkg.rels]) {
+ if (!pkg.parts.has(owner)) continue;
+ const xml = pkg.get(owner) ?? Buffer.alloc(0);
+ for (const [id, rel] of [...map]) {
+ if (rel.mode !== "Internal" || !CONTENT_REL.test(rel.type)) continue;
+ if (!xml.includes(`"${id}"`)) map.delete(id);
+ }
+ }
const listed = new Set(deckOrder(pkg).map((e) => e.part));
for (const part of slideParts(pkg)) {
if (!listed.has(part)) {
@@ -8475,10 +8700,8 @@ async function main(argv) {
need(command !== void 0 && file !== void 0 || cmd === "sheet" && file !== void 0, USAGE);
if (command === "unpack") {
need(positional[1], "unpack <file> <dir>");
- const dir = positional[1];
- const prior = existsSync2(dir) && statSync2(dir).isDirectory() ? dir : void 0;
- Package.open(file).unpack(dir, prior);
- return { dir };
+ Package.open(file).unpack(positional[1]);
+ return { dir: positional[1] };
}
if (command === "pack") {
const dir = file, target = positional[1];
@@ -8503,10 +8726,8 @@ async function main(argv) {
if (values.fix) {
const { fixed, remaining } = fixPackage(pkg2);
const out = outArgs(values, file);
- if (out !== file || statSync2(file).isDirectory()) {
- if (statSync2(file).isDirectory()) pkg2.unpack(file, file);
- else writeFileSync2(out, pkg2.save());
- } else writeFileSync2(out, pkg2.save());
+ if (statSync2(file).isDirectory()) pkg2.unpack(out);
+ else writeFileSync2(out, pkg2.save());
return remaining.length > 0 ? findingsResult(remaining, { fixed }) : { fixed, findings: [] };
}
const against = values.against ? openAny(values.against) : void 0;
@@ -8550,13 +8771,13 @@ async function main(argv) {
if (sub === "clean") {
const dry = values["dry-run"];
const result = pptxClean(pkg, { dryRun: dry });
- if (!dry && result.removed.length > 0) {
+ if (!dry) {
const out = outArgs(values, file);
- if (statSync2(file).isDirectory()) pkg.unpack(out, out === file ? file : void 0);
+ if (statSync2(file).isDirectory()) pkg.unpack(out);
else writeFileSync2(out, pkg.save());
result.file = out;
- } else if (!dry) result.file = file;
- return result.removed.length > 0 && !dry ? result : result;
+ }
+ return result;
}
throw new Usage("pptx inventory|layout|slides|clean");
}
@@ -8584,10 +8805,9 @@ function findingsResult(findings, extra = {}) {
}
main(process.argv.slice(2)).then((result) => {
if (JSON_MODE) {
- const ok = !result?.__findings;
- const data = result?.__findings ? { findings: result.findings, ...omit(result, ["__findings", "findings"]) } : result;
- console.log(JSON.stringify({ ok, data }));
- process.exit(ok ? 0 : 1);
+ const data = result?.__findings ? omit(result, ["__findings"]) : result;
+ console.log(JSON.stringify({ ok: true, data }));
+ process.exit(result?.__findings ? 1 : 0);
}
if (result?.__findings) {
for (const f of result.findings) console.log(`${f.part}: ${f.message}`);
diff --git a/ds-bot/package-lock.json b/ds-bot/package-lock.json
index 77a3bb41..494d617b 100644
--- a/ds-bot/package-lock.json
+++ b/ds-bot/package-lock.json
@@ -9,7 +9,7 @@
"version": "0.2.2",
"license": "Apache-2.0",
"devDependencies": {
- "@xmldom/xmldom": "^0.9.12",
+ "@xmldom/xmldom": "0.9.12",
"esbuild": "0.28.2"
},
"peerDependencies": {
diff --git a/ds-bot/package.json b/ds-bot/package.json
index 79ec32a6..2562771d 100644
--- a/ds-bot/package.json
+++ b/ds-bot/package.json
@@ -63,7 +63,7 @@
}
},
"devDependencies": {
- "@xmldom/xmldom": "^0.9.12",
+ "@xmldom/xmldom": "0.9.12",
"esbuild": "0.28.2"
}
}
diff --git a/ds-bot/src/office/check.js b/ds-bot/src/office/check.js
index d59611b8..3be4d66f 100644
--- a/ds-bot/src/office/check.js
+++ b/ds-bot/src/office/check.js
@@ -89,12 +89,15 @@ function runRules(pkg) {
if (name.endsWith('.rels')) continue
const rels = pkg.relsOf(name)
for (const el of elementsOf(doc.documentElement)) {
- for (const attr of ['r:id', 'r:embed', 'r:link', 'r:dm', 'r:lo', 'r:qs', 'r:cs', 'o:id']) {
+ for (const attr of ['r:id', 'r:embed', 'r:link', 'r:dm', 'r:lo', 'r:qs', 'r:cs']) {
const value = el.getAttribute?.(attr)
if (value && !rels.has(value)) out.push(finding('unknown-rel-ref', name, `${el.tagName} ${attr}="${value}" has no relationship`))
}
}
}
+ // Parts reached by a known relationship type must carry its content type —
+ // an application/xml Default is not a match.
+ contentTypeRules(pkg, out)
// Part-kind rules.
const isPptx = pkg.parts.has('ppt/presentation.xml'), isDocx = pkg.parts.has('word/document.xml')
if (isPptx) pptxRules(pkg, xmlDocs, out)
@@ -102,6 +105,63 @@ function runRules(pkg) {
return out
}
+const CT_BASE = 'application/vnd.openxmlformats-officedocument.'
+const pkgKind = (pkg) => pkg.parts.has('word/document.xml') ? 'docx' : pkg.parts.has('ppt/presentation.xml') ? 'pptx' : pkg.parts.has('xl/workbook.xml') ? 'xlsx' : undefined
+const MAIN_CT = { docx: CT_BASE + 'wordprocessingml.document.main+xml', pptx: CT_BASE + 'presentationml.presentation.main+xml', xlsx: CT_BASE + 'spreadsheetml.sheet.main+xml' }
+
+// Relationship-type suffix → expected content type (or a per-package map).
+const REL_TYPE_CT = {
+ officeDocument: MAIN_CT,
+ document: MAIN_CT,
+ styles: { docx: CT_BASE + 'wordprocessingml.styles+xml', xlsx: CT_BASE + 'spreadsheetml.styles+xml' },
+ numbering: CT_BASE + 'wordprocessingml.numbering+xml',
+ settings: CT_BASE + 'wordprocessingml.settings+xml',
+ comments: CT_BASE + 'wordprocessingml.comments+xml',
+ footnotes: CT_BASE + 'wordprocessingml.footnotes+xml',
+ endnotes: CT_BASE + 'wordprocessingml.endnotes+xml',
+ header: CT_BASE + 'wordprocessingml.header+xml',
+ footer: CT_BASE + 'wordprocessingml.footer+xml',
+ fontTable: CT_BASE + 'wordprocessingml.fontTable+xml',
+ theme: CT_BASE + 'theme+xml',
+ slide: CT_BASE + 'presentationml.slide+xml',
+ slideLayout: CT_BASE + 'presentationml.slideLayout+xml',
+ slideMaster: CT_BASE + 'presentationml.slideMaster+xml',
+ notesSlide: CT_BASE + 'presentationml.notesSlide+xml',
+ notesMaster: CT_BASE + 'presentationml.notesMaster+xml',
+ chart: CT_BASE + 'drawingml.chart+xml',
+ worksheet: CT_BASE + 'spreadsheetml.worksheet+xml',
+ sharedStrings: CT_BASE + 'spreadsheetml.sharedStrings+xml',
+ coreProperties: 'application/vnd.openxmlformats-package.core-properties+xml',
+ 'core-properties': 'application/vnd.openxmlformats-package.core-properties+xml',
+ extendedProperties: CT_BASE + 'extended-properties+xml',
+ 'extended-properties': CT_BASE + 'extended-properties+xml',
+}
+
+export function expectedContentType(pkg, relType) {
+ const suffix = relType.split('/').pop()
+ const rule = REL_TYPE_CT[suffix]
+ if (rule === undefined) return undefined
+ if (typeof rule === 'string') return rule
+ const kind = pkgKind(pkg)
+ return kind ? rule[kind] : undefined
+}
+
+function contentTypeRules(pkg, out) {
+ for (const [owner, map] of pkg.rels) {
+ for (const rel of map.values()) {
+ if (rel.mode === 'External') continue
+ const expected = expectedContentType(pkg, rel.type)
+ if (expected === undefined) continue
+ const target = pkg.targetOf(owner, rel.target)
+ if (!pkg.parts.has(target)) continue
+ const actual = pkg.contentType(target)
+ if (actual !== expected) {
+ out.push(finding('wrong-content-type', target, `${target} is ${actual ?? 'untyped'}, expected ${expected} for ${rel.type.split('/').pop()}`))
+ }
+ }
+ }
+}
+
function uniqueIds(elements, attr, part, rule, out) {
const seen = new Set()
for (const el of elements) {
@@ -201,7 +261,13 @@ export function fixPackage(pkg) {
}
if (dirty) { pkg.putXml(name, doc); fixed.push(name) }
}
- // paraId duplicates.
+ // paraId duplicates: renumber in document order to the next unused value
+ // below 0x80000000, deterministically.
+ const paraIds = new Set()
+ for (const name of pkg.parts.keys()) {
+ if (!/^word\//.test(name) || !name.endsWith('.xml')) continue
+ try { for (const p of elementsOf(parseXml(pkg.get(name)).documentElement, 'w:p')) paraIds.add(p.getAttribute('w14:paraId')) } catch { /* malformed stays */ }
+ }
for (const name of pkg.parts.keys()) {
if (!/^word\//.test(name) || !name.endsWith('.xml')) continue
let doc
@@ -211,13 +277,29 @@ export function fixPackage(pkg) {
const id = p.getAttribute('w14:paraId')
if (!id) continue
if (seen.has(id) || parseInt(id, 16) >= 0x80000000) {
- const next = (Math.floor(Math.random() * 0x7fffffff)).toString(16).padStart(8, '0')
- p.setAttribute('w14:paraId', seen.has(next) ? (parseInt(next, 16) ^ 0x12345).toString(16) : next); dirty = true
+ let next = 1
+ while (paraIds.has(next.toString(16).padStart(8, '0')) || paraIds.has(next.toString(16))) next++
+ const fresh = next.toString(16).padStart(8, '0')
+ p.setAttribute('w14:paraId', fresh)
+ paraIds.add(fresh)
+ dirty = true
}
seen.add(p.getAttribute('w14:paraId'))
}
if (dirty) { pkg.putXml(name, doc); fixed.push(name) }
}
+ // Wrong content types on parts a known relationship type points at.
+ for (const [owner, map] of pkg.rels) {
+ for (const rel of map.values()) {
+ if (rel.mode === 'External') continue
+ const expected = expectedContentType(pkg, rel.type)
+ if (expected === undefined) continue
+ const target = pkg.targetOf(owner, rel.target)
+ if (pkg.parts.has(target) && pkg.contentType(target) !== expected) {
+ pkg.override(target, expected); fixed.push(target)
+ }
+ }
+ }
// Content types: drop Overrides for absent parts, add Overrides for known types.
for (const part of [...pkg.overrides.keys()]) {
if (!pkg.parts.has(part)) { pkg.overrides.delete(part); fixed.push(`-${part}`) }
diff --git a/ds-bot/src/office/cli.js b/ds-bot/src/office/cli.js
index aef6a4f5..938081a1 100644
--- a/ds-bot/src/office/cli.js
+++ b/ds-bot/src/office/cli.js
@@ -66,10 +66,8 @@ async function main(argv) {
if (command === 'unpack') {
need(positional[1], 'unpack <file> <dir>')
- const dir = positional[1]
- const prior = existsSync(dir) && statSync(dir).isDirectory() ? dir : undefined
- Package.open(file).unpack(dir, prior)
- return { dir }
+ Package.open(file).unpack(positional[1])
+ return { dir: positional[1] }
}
if (command === 'pack') {
const dir = file, target = positional[1]
@@ -89,10 +87,8 @@ async function main(argv) {
if (values.fix) {
const { fixed, remaining } = fixPackage(pkg)
const out = outArgs(values, file)
- if (out !== file || statSync(file).isDirectory()) {
- if (statSync(file).isDirectory()) pkg.unpack(file, file)
- else writeFileSync(out, pkg.save())
- } else writeFileSync(out, pkg.save())
+ if (statSync(file).isDirectory()) pkg.unpack(out)
+ else writeFileSync(out, pkg.save())
return remaining.length > 0 ? findingsResult(remaining, { fixed }) : { fixed, findings: [] }
}
const against = values.against ? openAny(values.against) : undefined
@@ -138,13 +134,13 @@ async function main(argv) {
if (sub === 'clean') {
const dry = values['dry-run']
const result = pptxClean(pkg, { dryRun: dry })
- if (!dry && result.removed.length > 0) {
+ if (!dry) {
const out = outArgs(values, file)
- if (statSync(file).isDirectory()) pkg.unpack(out, out === file ? file : undefined)
+ if (statSync(file).isDirectory()) pkg.unpack(out)
else writeFileSync(out, pkg.save())
result.file = out
- } else if (!dry) result.file = file
- return result.removed.length > 0 && !dry ? result : result
+ }
+ return result
}
throw new Usage('pptx inventory|layout|slides|clean')
}
@@ -176,10 +172,11 @@ function findingsResult(findings, extra = {}) {
main(process.argv.slice(2)).then(result => {
if (JSON_MODE) {
- const ok = !result?.__findings
- const data = result?.__findings ? { findings: result.findings, ...omit(result, ['__findings', 'findings']) } : result
- console.log(JSON.stringify({ ok, data }))
- process.exit(ok ? 0 : 1)
+ // Findings are a successful run with results: ok stays true, the exit code
+ // (1) carries "findings remain". ok:false is for errors only.
+ const data = result?.__findings ? omit(result, ['__findings']) : result
+ console.log(JSON.stringify({ ok: true, data }))
+ process.exit(result?.__findings ? 1 : 0)
}
if (result?.__findings) {
for (const f of result.findings) console.log(`${f.part}: ${f.message}`)
diff --git a/ds-bot/src/office/docx.js b/ds-bot/src/office/docx.js
index f57a01f5..f999257e 100644
--- a/ds-bot/src/office/docx.js
+++ b/ds-bot/src/office/docx.js
@@ -249,16 +249,17 @@ function applyOp(pkg, getDoc, changed, op) {
if (op.set_field) {
const { match, value } = op.set_field
const re = new RegExp(match)
- let done = false
+ let any = false
for (const name of EDIT_PARTS(pkg, false)) {
const doc = getDoc(name)
+ let hit = false
for (const fld of elementsOf(doc.documentElement, 'w:fldSimple')) {
- if (re.test(fld.getAttribute('w:instr') ?? '')) { setFieldResult(doc, fld, String(value)); done = true }
+ if (re.test(fld.getAttribute('w:instr') ?? '')) { setFieldResult(doc, fld, String(value)); hit = true }
}
- if (setComplexFieldResult(doc, re, String(value))) done = true
- if (done) changed.add(name)
+ if (setComplexFieldResult(doc, re, String(value))) hit = true
+ if (hit) { changed.add(name); any = true }
}
- if (!done) throw new Error(`no field instruction matches /${match}/`)
+ if (!any) throw new Error(`no field instruction matches /${match}/`)
return
}
if (op.insert) {
@@ -311,29 +312,34 @@ function applyOp(pkg, getDoc, changed, op) {
function escapeRe(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') }
+// Replacement text with $1..$9 group, $& match and $$ escapes resolved — the
+// pattern is not re-run, so anchors and lookarounds cannot be lost.
+const expand = (m, withText) =>
+ withText.replace(/\$(\$|&|[1-9]\d?)/g, (_, g) => g === '$' ? '$' : g === '&' ? m[0] : (m[Number(g)] ?? ''))
+
+const nearestPara = (el) => { for (let n = el.parentNode; n; n = n.parentNode) if (n.tagName === 'w:p') return n; return null }
+
// Replace text across runs inside one document; deletions skipped; formatting of
-// the match-start run kept. Returns the number of replacements.
+// the match-start run kept. Matches are found once on the original text and
+// applied right-to-left, so offsets never shift and the replacement text itself
+// is never re-scanned. Returns the number of replacements.
function replaceInDoc(doc, re, withText) {
let total = 0
- const once = () => new RegExp(re.source, re.flags.replace('g', ''))
+ const global = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')
for (const p of elementsOf(doc.documentElement, 'w:p')) {
- let guard = 0
- while (guard++ < 200) {
- const runs = elementsOf(p, 'w:r').filter(r => !isDeleted(p, r))
- const texts = runs.map(r => elementsOf(r, 'w:t').map(t => t.textContent).join(''))
- const joined = texts.join('')
- const m = once().exec(joined)
- if (!m) break
+ const runs = elementsOf(p, 'w:r').filter(r => !isDeleted(p, r) && nearestPara(r) === p)
+ const texts = runs.map(r => elementsOf(r, 'w:t').map(t => t.textContent).join(''))
+ const joined = texts.join('')
+ for (const m of [...joined.matchAll(global)].reverse()) {
let cursor = 0, first = -1, last = -1
- for (const [i] of runs.entries()) {
+ for (let i = 0; i < runs.length; i++) {
const end = cursor + texts[i].length
if (first < 0 && m.index < end) first = i
if (m.index + m[0].length <= end) { last = i; break }
cursor = end
}
- if (first < 0) break
+ if (first < 0) continue
if (last < 0) last = runs.length - 1
- const replacement = m[0].replace(new RegExp(re.source), withText)
const startRun = runs[first]
const preStart = texts[first].slice(0, m.index - sumTo(texts, first))
const postEnd = texts[last].slice(m.index + m[0].length - sumTo(texts, last))
@@ -345,7 +351,7 @@ function replaceInDoc(doc, re, withText) {
return clone
}
setRunText(startRun, preStart)
- const repl = makeRun(replacement)
+ const repl = makeRun(expand(m, withText))
startRun.parentNode.insertBefore(repl, startRun.nextSibling)
if (first === last) {
if (postEnd !== '') startRun.parentNode.insertBefore(makeRun(postEnd), repl.nextSibling)
@@ -370,48 +376,103 @@ function setRunText(run, text) {
}
function setCellText(doc, tc, text) {
- for (const p of childrenOf(tc, 'w:p')) tc.removeChild(p)
+ // w:tcPr stays; the first paragraph's pPr and the first run's rPr carry over.
+ const paras = childrenOf(tc, 'w:p')
+ const pPr = paras[0] && childrenOf(paras[0], 'w:pPr')[0]
+ const firstRun = paras[0] && elementsOf(paras[0], 'w:r')[0]
+ const rPr = firstRun && childrenOf(firstRun, 'w:rPr')[0]
+ for (const p of paras) tc.removeChild(p)
const p = doc.createElement('w:p')
+ if (pPr) p.appendChild(pPr)
const r = doc.createElement('w:r')
+ if (rPr) r.appendChild(rPr)
const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')
t.appendChild(doc.createTextNode(text))
r.appendChild(t); p.appendChild(r); tc.appendChild(p)
}
function setFieldResult(doc, fld, value) {
- // Drop existing runs; one fresh run with the value.
+ // One run with the value, carrying the first run's rPr.
+ const rPr = elementsOf(fld, 'w:r')[0] && childrenOf(elementsOf(fld, 'w:r')[0], 'w:rPr')[0]
for (const child of [...fld.childNodes]) fld.removeChild(child)
const r = doc.createElement('w:r')
+ if (rPr) r.appendChild(rPr.cloneNode(true))
const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')
t.appendChild(doc.createTextNode(value))
r.appendChild(t); fld.appendChild(r)
}
+// Complex fields can nest and span paragraphs: track begin/end depth, and in
+// the result span (after the depth-1 separate, before the matching end) give
+// the first run the value and clear the rest.
function setComplexFieldResult(doc, re, value) {
- let done = false
- const nodes = elementsOf(doc.documentElement)
- for (let i = 0; i < nodes.length; i++) {
- if (nodes[i].tagName === 'w:instrText' && re.test(nodes[i].textContent)) {
- // Find the separate→end span in the same paragraph, keep the run right
- // after 'separate' and set its text.
- const p = ancestor(nodes[i], 'w:p')
- if (!p) continue
- let phase = 'pre', resultRun = null
- for (const el of elementsOf(p)) {
- if (el.tagName === 'w:fldChar') {
- const type = el.getAttribute('w:fldCharType')
- if (type === 'separate') phase = 'result'
- else if (type === 'end') phase = 'post'
- } else if (phase === 'result' && el.tagName === 'w:r' && resultRun === null) resultRun = el
- }
- if (resultRun) { setRunText(resultRun, value); done = true }
+ let hit = false
+ let depth = 0, inField = false, inResult = false, first = true
+ for (const el of elementsOf(doc.documentElement)) {
+ if (el.tagName === 'w:instrText') {
+ if (!inField && re.test(el.textContent)) { inField = true; depth = 1; inResult = false; first = true }
+ continue
+ }
+ if (!inField) continue
+ if (el.tagName === 'w:fldChar') {
+ const type = el.getAttribute('w:fldCharType')
+ if (type === 'begin') depth++
+ else if (type === 'end') { depth--; if (depth === 0) inField = false }
+ else if (type === 'separate' && depth === 1) inResult = true
+ continue
+ }
+ if (inResult && el.tagName === 'w:r') {
+ if (first) { setRunText(el, value); first = false }
+ else setRunText(el, '')
+ hit = true
}
}
- return done
+ return hit
}
const ancestor = (node, tag) => { for (let n = node.parentNode; n; n = n.parentNode) if (n.tagName === tag) return n; return null }
+// Split `run` at `offset`: the original keeps the first half, a clone with its
+// rPr takes the rest right after it. Returns the clone.
+function splitRunAt(doc, run, offset) {
+ const text = elementsOf(run, 'w:t').map(t => t.textContent).join('')
+ const clone = run.cloneNode(false)
+ while (clone.firstChild && clone.firstChild.tagName !== 'w:rPr') clone.removeChild(clone.firstChild)
+ const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')
+ t.appendChild(doc.createTextNode(text.slice(offset)))
+ clone.appendChild(t)
+ setRunText(run, text.slice(0, offset))
+ run.parentNode.insertBefore(clone, run.nextSibling)
+ return clone
+}
+
+// The run sequence covering `anchor` inside `p`, split at its exact edges.
+// Returns { firstRun, lastRun } or undefined when the phrase is not there.
+function phraseRuns(doc, p, anchor) {
+ const runs = elementsOf(p, 'w:r').filter(r => !isDeleted(p, r) && nearestPara(r) === p)
+ const texts = runs.map(r => elementsOf(r, 'w:t').map(t => t.textContent).join(''))
+ const joined = texts.join('')
+ const start = joined.indexOf(anchor)
+ if (start < 0) return undefined
+ const end = start + anchor.length
+ const locate = (pos) => {
+ let c = 0
+ for (let i = 0; i < runs.length; i++) {
+ const e = c + texts[i].length
+ if (pos < e || (pos === e && i === runs.length - 1)) return { i, off: pos - c }
+ c = e
+ }
+ return { i: runs.length - 1, off: texts.at(-1).length }
+ }
+ const e = locate(end), s = locate(start)
+ let lastRun
+ if (e.off === texts[e.i].length) lastRun = runs[e.i]
+ else if (e.off === 0) lastRun = runs[e.i - 1]
+ else { splitRunAt(doc, runs[e.i], e.off); lastRun = runs[e.i] }
+ const firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off)
+ return { firstRun, lastRun }
+}
+
function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = 'Bot', initials = 'B' }) {
const doc = getDoc('word/document.xml')
const p = blockIndex(doc).get(paragraph)
@@ -440,11 +501,18 @@ function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = 'B
const ref = doc.createElement('w:r')
const refEl = doc.createElement('w:commentReference'); refEl.setAttribute('w:id', id)
ref.appendChild(refEl)
+ // Range markers sit between runs, never before the paragraph's w:pPr.
+ const pPr = childrenOf(p, 'w:pPr')[0]
if (anchor) {
- p.insertBefore(rangeStart, p.firstChild)
- p.appendChild(rangeEnd); p.appendChild(ref)
+ const span = phraseRuns(doc, p, anchor)
+ if (!span) throw new Error(`anchor "${anchor}" not found in ${paragraph}`)
+ p.insertBefore(rangeStart, span.firstRun)
+ span.lastRun.parentNode.insertBefore(rangeEnd, span.lastRun.nextSibling)
+ span.lastRun.parentNode.insertBefore(ref, rangeEnd.nextSibling)
} else {
- p.appendChild(rangeEnd); p.appendChild(ref)
+ p.insertBefore(rangeStart, pPr ? pPr.nextSibling : p.firstChild)
+ p.appendChild(rangeEnd)
+ p.appendChild(ref)
}
changed.add('word/document.xml')
}
@@ -453,7 +521,40 @@ function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = 'B
export function resolveChanges(doc, accept) {
let dirty = false
const lift = (el) => { while (el.firstChild) el.parentNode.insertBefore(el.firstChild, el); el.parentNode.removeChild(el) }
- // Range markers first so wrappers see clean ranges.
+ // Paragraph and row marks first: they restructure the tree, so the generic
+ // ins/del pass must not see them. Merging moves this paragraph's content
+ // children (all but w:pPr), in order, into the next paragraph after its w:pPr
+ // — the merged paragraph keeps the next paragraph's mark and properties.
+ const mergeIntoNext = (p, pPr) => {
+ const next = p.nextSibling
+ if (!next || next.tagName !== 'w:p') return false
+ const nextPPr = childrenOf(next, 'w:pPr')[0]
+ const anchor = nextPPr ? nextPPr.nextSibling : next.firstChild
+ for (const node of [...p.childNodes]) {
+ if (node === pPr) continue
+ next.insertBefore(node, anchor)
+ }
+ p.parentNode.removeChild(p)
+ return true
+ }
+ for (const p of elementsOf(doc.documentElement, 'w:p')) {
+ const pPr = childrenOf(p, 'w:pPr')[0]
+ const rPr = pPr && childrenOf(pPr, 'w:rPr')[0]
+ if (!rPr) continue
+ const del = childrenOf(rPr, 'w:del')[0]
+ const ins = childrenOf(rPr, 'w:ins')[0]
+ if (del) { dirty = true; if (!accept || !mergeIntoNext(p, pPr)) rPr.removeChild(del) }
+ else if (ins) { dirty = true; if (accept || !mergeIntoNext(p, pPr)) rPr.removeChild(ins) }
+ }
+ for (const tr of elementsOf(doc.documentElement, 'w:tr')) {
+ const trPr = childrenOf(tr, 'w:trPr')[0]
+ if (!trPr) continue
+ const ins = childrenOf(trPr, 'w:ins')[0]
+ const del = childrenOf(trPr, 'w:del')[0]
+ if (ins) { dirty = true; if (accept) trPr.removeChild(ins); else tr.parentNode.removeChild(tr) }
+ else if (del) { dirty = true; if (accept) tr.parentNode.removeChild(tr); else trPr.removeChild(del) }
+ }
+ // Range markers next so wrappers see clean ranges.
for (const el of elementsOf(doc.documentElement)) {
const tag = el.tagName
if (tag === 'w:moveFromRangeStart' || tag === 'w:moveFromRangeEnd' || tag === 'w:moveToRangeStart' || tag === 'w:moveToRangeEnd'
@@ -478,18 +579,27 @@ export function resolveChanges(doc, accept) {
for (const t of elementsOf(el, 'w:delText')) { t.tagName = 'w:t' }
lift(el); dirty = true
}
- } else if (/^w:(pPrChange|rPrChange|tblPrChange|tcPrChange|trPrChange|sectPrChange|numberingChange)$/.test(tag)) {
- if (accept) { el.parentNode.removeChild(el); dirty = true }
+ } else if (tag === 'w:numberingChange') {
+ el.parentNode.removeChild(el); dirty = true
+ } else if (/^w:(pPrChange|rPrChange|tblPrChange|tcPrChange|trPrChange|sectPrChange)$/.test(tag)) {
+ const parent = el.parentNode
+ if (accept) { parent.removeChild(el); dirty = true }
else {
- // Restore the stored old properties into place of the change marker.
- const parent = el.parentNode
+ // Restore the stored old properties: the parent keeps the parts the
+ // stored copy never holds (a pPr's mark rPr and sectPr; a sectPr's
+ // header/footer references) and takes the rest from the stored copy.
const stored = childrenOf(el)[0]
- if (stored) parent.replaceChild(stored.cloneNode(true), el)
- else parent.removeChild(el)
+ const keep = new Set()
+ if (parent.tagName === 'w:pPr') { keep.add('w:rPr'); keep.add('w:sectPr') }
+ if (parent.tagName === 'w:sectPr') { keep.add('w:headerReference'); keep.add('w:footerReference') }
+ for (const c of [...parent.childNodes]) if (!keep.has(c.tagName) && c !== el) parent.removeChild(c)
+ if (stored) {
+ const anchor = [...parent.childNodes].find(c => keep.has(c.tagName)) ?? null
+ for (const s of [...stored.childNodes]) parent.insertBefore(s, anchor)
+ }
+ parent.removeChild(el)
dirty = true
}
- } else if (tag === 'w:del' && el.parentNode?.tagName === 'w:rPr' && el.parentNode.parentNode?.tagName === 'w:pPr') {
- // handled above via generic w:del
} else if (tag === 'w:cellIns' || tag === 'w:cellDel') {
const tc = ancestor(el, 'w:tc')
if (tc) {
@@ -499,25 +609,5 @@ export function resolveChanges(doc, accept) {
}
}
}
- // Deleted paragraph marks: w:pPr/w:rPr/w:del merges the paragraph with the next
- // on accept; on reject the mark alone disappears.
- for (const p of elementsOf(doc.documentElement, 'w:p')) {
- const pPr = firstOf(p, 'w:pPr')
- const rPr = pPr && firstOf(pPr, 'w:rPr')
- const del = rPr && firstOf(rPr, 'w:del')
- if (!del) continue
- dirty = true
- if (accept) {
- const next = p.nextSibling
- if (next && next.tagName === 'w:p') {
- for (const child of [...p.childNodes]) if (child !== pPr) next.insertBefore(child, next.firstChild)
- p.parentNode.removeChild(p)
- } else {
- rPr.removeChild(del)
- }
- } else {
- rPr.removeChild(del)
- }
- }
return dirty
}
diff --git a/ds-bot/src/office/package.js b/ds-bot/src/office/package.js
index 0960bf25..42117047 100644
--- a/ds-bot/src/office/package.js
+++ b/ds-bot/src/office/package.js
@@ -1,7 +1,7 @@
// The OOXML package: parts, [Content_Types].xml, and .rels files as a small model.
// Loads from a .docx/.pptx/.xlsx ZIP or from an `unpack` directory; saves back
// keeping untouched members' compressed bytes.
-import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, readFileSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
import { dirname, join, posix } from 'node:path'
import { parseXml, serializeXml, childrenOf } from './xml.js'
import { readZip, writeZip } from './zip.js'
@@ -15,6 +15,8 @@ const absTarget = (base, target) => {
return posix.normalize(posix.join(posix.dirname(base), target))
}
+const relSig = (map) => [...map.values()].map(r => `${r.id}${r.type}${r.target}${r.mode}`).join('')
+
/** One package: a name→{data|raw} part map plus content-type and relationship views. */
export class Package {
constructor() {
@@ -80,6 +82,9 @@ export class Package {
} catch { /* malformed rels surface through check */ }
this.rels.set(owner, map)
}
+ // Baselines for flush(): a .rels part is rewritten only when its map changed.
+ this.relSigs = new Map([...this.rels].map(([owner, map]) => [owner, relSig(map)]))
+ this.ctSig = JSON.stringify([...this.defaults].sort().concat([...this.overrides].filter(([n]) => this.parts.has(n)).sort()))
}
/** @returns {Buffer|undefined} part bytes */
@@ -121,13 +126,16 @@ export class Package {
return owner === '' ? '_rels/.rels' : `${posix.dirname(owner)}/_rels/${posix.basename(owner)}.rels`
}
- /** Write .rels and [Content_Types].xml back into parts from the model. */
+ /** Write .rels and [Content_Types].xml back into parts from the model — only
+ * when the model changed, so untouched members keep their original bytes. */
flush() {
for (const name of [...this.parts.keys()]) if (name.endsWith('.rels') && !this.#ownerStillHasRels(name)) this.parts.delete(name)
for (const [owner, map] of this.rels) {
if (owner !== '' && !this.parts.has(owner)) { this.rels.delete(owner); continue }
const name = this.relsName(owner)
if (map.size === 0) { this.parts.delete(name); continue }
+ const sig = relSig(map)
+ if (sig === (this.relSigs ??= new Map()).get(owner) && this.parts.has(name)) continue
const doc = parseXml('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>')
for (const rel of map.values()) {
const el = doc.createElement('Relationship')
@@ -136,16 +144,21 @@ export class Package {
doc.documentElement.appendChild(el)
}
this.putXml(name, doc)
+ this.relSigs.set(owner, sig)
}
- const ct = parseXml('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>')
- for (const [ext, type] of this.defaults) {
- const el = ct.createElement('Default'); el.setAttribute('Extension', ext); el.setAttribute('ContentType', type); ct.documentElement.appendChild(el)
- }
- for (const [name, type] of this.overrides) {
- if (!this.parts.has(name)) continue
- const el = ct.createElement('Override'); el.setAttribute('PartName', `/${name}`); el.setAttribute('ContentType', type); ct.documentElement.appendChild(el)
+ const ctSig = JSON.stringify([...this.defaults].sort() .concat([...this.overrides].filter(([n]) => this.parts.has(n)).sort()))
+ if (ctSig !== this.ctSig || !this.parts.has('[Content_Types].xml')) {
+ const ct = parseXml('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>')
+ for (const [ext, type] of this.defaults) {
+ const el = ct.createElement('Default'); el.setAttribute('Extension', ext); el.setAttribute('ContentType', type); ct.documentElement.appendChild(el)
+ }
+ for (const [name, type] of this.overrides) {
+ if (!this.parts.has(name)) continue
+ const el = ct.createElement('Override'); el.setAttribute('PartName', `/${name}`); el.setAttribute('ContentType', type); ct.documentElement.appendChild(el)
+ }
+ this.putXml('[Content_Types].xml', ct)
+ this.ctSig = ctSig
}
- this.putXml('[Content_Types].xml', ct)
this.parts.delete('') // never emit a directory entry
}
@@ -160,11 +173,12 @@ export class Package {
return writeZip([...this.parts.entries()].map(([name, part]) => ({ name, ...part })))
}
- /** Write an `unpack` directory tree. `dir` must be new or the output of a previous unpack. */
- unpack(dir, previous) {
- if (existsSync(dir)) {
- const names = readdirSync(dir)
- if (names.length > 0 && !(previous && previous === dir)) fail(`unpack: ${dir} exists and is not an earlier unpack`)
+ /** Write an `unpack` directory tree. An existing non-empty dir must hold an
+ * earlier unpack ([Content_Types].xml at its root); files the package no
+ * longer has are removed. */
+ unpack(dir) {
+ if (existsSync(dir) && readdirSync(dir).length > 0 && !existsSync(join(dir, '[Content_Types].xml'))) {
+ fail(`unpack: ${dir} exists and is not an earlier unpack`)
}
mkdirSync(dir, { recursive: true })
const keep = new Set()
@@ -178,6 +192,15 @@ export class Package {
}
writeFileSync(file, data)
}
+ const sweep = (rel) => {
+ for (const entry of readdirSync(join(dir, rel))) {
+ const path = rel === '' ? entry : `${rel}/${entry}`
+ const full = join(dir, path)
+ if (statSync(full).isDirectory()) { sweep(path); if (readdirSync(full).length === 0) rmdirSync(full) }
+ else if (!keep.has(full)) unlinkSync(full)
+ }
+ }
+ sweep('')
}
}
diff --git a/ds-bot/src/office/pptx.js b/ds-bot/src/office/pptx.js
index b70e59fc..828200cf 100644
--- a/ds-bot/src/office/pptx.js
+++ b/ds-bot/src/office/pptx.js
@@ -225,7 +225,10 @@ export function pptxLayout(pkg, slidesFilter, { autofit = false } = {}) {
const old = firstOf(bodyPr, tag)
if (old) bodyPr.removeChild(old)
}
- bodyPr.appendChild(doc.createElement('a:normAutofit'))
+ // a:normAutofit belongs before a:scene3d / a:sp3d / a:flatTx / a:extLst.
+ const anchor = childrenOf(bodyPr).find(c =>
+ ['a:scene3d', 'a:sp3d', 'a:flatTx', 'a:extLst'].includes(c.tagName))
+ bodyPr.insertBefore(doc.createElement('a:normAutofit'), anchor ?? null)
fitted.push(`${slide.position}:${shapeName(el)}`)
}
}
@@ -258,6 +261,8 @@ export function pptxSlides(pkg, order) {
const dup = String(nextIds.slide++)
const newPart = `ppt/slides/slide${dup}.xml`
pkg.parts.set(newPart, { data: pkg.get(source.part) })
+ if (pkg.overrides.has(source.part)) pkg.overrides.set(newPart, pkg.overrides.get(source.part))
+ const mapped = new Map([[source.part, newPart]])
const rid = `rId${nextIds.rid++}`
presRels.set(rid, { id: rid, type: presRels.get(source.rid)?.type ?? 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide', target: `slides/slide${dup}.xml`, mode: 'Internal' })
const sid = String(nextIds.sldId++)
@@ -267,11 +272,22 @@ export function pptxSlides(pkg, order) {
if (rel.mode === 'External') { slideRels.set(id, { ...rel }); continue }
const target = pkg.targetOf(source.part, rel.target)
if (/notesSlide|chart|package|oleObject|embeddings/i.test(rel.type) && pkg.parts.has(target)) {
- const newTarget = copyPart(pkg, target)
+ const newTarget = copyPart(pkg, target, mapped)
slideRels.set(id, { ...rel, target: posix.relative(posix.dirname(newPart), newTarget) })
} else slideRels.set(id, { ...rel })
}
pkg.rels.set(newPart, slideRels)
+ // Copied parts keep rels to mapped sources — the notes slide must point at
+ // the new slide, a chart at its new embedded workbook.
+ for (const copy of mapped.values()) {
+ const map = pkg.rels.get(copy)
+ if (!map) continue
+ for (const rel of map.values()) {
+ if (rel.mode === 'External') continue
+ const abs = pkg.targetOf(copy, rel.target)
+ if (mapped.has(abs)) rel.target = posix.relative(posix.dirname(copy), mapped.get(abs))
+ }
+ }
newEntries.push({ sldId: sid, rid, part: newPart })
duplicated.push(pos)
}
@@ -297,12 +313,13 @@ export function pptxSlides(pkg, order) {
const maxNum = (names, re) => Math.max(0, ...names.map(n => Number(re.exec(n)?.[1] ?? 0)))
const nextRid = (map) => { let i = 1; while (map.has(`rId${i}`)) i++; return i }
-function copyPart(pkg, part) {
+function copyPart(pkg, part, mapped = new Map()) {
const dir = posix.dirname(part), base = posix.basename(part)
const stem = base.replace(/\d+(\.\w+)$/, ''), extn = base.match(/\.\w+$/)[0]
const n = maxNum([...pkg.parts.keys()].filter(k => k.startsWith(dir + '/')), new RegExp(`${stem}(\\d+)${extn.replace('.', '\\.')}$`)) + 1
const target = `${dir}/${stem}${n}${extn}`
pkg.parts.set(target, { data: pkg.get(part) })
+ mapped.set(part, target)
if (pkg.overrides.has(part)) pkg.overrides.set(target, pkg.overrides.get(part))
// Copy that part's rels; duplicate its own embedded parts (chart workbooks).
const map = new Map()
@@ -310,7 +327,7 @@ function copyPart(pkg, part) {
if (rel.mode === 'External') { map.set(id, { ...rel }); continue }
const t = pkg.targetOf(part, rel.target)
if (/package|oleObject|workbook|embeddings|xlsx/i.test(rel.type) && pkg.parts.has(t)) {
- map.set(id, { ...rel, target: posix.relative(dir, copyPart(pkg, t)) })
+ map.set(id, { ...rel, target: posix.relative(dir, copyPart(pkg, t, mapped)) })
} else map.set(id, { ...rel })
}
if (map.size > 0) pkg.rels.set(target, map)
@@ -323,8 +340,22 @@ function copyPart(pkg, part) {
* remaining rel references.
* @returns {{ removed: string[] }}
*/
+// Relationship kinds that only matter when the owning XML references them:
+// deleting a picture leaves its r:embed rel behind, and so on. Structural rels
+// (layouts, masters, notes, themes, comments, tags) are not listed.
+const CONTENT_REL = /\/(image|media|video|audio|chart|oleObject|package|diagramData|diagramLayout|diagramColors|diagramQuickStyle|drawing|hyperlink)$/
+
export function pptxClean(pkg, { dryRun = false } = {}) {
const removed = []
+ // Drop content relationships whose Id no part XML uses any more.
+ for (const [owner, map] of [...pkg.rels]) {
+ if (!pkg.parts.has(owner)) continue
+ const xml = pkg.get(owner) ?? Buffer.alloc(0)
+ for (const [id, rel] of [...map]) {
+ if (rel.mode !== 'Internal' || !CONTENT_REL.test(rel.type)) continue
+ if (!xml.includes(`"${id}"`)) map.delete(id)
+ }
+ }
// Slides not in sldIdLst.
const listed = new Set(deckOrder(pkg).map(e => e.part))
for (const part of slideParts(pkg)) {
diff --git a/ds-bot/src/office/skills/office-tools/SKILL.md b/ds-bot/src/office/skills/office-tools/SKILL.md
index a9a545b1..7e6162ca 100644
--- a/ds-bot/src/office/skills/office-tools/SKILL.md
+++ b/ds-bot/src/office/skills/office-tools/SKILL.md
@@ -7,7 +7,7 @@ description: Inspect, check, and edit .docx/.pptx/.xlsx packages in place — bl
A bundled command line for the jobs the format skills cannot do safely: reading a document's structure with stable ids, repairing packages, targeted edits, tracked changes, layout findings, and contact sheets of rendered pages. Run every command through the Node and CLI paths in the runtime block at the end of this skill.
-Every command accepts `--json` and prints `{"ok":true,"data":…}` or `{"ok":false,"error":{"code","message"}}`. Exit codes: 0 = ok, 1 = findings remain, 2 = usage or failure. Edits write a new file (`-o out`) or overwrite the input (`--in-place`); they never touch the input otherwise.
+Every command accepts `--json` and prints `{"ok":true,"data":…}` for a run that completed, or `{"ok":false,"error":{"code","message"}}` when it failed. Findings are results, not failures: they land inside `data.findings` with `ok` still true, and the exit code carries them — 0 = ok, 1 = findings remain, 2 = usage or failure. Edits write a new file (`-o out`) or overwrite the input (`--in-place`); they never touch the input otherwise.
## Commands
diff --git a/ds-bot/test/fixtures/office/sample.docx b/ds-bot/test/fixtures/office/sample.docx
index ac3d98ee..18a786ba 100644
Binary files a/ds-bot/test/fixtures/office/sample.docx and b/ds-bot/test/fixtures/office/sample.docx differ
diff --git a/ds-bot/test/fixtures/office/sample.pptx b/ds-bot/test/fixtures/office/sample.pptx
index f9695859..dbb79d65 100644
Binary files a/ds-bot/test/fixtures/office/sample.pptx and b/ds-bot/test/fixtures/office/sample.pptx differ
diff --git a/ds-bot/test/fixtures/office/sample.xlsx b/ds-bot/test/fixtures/office/sample.xlsx
index c8f740e4..707d46ba 100644
Binary files a/ds-bot/test/fixtures/office/sample.xlsx and b/ds-bot/test/fixtures/office/sample.xlsx differ
diff --git a/ds-bot/test/office.test.js b/ds-bot/test/office.test.js
index 3b5f837b..46157b26 100644
--- a/ds-bot/test/office.test.js
+++ b/ds-bot/test/office.test.js
@@ -10,11 +10,11 @@ import assert from 'node:assert'
import { readZip } from '../src/office/zip.js'
import { Package } from '../src/office/package.js'
import { checkPackage, fixPackage } from '../src/office/check.js'
-import { docxRead } from '../src/office/docx.js'
+import { docxRead, docxEdit } from '../src/office/docx.js'
import { pptxInventory, pptxLayout } from '../src/office/pptx.js'
import { xlsxInventory, xlsxRead } from '../src/office/xlsx.js'
import { decodePng, encodePng, contactSheet } from '../src/office/png.js'
-import { parseXml } from '../src/office/xml.js'
+import { parseXml, elementsOf, childrenOf, textOf } from '../src/office/xml.js'
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)))
const FIXTURES = join(ROOT, 'test', 'fixtures', 'office')
@@ -259,4 +259,198 @@ describe('unpack/pack', () => {
assert.ok(res.ok)
assert.equal(checkPackage(Package.open(join(dir, 'back.docx'))).length, 0)
})
+ test('unpack reuses an earlier unpack and removes stale files', () => {
+ const dir = tmp()
+ const dest = join(dir, 'un')
+ cli('unpack', fixture('sample.docx'), dest)
+ writeFileSync(join(dest, 'stale.xml'), '<x/>')
+ cli('unpack', fixture('sample.docx'), dest)
+ assert.ok(!existsSync(join(dest, 'stale.xml')))
+ })
+ test('unpack refuses an existing directory that is not an unpack', () => {
+ const dir = tmp()
+ writeFileSync(join(dir, 'random.txt'), 'hi')
+ assert.throws(() => execFileSync(NODE, [CLI, 'unpack', fixture('sample.docx'), dir, '--json']), /unpack/)
+ })
+})
+
+describe('paragraph and row marks (review 1)', () => {
+ test('accept merges a paragraph with a deleted mark into the next, in order', () => {
+ const dir = tmp()
+ const out = join(dir, 'merged.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops', JSON.stringify([{ accept_changes: {} }]))
+ const { blocks } = docxRead(Package.open(out))
+ assert.equal(blocks.length, 6) // p5 merged into the field paragraph
+ const merged = blocks.find(b => /Final closing/.test(b.text))
+ assert.match(merged.text, /Final closing paragraph\. PAGE/)
+ assert.ok(!merged.deletedParagraphMark)
+ })
+ test('reject drops the deleted paragraph mark and keeps both paragraphs', () => {
+ const dir = tmp()
+ const out = join(dir, 'rejected.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops', JSON.stringify([{ reject_changes: {} }]))
+ const { blocks } = docxRead(Package.open(out))
+ assert.ok(blocks.some(b => b.text === 'Final closing paragraph.' && !b.deletedParagraphMark))
+ assert.ok(blocks.some(b => / PAGE /.test(b.text)))
+ })
+})
+
+describe('property change restore (review 2)', () => {
+ test('reject restores stored run properties under the run rPr', () => {
+ const dir = tmp()
+ const out = join(dir, 'rej.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops', JSON.stringify([{ reject_changes: {} }]))
+ const doc = Package.open(out).xml('word/document.xml')
+ // The "." run carried rPrChange storing a bold rPr: on reject the run
+ // becomes bold again, with no change element left.
+ const runs = elementsOf(doc.documentElement, 'w:r')
+ const dot = runs.find(r => textOf(r) === '.')
+ assert.ok(dot, 'no "." run')
+ assert.ok(childrenOf(childrenOf(dot, 'w:rPr')[0], 'w:b')[0] !== undefined)
+ assert.equal(elementsOf(doc.documentElement, 'w:rPrChange').length, 0)
+ })
+})
+
+describe('replace edge cases (review 3)', () => {
+ test('a→aa terminates (no re-scan of inserted text)', () => {
+ const dir = tmp()
+ const out = join(dir, 'aa.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops', JSON.stringify([{ replace: { find: 'a', with: 'aa' } }]))
+ const { blocks } = docxRead(Package.open(out))
+ assert.ok(blocks.some(b => (b.text ?? '').includes('aa')))
+ })
+ test('regex groups expand ($1), and case-insensitive flags hold', () => {
+ const dir = tmp()
+ const out = join(dir, 're.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops',
+ JSON.stringify([{ replace: { find: '(north) region', with: 'south-$1 area', regex: true } }]))
+ assert.match(docxRead(Package.open(out)).blocks[1].text, /south-north area/)
+ })
+})
+
+describe('comment op (review 4)', () => {
+ test('anchor wraps exactly the phrase and passes check', () => {
+ const dir = tmp()
+ const out = join(dir, 'c.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops',
+ JSON.stringify([{ comment: { paragraph: 'p2', text: 'look here', anchor: 'north' } }]))
+ const pkg = Package.open(out)
+ assert.deepEqual(checkPackage(pkg), [])
+ const doc = pkg.xml('word/document.xml')
+ const p = elementsOf(doc.documentElement, 'w:p').find(p => textOf(p).includes('south') || textOf(p).includes('north'))
+ const start = elementsOf(p, 'w:commentRangeStart')[0]
+ assert.ok(start)
+ // The range opens right before the run holding the phrase.
+ assert.match(textOf(start.nextSibling), /^north$/)
+ })
+ test('an anchor that is not in the paragraph fails the whole batch', () => {
+ const dir = tmp()
+ const out = join(dir, 'c2.docx')
+ const res = cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops',
+ JSON.stringify([{ comment: { paragraph: 'p2', text: 'x', anchor: 'zz-not-there' } }]))
+ assert.equal(res.ok, false)
+ assert.match(res.error.message, /op 0/)
+ assert.ok(!existsSync(out))
+ })
+})
+
+describe('field and cell edits (review 5, 6)', () => {
+ test('set_field rewrites the complex result and touches only document.xml', () => {
+ const dir = tmp()
+ const out = join(dir, 'f.docx')
+ const res = cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops',
+ JSON.stringify([{ set_field: { match: 'PAGE', value: '7' } }]))
+ assert.deepEqual(res.data.changed, ['word/document.xml'])
+ const fields = cli('docx', 'fields', out).data
+ assert.ok(fields.some(f => f.instruction.includes('PAGE') && f.result === '7'))
+ })
+ test('set_cell keeps tcPr, pPr and the run rPr', () => {
+ const dir = tmp()
+ const out = join(dir, 'cell.docx')
+ cli('docx', 'edit', fixture('sample.docx'), '-o', out, '--ops',
+ JSON.stringify([{ set_cell: { table: 1, row: 1, col: 1, text: 'New title' } }]))
+ const { blocks } = docxRead(Package.open(out))
+ assert.equal(blocks.find(b => b.id === 't1').rows[0][0].gridSpan, 2)
+ })
+})
+
+describe('pptx duplication and clean (review 7, 8)', () => {
+ test('duplicated slide gets its Override and the notes slide points back at it', () => {
+ const dir = tmp()
+ const out = join(dir, 'dup.pptx')
+ cli('pptx', 'slides', fixture('sample.pptx'), '-o', out, '--order', '1,1,2,3')
+ const pkg = Package.open(out)
+ const dup = 'ppt/slides/slide4.xml'
+ assert.equal(pkg.contentType(dup), 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml')
+ const notesRel = [...pkg.relsOf(dup).values()].find(r => r.type.endsWith('/notesSlide'))
+ assert.ok(notesRel, 'duplicated slide kept no notes slide')
+ const notes = pkg.targetOf(dup, notesRel.target)
+ const back = [...pkg.relsOf(notes).values()].find(r => r.type.endsWith('/slide'))
+ assert.equal(pkg.targetOf(notes, back.target), dup)
+ assert.deepEqual(checkPackage(pkg), [])
+ })
+ test('clean drops relationship ids that no XML uses', () => {
+ const dir = tmp()
+ const broken = join(dir, 'b.pptx')
+ const pkg = Package.open(fixture('sample.pptx'))
+ const doc = pkg.xml('ppt/slides/slide1.xml')
+ const pic = elementsOf(doc.documentElement, 'p:pic')[0]
+ pic.parentNode.removeChild(pic)
+ pkg.putXml('ppt/slides/slide1.xml', doc)
+ writeFileSync(broken, pkg.save())
+ const res = cli('pptx', 'clean', broken, '-o', join(dir, 'c.pptx'))
+ assert.ok(res.data.removed.some(n => n === 'ppt/media/image1.png'))
+ assert.deepEqual(checkPackage(Package.open(res.data.file)), [])
+ })
+})
+
+describe('check rules (review 10, 11)', () => {
+ test('o:id in VML is not a relationship reference', () => {
+ const pkg = Package.open(fixture('sample.docx'))
+ const doc = pkg.xml('word/document.xml')
+ const p = parseXml(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><w:r><w:pict><v:shape o:id="_x0000_s1" id="x" style="width:10pt;height:10pt"/></w:pict></w:r></w:p>`).documentElement
+ doc.documentElement.appendChild(doc.importNode(p, true))
+ pkg.putXml('word/document.xml', doc)
+ assert.ok(!checkPackage(pkg).some(f => f.rule === 'unknown-rel-ref'))
+ })
+ test('wrong-content-type reports and --fix sets the Override', () => {
+ const pkg = Package.open(fixture('sample.docx'))
+ pkg.override('word/styles.xml', 'application/xml')
+ const found = checkPackage(pkg)
+ assert.ok(found.some(f => f.rule === 'wrong-content-type' && f.part === 'word/styles.xml'))
+ const { remaining } = fixPackage(pkg)
+ assert.deepEqual(remaining, [])
+ assert.equal(pkg.contentType('word/styles.xml'), 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml')
+ })
+})
+
+describe('JSON findings shape (review 14)', () => {
+ test('findings come back ok:true with exit 1', () => {
+ const dir = tmp()
+ const pkg = Package.open(fixture('sample.docx'))
+ pkg.override('word/styles.xml', 'application/xml')
+ const file = join(dir, 'bad.docx')
+ writeFileSync(file, pkg.save())
+ const res = cli('check', file)
+ assert.equal(res.ok, true)
+ assert.ok(res.data.findings.some(f => f.rule === 'wrong-content-type'))
+ let status = 0
+ try { execFileSync(NODE, [CLI, 'check', file, '--json']) } catch (e) { status = e.status }
+ assert.equal(status, 1)
+ })
+})
+
+describe('flush keeps untouched members (review 16)', () => {
+ test('a document.xml-only edit preserves every other member byte', () => {
+ const src = fixture('sample.docx')
+ const pkg = Package.open(src)
+ const before = readZip(readFileSync(src))
+ docxEdit(pkg, [{ set_cell: { table: 1, row: 2, col: 2, text: '99' } }])
+ const after = readZip(pkg.save())
+ const changed = after.filter(m => {
+ const old = before.find(b => b.name === m.name)
+ return !old || !old.raw.equals(m.raw)
+ }).map(m => m.name)
+ assert.deepEqual(changed.sort(), ['word/document.xml'])
+ })
})