@folijs/docx 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +95 -15
- package/dist/export/balance.d.ts +17 -0
- package/dist/export/balance.d.ts.map +1 -0
- package/dist/export/balance.js +111 -0
- package/dist/export/balance.js.map +1 -0
- package/dist/export/content.d.ts +7 -0
- package/dist/export/content.d.ts.map +1 -1
- package/dist/export/content.js +185 -14
- package/dist/export/content.js.map +1 -1
- package/dist/export/controls.d.ts +18 -0
- package/dist/export/controls.d.ts.map +1 -0
- package/dist/export/controls.js +232 -0
- package/dist/export/controls.js.map +1 -0
- package/dist/export/index.d.ts +6 -1
- package/dist/export/index.d.ts.map +1 -1
- package/dist/export/index.js +277 -28
- package/dist/export/index.js.map +1 -1
- package/dist/export/parts.d.ts +9 -1
- package/dist/export/parts.d.ts.map +1 -1
- package/dist/export/parts.js +69 -2
- package/dist/export/parts.js.map +1 -1
- package/dist/export/pictures.d.ts +54 -0
- package/dist/export/pictures.d.ts.map +1 -0
- package/dist/export/pictures.js +359 -0
- package/dist/export/pictures.js.map +1 -0
- package/dist/export/properties.d.ts +11 -1
- package/dist/export/properties.d.ts.map +1 -1
- package/dist/export/properties.js +57 -3
- package/dist/export/properties.js.map +1 -1
- package/dist/export/tables.d.ts +27 -0
- package/dist/export/tables.d.ts.map +1 -0
- package/dist/export/tables.js +344 -0
- package/dist/export/tables.js.map +1 -0
- package/dist/images.d.ts +21 -0
- package/dist/images.d.ts.map +1 -0
- package/dist/images.js +53 -0
- package/dist/images.js.map +1 -0
- package/dist/import/content.d.ts +15 -1
- package/dist/import/content.d.ts.map +1 -1
- package/dist/import/content.js +357 -15
- package/dist/import/content.js.map +1 -1
- package/dist/import/controls.d.ts +14 -0
- package/dist/import/controls.d.ts.map +1 -0
- package/dist/import/controls.js +123 -0
- package/dist/import/controls.js.map +1 -0
- package/dist/import/index.d.ts +10 -2
- package/dist/import/index.d.ts.map +1 -1
- package/dist/import/index.js +76 -13
- package/dist/import/index.js.map +1 -1
- package/dist/import/ooxml.d.ts +4 -0
- package/dist/import/ooxml.d.ts.map +1 -1
- package/dist/import/ooxml.js +4 -0
- package/dist/import/ooxml.js.map +1 -1
- package/dist/import/parts.d.ts.map +1 -1
- package/dist/import/parts.js +8 -1
- package/dist/import/parts.js.map +1 -1
- package/dist/import/pictures.d.ts +34 -0
- package/dist/import/pictures.d.ts.map +1 -0
- package/dist/import/pictures.js +147 -0
- package/dist/import/pictures.js.map +1 -0
- package/dist/import/properties.d.ts +2 -0
- package/dist/import/properties.d.ts.map +1 -1
- package/dist/import/properties.js +55 -0
- package/dist/import/properties.js.map +1 -1
- package/dist/import/report.d.ts +6 -0
- package/dist/import/report.d.ts.map +1 -1
- package/dist/import/report.js +5 -0
- package/dist/import/report.js.map +1 -1
- package/dist/import/stories.d.ts +19 -0
- package/dist/import/stories.d.ts.map +1 -0
- package/dist/import/stories.js +119 -0
- package/dist/import/stories.js.map +1 -0
- package/dist/import/tables.d.ts +31 -0
- package/dist/import/tables.d.ts.map +1 -0
- package/dist/import/tables.js +236 -0
- package/dist/import/tables.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/settings.d.ts +18 -4
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +173 -26
- package/dist/settings.js.map +1 -1
- package/package.json +3 -3
- package/src/export/balance.ts +113 -0
- package/src/export/content.ts +240 -15
- package/src/export/controls.ts +259 -0
- package/src/export/index.ts +357 -32
- package/src/export/parts.ts +94 -2
- package/src/export/pictures.ts +411 -0
- package/src/export/properties.ts +69 -3
- package/src/export/tables.ts +386 -0
- package/src/images.ts +60 -0
- package/src/import/content.ts +365 -17
- package/src/import/controls.ts +137 -0
- package/src/import/index.ts +87 -12
- package/src/import/ooxml.ts +4 -0
- package/src/import/parts.ts +14 -1
- package/src/import/pictures.ts +176 -0
- package/src/import/properties.ts +59 -0
- package/src/import/report.ts +11 -0
- package/src/import/stories.ts +122 -0
- package/src/import/tables.ts +267 -0
- package/src/index.ts +1 -0
- package/src/settings.ts +191 -27
package/src/import/content.ts
CHANGED
|
@@ -4,10 +4,18 @@
|
|
|
4
4
|
* for markers that have nothing to show: field characters, range ends).
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
type ControlJSON,
|
|
9
|
+
type MarkJSON,
|
|
10
|
+
type NodeJSON,
|
|
11
|
+
type ResourceJSON,
|
|
12
|
+
schema,
|
|
13
|
+
} from "@folijs/core";
|
|
8
14
|
import type { DocxPackage, Relationship } from "../package.js";
|
|
9
15
|
import type { XmlElement, XmlNode } from "../xml.js";
|
|
10
|
-
import {
|
|
16
|
+
import { controlProperties } from "./controls.js";
|
|
17
|
+
import { attr, child, deepText, elements, integer, isW, R, REL, textOf, W } from "./ooxml.js";
|
|
18
|
+
import { pictureAttrs } from "./pictures.js";
|
|
11
19
|
import {
|
|
12
20
|
attributeBag,
|
|
13
21
|
type Context,
|
|
@@ -18,6 +26,7 @@ import {
|
|
|
18
26
|
setParagraph,
|
|
19
27
|
sourceOf,
|
|
20
28
|
} from "./properties.js";
|
|
29
|
+
import { cellProperties, grid, rowProperties, tableProperties } from "./tables.js";
|
|
21
30
|
|
|
22
31
|
export interface ContentContext extends Context {
|
|
23
32
|
readonly pkg: DocxPackage;
|
|
@@ -27,8 +36,23 @@ export interface ContentContext extends Context {
|
|
|
27
36
|
readonly moves: Moves;
|
|
28
37
|
/** The part's relationship with this id (hyperlink targets). */
|
|
29
38
|
readonly relationship: (id: string) => Relationship | undefined;
|
|
39
|
+
/** How many tables the content is in. */
|
|
40
|
+
readonly tableDepth?: number;
|
|
41
|
+
/** The envelope's resources (pictures' images), filled as content is read. */
|
|
42
|
+
readonly resources: Map<string, ResourceJSON>;
|
|
43
|
+
/** The envelope's content controls, filled as content is read. */
|
|
44
|
+
readonly controls: Map<string, ControlJSON>;
|
|
45
|
+
/** Numbers the part's controls: their ids are `<part>#<n>`, the same on every reading. */
|
|
46
|
+
readonly ordinals: { next: number };
|
|
47
|
+
/**
|
|
48
|
+
* The kind of story being read (ADR-0007), or a comment: what one may not hold stays verbatim.
|
|
49
|
+
*/
|
|
50
|
+
readonly story?: "header" | "footer" | "footnote" | "endnote" | "comment";
|
|
30
51
|
}
|
|
31
52
|
|
|
53
|
+
/** Tables nested deeper stay opaque: within the model's depth (three levels per table). */
|
|
54
|
+
const MAX_TABLE_DEPTH = 64;
|
|
55
|
+
|
|
32
56
|
/** Labels of the opaque elements that show something; anything else unknown shows its name. */
|
|
33
57
|
const BLOCK_LABELS: Record<string, string | null> = {
|
|
34
58
|
tbl: "table",
|
|
@@ -78,6 +102,9 @@ const INLINE_LABELS: Record<string, string | null> = {
|
|
|
78
102
|
customXmlDelRangeEnd: null,
|
|
79
103
|
};
|
|
80
104
|
|
|
105
|
+
/** What a separator note holds: kept in its run, with nothing to report or edit. */
|
|
106
|
+
const NOTE_SEPARATORS = new Set(["separator", "continuationSeparator", "continuationNotice"]);
|
|
107
|
+
|
|
81
108
|
/** Recomputed by Word, never kept: spelling marks, the last rendered page breaks. */
|
|
82
109
|
const DROPPED = new Set(["proofErr", "lastRenderedPageBreak"]);
|
|
83
110
|
|
|
@@ -97,27 +124,216 @@ export function blocks(
|
|
|
97
124
|
section?: XmlElement,
|
|
98
125
|
): NodeJSON[] {
|
|
99
126
|
const out: NodeJSON[] = [];
|
|
127
|
+
const tables = new Map<NodeJSON, XmlElement>();
|
|
100
128
|
for (const node of nodes) {
|
|
101
129
|
if (node.kind !== "element" || node === section) continue;
|
|
102
130
|
if (isW(node, "p")) out.push(paragraph(node, context));
|
|
103
|
-
else if (node
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
131
|
+
else if (isW(node, "sdt"))
|
|
132
|
+
out.push(...(blockControl(node, context) ?? [opaqueOf(node, context)]));
|
|
133
|
+
else if (isW(node, "tbl")) {
|
|
134
|
+
// A table right after a table is one table to Word (it merges them): kept verbatim.
|
|
135
|
+
const table = out.at(-1)?.type === "table" ? undefined : tableOf(node, context);
|
|
136
|
+
if (table) tables.set(table, node);
|
|
137
|
+
out.push(table ?? opaqueOf(node, context));
|
|
138
|
+
} else if (node.ns === W && DROPPED.has(node.local)) context.report.dropped(node.name);
|
|
139
|
+
else out.push(opaqueOf(node, context));
|
|
140
|
+
}
|
|
141
|
+
// Content does not end with a table (Word always puts a paragraph after one).
|
|
142
|
+
const last = out.at(-1);
|
|
143
|
+
const source = last && tables.get(last);
|
|
144
|
+
if (source) out[out.length - 1] = opaqueOf(source, context);
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A control's properties taken into the envelope, its id and its content element. */
|
|
149
|
+
function controlOf(
|
|
150
|
+
sdt: XmlElement,
|
|
151
|
+
context: ContentContext,
|
|
152
|
+
): { id: string; content: XmlElement } | undefined {
|
|
153
|
+
const content = child(sdt, "sdtContent");
|
|
154
|
+
if (!content || sdt.attributes.length) return undefined;
|
|
155
|
+
const children = elements(sdt);
|
|
156
|
+
const once = (local: string) => children.filter((c) => isW(c, local)).length <= 1;
|
|
157
|
+
const known = new Set(["sdtPr", "sdtEndPr", "sdtContent"]);
|
|
158
|
+
if (children.some((c) => !isW(c) || !known.has(c.local))) return undefined;
|
|
159
|
+
if (![...known].every(once)) return undefined;
|
|
160
|
+
const control = controlProperties(sdt, context);
|
|
161
|
+
if (!control) return undefined;
|
|
162
|
+
const id = `${context.part}#${context.ordinals.next++}`;
|
|
163
|
+
context.controls.set(id, control);
|
|
164
|
+
return { id, content };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** How many controls the model lets a block, row or cell be in. */
|
|
168
|
+
const MAX_CONTROLS = 64;
|
|
169
|
+
const roomForControl = (node: NodeJSON) =>
|
|
170
|
+
((node.attrs?.controls as string[] | undefined)?.length ?? 0) < MAX_CONTROLS;
|
|
171
|
+
|
|
172
|
+
/** `node` inside control `id`, outermost first. */
|
|
173
|
+
function inControl(node: NodeJSON, id: string): NodeJSON {
|
|
174
|
+
const controls = [id, ...((node.attrs?.controls as string[] | undefined) ?? [])];
|
|
175
|
+
return { ...node, attrs: { ...node.attrs, controls } };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** A block-level control's blocks, each listing it; undefined keeps it verbatim (one that is empty). */
|
|
179
|
+
function blockControl(sdt: XmlElement, context: ContentContext): NodeJSON[] | undefined {
|
|
180
|
+
const control = controlOf(sdt, context);
|
|
181
|
+
if (!control) return undefined;
|
|
182
|
+
const inner = blocks(control.content.children, context);
|
|
183
|
+
if (!inner.length || !inner.every(roomForControl)) return undefined;
|
|
184
|
+
return inner.map((block) => inControl(block, control.id));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The rows (or cells) among `nodes`, those in row-level (cell-level) controls listing them;
|
|
189
|
+
* undefined when anything else is there, which keeps the table verbatim.
|
|
190
|
+
*/
|
|
191
|
+
function controlled(
|
|
192
|
+
nodes: readonly XmlNode[],
|
|
193
|
+
local: "tr" | "tc",
|
|
194
|
+
read: (el: XmlElement) => NodeJSON | undefined,
|
|
195
|
+
context: ContentContext,
|
|
196
|
+
): NodeJSON[] | undefined {
|
|
197
|
+
const out: NodeJSON[] = [];
|
|
198
|
+
for (const node of nodes) {
|
|
199
|
+
if (node.kind !== "element") continue;
|
|
200
|
+
if (isW(node, local)) {
|
|
201
|
+
const item = read(node);
|
|
202
|
+
if (!item) return undefined;
|
|
203
|
+
out.push(item);
|
|
204
|
+
} else if (isW(node, "sdt")) {
|
|
205
|
+
const control = controlOf(node, context);
|
|
206
|
+
const inner = control && controlled(control.content.children, local, read, context);
|
|
207
|
+
if (!control || !inner?.length || !inner.every(roomForControl)) return undefined;
|
|
208
|
+
out.push(...inner.map((item) => inControl(item, control.id)));
|
|
209
|
+
} else return undefined;
|
|
111
210
|
}
|
|
112
211
|
return out;
|
|
113
212
|
}
|
|
114
213
|
|
|
214
|
+
function opaqueOf(node: XmlElement, context: ContentContext): NodeJSON {
|
|
215
|
+
context.report.opaque(node.name);
|
|
216
|
+
return {
|
|
217
|
+
type: "opaqueBlock",
|
|
218
|
+
attrs: withLabel({ xml: sourceOf(context, node) }, label(BLOCK_LABELS, node)),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* `w:tbl` as a table; undefined when the model cannot hold it as it is (rows or cells inside a
|
|
224
|
+
* content control or custom XML, markers between rows, elements out of Word's order, a cell
|
|
225
|
+
* not ending with a paragraph, tables nested too deep): the caller keeps it verbatim.
|
|
226
|
+
*/
|
|
227
|
+
function tableOf(tbl: XmlElement, context: ContentContext): NodeJSON | undefined {
|
|
228
|
+
const tableDepth = (context.tableDepth ?? 0) + 1;
|
|
229
|
+
if (tableDepth > MAX_TABLE_DEPTH || tbl.attributes.length) return undefined;
|
|
230
|
+
const inner: ContentContext = { ...context, tableDepth };
|
|
231
|
+
let tblPr: XmlElement | undefined;
|
|
232
|
+
let tblGrid: XmlElement | undefined;
|
|
233
|
+
const rows: NodeJSON[] = [];
|
|
234
|
+
let stage = 0; // 0: w:tblPr may come, 1: w:tblGrid may come, 2: rows
|
|
235
|
+
for (const c of tbl.children) {
|
|
236
|
+
if (c.kind !== "element") continue;
|
|
237
|
+
if (stage === 0 && isW(c, "tblPr")) {
|
|
238
|
+
tblPr = c;
|
|
239
|
+
stage = 1;
|
|
240
|
+
} else if (stage <= 1 && isW(c, "tblGrid")) {
|
|
241
|
+
tblGrid = c;
|
|
242
|
+
stage = 2;
|
|
243
|
+
} else if (isW(c, "tr") || isW(c, "sdt")) {
|
|
244
|
+
stage = 2;
|
|
245
|
+
const read = controlled([c], "tr", (tr) => rowOf(tr, inner), inner);
|
|
246
|
+
if (!read) return undefined;
|
|
247
|
+
rows.push(...read);
|
|
248
|
+
} else return undefined;
|
|
249
|
+
}
|
|
250
|
+
if (!rows.length) return undefined;
|
|
251
|
+
return {
|
|
252
|
+
type: "table",
|
|
253
|
+
attrs: {
|
|
254
|
+
...tableProperties(tblPr, context, true),
|
|
255
|
+
grid: grid(tblGrid, context),
|
|
256
|
+
...(tblGrid ? { gridExt: sourceOf(context, tblGrid) } : {}),
|
|
257
|
+
},
|
|
258
|
+
content: rows,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const ROW_ATTRS = schema.nodes.tableRow?.spec.attrs ?? {};
|
|
263
|
+
|
|
264
|
+
function rowOf(tr: XmlElement, context: ContentContext): NodeJSON | undefined {
|
|
265
|
+
let tblPrEx: XmlElement | undefined;
|
|
266
|
+
let trPr: XmlElement | undefined;
|
|
267
|
+
const cells: NodeJSON[] = [];
|
|
268
|
+
let stage = 0; // 0: w:tblPrEx, 1: w:trPr, 2: cells
|
|
269
|
+
for (const c of tr.children) {
|
|
270
|
+
if (c.kind !== "element") continue;
|
|
271
|
+
if (stage === 0 && isW(c, "tblPrEx")) {
|
|
272
|
+
tblPrEx = c;
|
|
273
|
+
stage = 1;
|
|
274
|
+
} else if (stage <= 1 && isW(c, "trPr")) {
|
|
275
|
+
trPr = c;
|
|
276
|
+
stage = 2;
|
|
277
|
+
} else if (isW(c, "tc") || isW(c, "sdt")) {
|
|
278
|
+
stage = 2;
|
|
279
|
+
const read = controlled([c], "tc", (tc) => cellOf(tc, context), context);
|
|
280
|
+
if (!read) return undefined;
|
|
281
|
+
cells.push(...read);
|
|
282
|
+
} else return undefined;
|
|
283
|
+
}
|
|
284
|
+
if (!cells.length) return undefined;
|
|
285
|
+
const attrs: Record<string, unknown> = rowProperties(trPr, context, true);
|
|
286
|
+
if (tblPrEx) {
|
|
287
|
+
const exceptions = tableProperties(tblPrEx, context, true);
|
|
288
|
+
// Not a row's to override (CT_TblPrEx): left in the element, not modeled.
|
|
289
|
+
for (const key of ["style", "rowBandSize", "columnBandSize"]) {
|
|
290
|
+
if (key in exceptions) {
|
|
291
|
+
context.report.unmodeled(tblPrEx.name, `${key} is not an exception (kept in the element)`);
|
|
292
|
+
delete exceptions[key];
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const error = ROW_ATTRS.exceptions?.validate?.(exceptions);
|
|
296
|
+
// Its values are checked one by one: only a too-large element can fail here.
|
|
297
|
+
if (error) return undefined;
|
|
298
|
+
attrs.exceptions = exceptions;
|
|
299
|
+
}
|
|
300
|
+
const bag = attributeBag(tr, [], context);
|
|
301
|
+
if (bag) attrs.attributes = bag;
|
|
302
|
+
return { type: "tableRow", ...(Object.keys(attrs).length ? { attrs } : {}), content: cells };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function cellOf(tc: XmlElement, context: ContentContext): NodeJSON | undefined {
|
|
306
|
+
if (tc.attributes.length) return undefined;
|
|
307
|
+
const first = tc.children.find((c): c is XmlElement => c.kind === "element");
|
|
308
|
+
const tcPr = first && isW(first, "tcPr") ? first : undefined;
|
|
309
|
+
const content = blocks(
|
|
310
|
+
tc.children.filter((c) => c !== tcPr),
|
|
311
|
+
context,
|
|
312
|
+
);
|
|
313
|
+
if (content.at(-1)?.type !== "paragraph" || !content.every(allowedInCell)) return undefined;
|
|
314
|
+
const attrs = cellProperties(tcPr, context, true);
|
|
315
|
+
return { type: "tableCell", ...(Object.keys(attrs).length ? { attrs } : {}), content };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Whether a cell's block is one the model allows there: not a paragraph that ends a section or
|
|
320
|
+
* holds a page or column break. A table with one stays verbatim (lossless) instead.
|
|
321
|
+
*/
|
|
322
|
+
function allowedInCell(block: NodeJSON): boolean {
|
|
323
|
+
if (block.type !== "paragraph") return true;
|
|
324
|
+
if (block.attrs?.section) return false;
|
|
325
|
+
return !block.content?.some((n) => n.type === "pageBreak" || n.type === "columnBreak");
|
|
326
|
+
}
|
|
327
|
+
|
|
115
328
|
function withLabel(attrs: Record<string, unknown>, value: string | null): Record<string, unknown> {
|
|
116
329
|
return value === null ? attrs : { ...attrs, label: value.slice(0, 255) || null };
|
|
117
330
|
}
|
|
118
331
|
|
|
119
332
|
export function paragraph(p: XmlElement, context: ContentContext): NodeJSON {
|
|
120
|
-
|
|
333
|
+
// A story ends no section: such a paragraph stays as it is.
|
|
334
|
+
const pPr = child(p, "pPr");
|
|
335
|
+
if (context.story && pPr && child(pPr, "sectPr")) return opaqueOf(p, context);
|
|
336
|
+
const attrs: Record<string, unknown> = paragraphProperties(pPr, context, true);
|
|
121
337
|
const bag = attributeBag(p, [], context);
|
|
122
338
|
if (bag) setParagraph(attrs, "attributes", bag, p.name, context);
|
|
123
339
|
const content: NodeJSON[] = [];
|
|
@@ -199,6 +415,11 @@ function inlines(
|
|
|
199
415
|
case "commentRangeStart":
|
|
200
416
|
case "commentRangeEnd": {
|
|
201
417
|
const type = MARKER_TYPES[el.local] as string;
|
|
418
|
+
// Stories hold no comments (ADR-0007): such an anchor stays verbatim.
|
|
419
|
+
if (type.startsWith("comment") && context.story && context.story !== "comment") {
|
|
420
|
+
opaque(el, BLOCK_LABELS);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
202
423
|
const isStart = type === "bookmarkStart";
|
|
203
424
|
const bag = attributeBag(el, isStart ? ["w:id", "w:name"] : ["w:id"], context);
|
|
204
425
|
const attrs: Record<string, unknown> = { id: integer(attr(el, "id")) };
|
|
@@ -223,9 +444,37 @@ function inlines(
|
|
|
223
444
|
context.moves.close(el.local.startsWith("moveFrom") ? "from" : "to", attr(el, "id") ?? "");
|
|
224
445
|
opaque(el);
|
|
225
446
|
break;
|
|
226
|
-
case "fldSimple":
|
|
227
|
-
|
|
447
|
+
case "fldSimple": {
|
|
448
|
+
const instruction = attr(el, "instr");
|
|
449
|
+
const start = node("fieldStart", {
|
|
450
|
+
simple: true,
|
|
451
|
+
// The start tag, as an element holding the result has it.
|
|
452
|
+
ext: context.source.slice(el.start, el.openEnd).replace(/\/>$/, ">"),
|
|
453
|
+
});
|
|
454
|
+
const code = node("fieldCode", { text: instruction ?? "" });
|
|
455
|
+
const leaves = valid(start, el, context) && valid(code, el, context);
|
|
456
|
+
if (instruction === undefined || !leaves) {
|
|
457
|
+
opaque(el, INLINE_LABELS, deepText(el) || "field");
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
out.push(start, code, node("fieldSeparator"));
|
|
461
|
+
inlines(el.children, marks, context, out);
|
|
462
|
+
out.push(node("fieldEnd"));
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
case "sdt": {
|
|
466
|
+
const blocksInside = (c: XmlElement) =>
|
|
467
|
+
elements(c).some((e) => isW(e, "p") || isW(e, "tbl"));
|
|
468
|
+
const control = controlOf(el, context);
|
|
469
|
+
if (!control || blocksInside(control.content)) {
|
|
470
|
+
opaque(el);
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
out.push(node("controlStart", { id: control.id }));
|
|
474
|
+
inlines(control.content.children, marks, context, out);
|
|
475
|
+
out.push(node("controlEnd", { id: control.id }));
|
|
228
476
|
break;
|
|
477
|
+
}
|
|
229
478
|
default:
|
|
230
479
|
if (DROPPED.has(el.local)) context.report.dropped(el.name);
|
|
231
480
|
else opaque(el);
|
|
@@ -353,7 +602,9 @@ function run(
|
|
|
353
602
|
break;
|
|
354
603
|
case "br": {
|
|
355
604
|
const type = attr(el, "type");
|
|
356
|
-
|
|
605
|
+
const inNote = context.story === "footnote" || context.story === "endnote";
|
|
606
|
+
if ((type === "page" || type === "column") && inNote) opaqueRun(el);
|
|
607
|
+
else if (type === "page") out.push(node("pageBreak", marks));
|
|
357
608
|
else if (type === "column") out.push(node("columnBreak", marks));
|
|
358
609
|
else {
|
|
359
610
|
const clear = attr(el, "clear");
|
|
@@ -372,7 +623,29 @@ function run(
|
|
|
372
623
|
case "softHyphen":
|
|
373
624
|
text("");
|
|
374
625
|
break;
|
|
626
|
+
case "drawing": {
|
|
627
|
+
const image: [string?, ResourceJSON?] = [];
|
|
628
|
+
const attrs = pictureAttrs(
|
|
629
|
+
el,
|
|
630
|
+
{ ...context, partOf: (name) => context.pkg.part(name) },
|
|
631
|
+
image,
|
|
632
|
+
);
|
|
633
|
+
const leaf = attrs && valid(node("picture", marks, attrs), el, context);
|
|
634
|
+
if (!leaf) {
|
|
635
|
+
opaqueRun(el);
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
out.push(leaf);
|
|
639
|
+
const [key, entry] = image;
|
|
640
|
+
if (key && entry && !context.resources.has(key)) context.resources.set(key, entry);
|
|
641
|
+
if (!attrs.alt && !attrs.decorative) context.report.withoutAlt();
|
|
642
|
+
break;
|
|
643
|
+
}
|
|
375
644
|
case "commentReference": {
|
|
645
|
+
if (context.story && context.story !== "comment") {
|
|
646
|
+
opaqueRun(el);
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
376
649
|
const bag = attributeBag(el, ["w:id"], context);
|
|
377
650
|
const attrs: Record<string, unknown> = { id: integer(attr(el, "id")) };
|
|
378
651
|
if (bag) attrs.attributes = bag;
|
|
@@ -381,16 +654,91 @@ function run(
|
|
|
381
654
|
else opaqueRun(el);
|
|
382
655
|
break;
|
|
383
656
|
}
|
|
657
|
+
case "fldChar": {
|
|
658
|
+
const type = attr(el, "fldCharType");
|
|
659
|
+
const name = { begin: "fieldStart", separate: "fieldSeparator", end: "fieldEnd" }[
|
|
660
|
+
type ?? ""
|
|
661
|
+
];
|
|
662
|
+
if (!name) {
|
|
663
|
+
opaqueRun(el);
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
const known = ["w:fldCharType", "w:dirty", "w:fldLock"];
|
|
667
|
+
const plain = !elements(el).length && el.attributes.every((a) => known.includes(a.name));
|
|
668
|
+
const flags: Record<string, unknown> = {};
|
|
669
|
+
if (name === "fieldStart" && plain) {
|
|
670
|
+
const dirty = attr(el, "dirty");
|
|
671
|
+
const locked = attr(el, "fldLock");
|
|
672
|
+
if (dirty !== undefined) flags.dirty = !["0", "false", "off"].includes(dirty);
|
|
673
|
+
if (locked !== undefined) flags.locked = !["0", "false", "off"].includes(locked);
|
|
674
|
+
}
|
|
675
|
+
// Form field data, or anything else on it: the element itself is kept.
|
|
676
|
+
const simple = plain && (name === "fieldStart" || el.attributes.length === 1);
|
|
677
|
+
const attrs = simple ? flags : { ext: sourceOf(context, el) };
|
|
678
|
+
const leaf = valid(
|
|
679
|
+
node(name, marks, Object.keys(attrs).length ? attrs : undefined),
|
|
680
|
+
el,
|
|
681
|
+
context,
|
|
682
|
+
);
|
|
683
|
+
if (leaf) out.push(leaf);
|
|
684
|
+
else opaqueRun(el);
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
case "instrText":
|
|
688
|
+
case "delInstrText": {
|
|
689
|
+
const bag = attributeBag(el, ["xml:space"], context);
|
|
690
|
+
const attrs: Record<string, unknown> = { text: spaced(el) };
|
|
691
|
+
if (bag) attrs.attributes = bag;
|
|
692
|
+
const leaf = valid(node("fieldCode", marks, attrs), el, context);
|
|
693
|
+
if (leaf) out.push(leaf);
|
|
694
|
+
else opaqueRun(el);
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
case "footnoteReference":
|
|
698
|
+
case "endnoteReference": {
|
|
699
|
+
const id = integer(attr(el, "id"));
|
|
700
|
+
const custom = attr(el, "customMarkFollows");
|
|
701
|
+
const on = custom === undefined ? null : ["1", "true", "on"].includes(custom);
|
|
702
|
+
// A story holds no references (ADR-0007), and a value the node cannot say stays verbatim.
|
|
703
|
+
if (
|
|
704
|
+
context.story ||
|
|
705
|
+
id === undefined ||
|
|
706
|
+
(custom !== undefined && !["0", "1", "true", "false", "on", "off"].includes(custom))
|
|
707
|
+
) {
|
|
708
|
+
opaqueRun(el);
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
const bag = attributeBag(el, ["w:id", "w:customMarkFollows"], context);
|
|
712
|
+
const attrs: Record<string, unknown> = {
|
|
713
|
+
story: `${el.local === "footnoteReference" ? "footnote" : "endnote"}:${id}`,
|
|
714
|
+
};
|
|
715
|
+
if (on !== null) attrs.customMark = on;
|
|
716
|
+
if (bag) attrs.attributes = bag;
|
|
717
|
+
const leaf = valid(node("noteReference", marks, attrs), el, context);
|
|
718
|
+
if (leaf) out.push(leaf);
|
|
719
|
+
else opaqueRun(el);
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
case "footnoteRef":
|
|
723
|
+
case "endnoteRef":
|
|
724
|
+
if (context.story === el.local.slice(0, -3) && !el.attributes.length)
|
|
725
|
+
out.push(node("noteMark", marks));
|
|
726
|
+
else opaqueRun(el);
|
|
727
|
+
break;
|
|
384
728
|
default:
|
|
385
729
|
if (DROPPED.has(el.local)) context.report.dropped(el.name);
|
|
386
|
-
else opaqueRun(el);
|
|
730
|
+
else opaqueRun(el, undefined, context.story && NOTE_SEPARATORS.has(el.local));
|
|
387
731
|
}
|
|
388
732
|
}
|
|
389
733
|
// A run with properties and nothing else shows nothing; Word drops those too.
|
|
390
734
|
if (out.length === before) context.report.dropped("w:r (empty)");
|
|
391
735
|
|
|
392
|
-
function opaqueRun(
|
|
393
|
-
|
|
736
|
+
function opaqueRun(
|
|
737
|
+
el: XmlElement,
|
|
738
|
+
labelValue = label(INLINE_LABELS, el),
|
|
739
|
+
expected: boolean | undefined = false,
|
|
740
|
+
): void {
|
|
741
|
+
if (!expected) context.report.opaque(el.name);
|
|
394
742
|
out.push(
|
|
395
743
|
node("opaqueInline", marks, {
|
|
396
744
|
...withLabel({ xml: sourceOf(context, el) }, labelValue),
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content controls (`w:sdt`, ADR-0007): their properties as the envelope's `controls`, with the
|
|
3
|
+
* elements they were read from kept verbatim in `ext`. Export reads the same way, to tell what a
|
|
4
|
+
* control says differently from its `ext`.
|
|
5
|
+
*/
|
|
6
|
+
import { type ControlJSON, checkControl } from "@folijs/core";
|
|
7
|
+
import type { DocxPackage } from "../package.js";
|
|
8
|
+
import type { XmlElement } from "../xml.js";
|
|
9
|
+
import { attr, child, deepText, elements, isW, W, W14, W15 } from "./ooxml.js";
|
|
10
|
+
import { type Context, compactDefined, runProperties, sourceOf } from "./properties.js";
|
|
11
|
+
|
|
12
|
+
const KINDS: readonly [string, string, ControlJSON["kind"]][] = [
|
|
13
|
+
[W, "text", "text"],
|
|
14
|
+
[W, "richText", "richText"],
|
|
15
|
+
[W, "dropDownList", "dropDown"],
|
|
16
|
+
[W, "comboBox", "comboBox"],
|
|
17
|
+
[W, "date", "date"],
|
|
18
|
+
[W14, "checkbox", "checkbox"],
|
|
19
|
+
[W, "picture", "picture"],
|
|
20
|
+
[W15, "repeatingSection", "repeating"],
|
|
21
|
+
[W15, "repeatingSectionItem", "repeatingItem"],
|
|
22
|
+
];
|
|
23
|
+
/** What marks a control as something other than an editable value. */
|
|
24
|
+
const OTHER = new Set([
|
|
25
|
+
"docPartObj",
|
|
26
|
+
"docPartList",
|
|
27
|
+
"group",
|
|
28
|
+
"equation",
|
|
29
|
+
"citation",
|
|
30
|
+
"bibliography",
|
|
31
|
+
]);
|
|
32
|
+
const GLOSSARY =
|
|
33
|
+
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument";
|
|
34
|
+
|
|
35
|
+
const on = (value: string | undefined) => value !== "0" && value !== "false" && value !== "off";
|
|
36
|
+
|
|
37
|
+
const glossaries = new WeakMap<DocxPackage, ReadonlyMap<string, string>>();
|
|
38
|
+
|
|
39
|
+
/** The text of the package's glossary entries, by name: what placeholders come back as. */
|
|
40
|
+
function glossaryTexts(pkg: DocxPackage): ReadonlyMap<string, string> {
|
|
41
|
+
const known = glossaries.get(pkg);
|
|
42
|
+
if (known) return known;
|
|
43
|
+
const texts = new Map<string, string>();
|
|
44
|
+
const relationship = pkg
|
|
45
|
+
.relationships(pkg.mainDocument)
|
|
46
|
+
.find((r) => r.type === GLOSSARY && !r.external);
|
|
47
|
+
const part = relationship?.partName ? pkg.part(relationship.partName) : undefined;
|
|
48
|
+
const parts = part && child(pkg.xml(part.name).root, "docParts");
|
|
49
|
+
for (const entry of parts ? elements(parts) : []) {
|
|
50
|
+
const name = attr(child(child(entry, "docPartPr") ?? entry, "name"), "val");
|
|
51
|
+
const body = child(entry, "docPartBody");
|
|
52
|
+
if (name && body) texts.set(name, deepText(body));
|
|
53
|
+
}
|
|
54
|
+
glossaries.set(pkg, texts);
|
|
55
|
+
return texts;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The properties of `sdt`'s `w:sdtPr` and `w:sdtEndPr`, or undefined if the model cannot hold them. */
|
|
59
|
+
export function controlProperties(
|
|
60
|
+
sdt: XmlElement,
|
|
61
|
+
context: Context & { pkg?: DocxPackage },
|
|
62
|
+
): ControlJSON | undefined {
|
|
63
|
+
const pr = child(sdt, "sdtPr");
|
|
64
|
+
const end = child(sdt, "sdtEndPr");
|
|
65
|
+
const props = pr ? elements(pr) : [];
|
|
66
|
+
const find = (ns: string, local: string) => props.find((p) => p.ns === ns && p.local === local);
|
|
67
|
+
const value = (local: string) => attr(find(W, local), "val");
|
|
68
|
+
const typed = KINDS.find(([ns, local]) => find(ns, local));
|
|
69
|
+
const kind: ControlJSON["kind"] = props.some((p) => p.ns === W && OTHER.has(p.local))
|
|
70
|
+
? "other"
|
|
71
|
+
: (typed?.[2] ?? "richText");
|
|
72
|
+
const list = find(W, "dropDownList") ?? find(W, "comboBox");
|
|
73
|
+
const date = find(W, "date");
|
|
74
|
+
const text = find(W, "text");
|
|
75
|
+
const checkbox = find(W14, "checkbox");
|
|
76
|
+
const inBox = (local: string) =>
|
|
77
|
+
checkbox && elements(checkbox).find((c) => c.ns === W14 && c.local === local);
|
|
78
|
+
const glyph = (local: string) => {
|
|
79
|
+
const state = inBox(local);
|
|
80
|
+
const code = attr(state, "val", W14);
|
|
81
|
+
return code === undefined
|
|
82
|
+
? undefined
|
|
83
|
+
: compactDefined({ value: code, font: attr(state, "font", W14) });
|
|
84
|
+
};
|
|
85
|
+
const checked = inBox("checked");
|
|
86
|
+
const placeholder = find(W, "placeholder");
|
|
87
|
+
const showing = find(W, "showingPlcHdr");
|
|
88
|
+
const binding = find(W, "dataBinding");
|
|
89
|
+
const docPart = placeholder && attr(child(placeholder, "docPart"), "val");
|
|
90
|
+
const section = find(W15, "repeatingSection");
|
|
91
|
+
const fixed =
|
|
92
|
+
section && elements(section).find((c) => c.local === "doNotAllowInsertDeleteSection");
|
|
93
|
+
const format = pr ? runProperties(child(pr, "rPr"), context) : [];
|
|
94
|
+
const entry = docPart !== undefined ? context.pkg && glossaryTexts(context.pkg).get(docPart) : "";
|
|
95
|
+
const control = compactDefined({
|
|
96
|
+
kind,
|
|
97
|
+
tag: value("tag"),
|
|
98
|
+
alias: value("alias"),
|
|
99
|
+
lock: value("lock"),
|
|
100
|
+
items: list
|
|
101
|
+
? elements(list)
|
|
102
|
+
.filter((item) => isW(item, "listItem"))
|
|
103
|
+
.map((item) =>
|
|
104
|
+
compactDefined({
|
|
105
|
+
value: attr(item, "value") ?? attr(item, "displayText") ?? "",
|
|
106
|
+
display: attr(item, "displayText"),
|
|
107
|
+
}),
|
|
108
|
+
)
|
|
109
|
+
: undefined,
|
|
110
|
+
dateFormat: date && attr(child(date, "dateFormat"), "val"),
|
|
111
|
+
date: date && attr(date, "fullDate"),
|
|
112
|
+
checked: checked ? on(attr(checked, "val", W14)) : undefined,
|
|
113
|
+
checkedState: glyph("checkedState"),
|
|
114
|
+
uncheckedState: glyph("uncheckedState"),
|
|
115
|
+
multiLine:
|
|
116
|
+
text && attr(text, "multiLine") !== undefined ? on(attr(text, "multiLine")) : undefined,
|
|
117
|
+
format: format.length ? format : undefined,
|
|
118
|
+
noInsertDelete: fixed ? on(attr(fixed, "val") ?? attr(fixed, "val", W15)) : undefined,
|
|
119
|
+
placeholder:
|
|
120
|
+
showing || docPart !== undefined
|
|
121
|
+
? compactDefined({
|
|
122
|
+
showing: showing ? on(attr(showing, "val")) : undefined,
|
|
123
|
+
docPart,
|
|
124
|
+
text: entry || undefined,
|
|
125
|
+
})
|
|
126
|
+
: undefined,
|
|
127
|
+
binding: binding
|
|
128
|
+
? compactDefined({
|
|
129
|
+
xpath: attr(binding, "xpath"),
|
|
130
|
+
storeItemId: attr(binding, "storeItemID"),
|
|
131
|
+
prefixMappings: attr(binding, "prefixMappings"),
|
|
132
|
+
})
|
|
133
|
+
: undefined,
|
|
134
|
+
ext: `<${sdt.name}>${pr ? sourceOf(context, pr) : ""}${end ? sourceOf(context, end) : ""}</${sdt.name}>`,
|
|
135
|
+
}) as unknown as ControlJSON;
|
|
136
|
+
return checkControl(control) ? undefined : control;
|
|
137
|
+
}
|