@folijs/docx 0.0.0-stage → 0.3.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 +29 -0
- package/LICENSE +21 -0
- package/README.md +141 -2
- package/dist/export/content.d.ts +17 -0
- package/dist/export/content.d.ts.map +1 -0
- package/dist/export/content.js +165 -0
- package/dist/export/content.js.map +1 -0
- package/dist/export/index.d.ts +14 -0
- package/dist/export/index.d.ts.map +1 -0
- package/dist/export/index.js +352 -0
- package/dist/export/index.js.map +1 -0
- package/dist/export/parts.d.ts +24 -0
- package/dist/export/parts.d.ts.map +1 -0
- package/dist/export/parts.js +413 -0
- package/dist/export/parts.js.map +1 -0
- package/dist/export/properties.d.ts +39 -0
- package/dist/export/properties.d.ts.map +1 -0
- package/dist/export/properties.js +506 -0
- package/dist/export/properties.js.map +1 -0
- package/dist/export/xmlout.d.ts +63 -0
- package/dist/export/xmlout.d.ts.map +1 -0
- package/dist/export/xmlout.js +194 -0
- package/dist/export/xmlout.js.map +1 -0
- package/dist/import/content.d.ts +35 -0
- package/dist/import/content.d.ts.map +1 -0
- package/dist/import/content.js +370 -0
- package/dist/import/content.js.map +1 -0
- package/dist/import/index.d.ts +26 -0
- package/dist/import/index.d.ts.map +1 -0
- package/dist/import/index.js +96 -0
- package/dist/import/index.js.map +1 -0
- package/dist/import/ooxml.d.ts +38 -0
- package/dist/import/ooxml.d.ts.map +1 -0
- package/dist/import/ooxml.js +70 -0
- package/dist/import/ooxml.js.map +1 -0
- package/dist/import/parts.d.ts +35 -0
- package/dist/import/parts.d.ts.map +1 -0
- package/dist/import/parts.js +365 -0
- package/dist/import/parts.js.map +1 -0
- package/dist/import/properties.d.ts +50 -0
- package/dist/import/properties.d.ts.map +1 -0
- package/dist/import/properties.js +403 -0
- package/dist/import/properties.js.map +1 -0
- package/dist/import/report.d.ts +28 -0
- package/dist/import/report.d.ts.map +1 -0
- package/dist/import/report.js +42 -0
- package/dist/import/report.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/package.d.ts +70 -0
- package/dist/package.d.ts.map +1 -0
- package/dist/package.js +259 -0
- package/dist/package.js.map +1 -0
- package/dist/xml.d.ts +79 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +384 -0
- package/dist/xml.js.map +1 -0
- package/dist/zip.d.ts +66 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +356 -0
- package/dist/zip.js.map +1 -0
- package/package.json +36 -3
- package/src/export/content.ts +229 -0
- package/src/export/index.ts +445 -0
- package/src/export/parts.ts +545 -0
- package/src/export/properties.ts +608 -0
- package/src/export/xmlout.ts +225 -0
- package/src/import/content.ts +413 -0
- package/src/import/index.ts +126 -0
- package/src/import/ooxml.ts +77 -0
- package/src/import/parts.ts +423 -0
- package/src/import/properties.ts +504 -0
- package/src/import/report.ts +64 -0
- package/src/index.ts +24 -0
- package/src/package.ts +328 -0
- package/src/xml.ts +443 -0
- package/src/zip.ts +401 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema v3 attributes and marks back to `w:pPr`, `w:rPr` and `w:sectPr`. An element read from
|
|
3
|
+
* a file comes back verbatim while its values are what import read from it; otherwise each
|
|
4
|
+
* child is decided on its own: one whose property is unchanged is copied from the original
|
|
5
|
+
* (with its unknown attributes), a changed one is written from the model, and children Foli
|
|
6
|
+
* does not model stay where Word requires them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { type MarkJSON, schema } from "@folijs/core";
|
|
10
|
+
import { W } from "../import/ooxml.js";
|
|
11
|
+
import {
|
|
12
|
+
paragraphProperties,
|
|
13
|
+
section as readSection,
|
|
14
|
+
runProperties,
|
|
15
|
+
} from "../import/properties.js";
|
|
16
|
+
import { Recorder } from "../import/report.js";
|
|
17
|
+
import type { XmlElement } from "../xml.js";
|
|
18
|
+
import { attributesOf, el, fragment, merged, type Namespaces, onOff, val } from "./xmlout.js";
|
|
19
|
+
|
|
20
|
+
type Original = { element: XmlElement; source: string } | undefined;
|
|
21
|
+
type Json = Record<string, unknown>;
|
|
22
|
+
|
|
23
|
+
/** Structural equality of JSON values, key order aside. */
|
|
24
|
+
export function same(a: unknown, b: unknown): boolean {
|
|
25
|
+
if (a === b) return true;
|
|
26
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
|
|
27
|
+
return (a ?? undefined) === (b ?? undefined);
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
30
|
+
const ka = Object.keys(a).filter((k) => (a as Json)[k] !== undefined);
|
|
31
|
+
const kb = Object.keys(b).filter((k) => (b as Json)[k] !== undefined);
|
|
32
|
+
return ka.length === kb.length && ka.every((k) => same((a as Json)[k], (b as Json)[k]));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const reading = (original: NonNullable<Original>) => ({
|
|
36
|
+
source: original.source,
|
|
37
|
+
report: new Recorder(),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Canonical paragraph attributes (defaults left out), as the document holds them. */
|
|
41
|
+
function canonicalParagraph(attrs: Json): Json {
|
|
42
|
+
return (schema.nodeFromJSON({ type: "paragraph", attrs }).toJSON().attrs ?? {}) as Json;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Canonical marks, ordered by type: comparable whatever order they were read in. */
|
|
46
|
+
function canonicalMarks(marks: readonly MarkJSON[]): MarkJSON[] {
|
|
47
|
+
return marks
|
|
48
|
+
.map((m) => schema.markFromJSON(m).toJSON())
|
|
49
|
+
.sort((a, b) => (a.type < b.type ? -1 : a.type > b.type ? 1 : 0));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Children of `original` named `locals`, as written, when `unchanged`; otherwise `fresh()`.
|
|
54
|
+
* One property may span several children (`w:b` and `w:bCs`).
|
|
55
|
+
*/
|
|
56
|
+
function child(
|
|
57
|
+
original: Original,
|
|
58
|
+
unchanged: boolean,
|
|
59
|
+
locals: readonly string[],
|
|
60
|
+
fresh: () => string,
|
|
61
|
+
): string {
|
|
62
|
+
if (!original || !unchanged) return fresh();
|
|
63
|
+
let out = "";
|
|
64
|
+
for (const c of original.element.children) {
|
|
65
|
+
if (c.kind === "element" && c.ns === W && locals.includes(c.local))
|
|
66
|
+
out += original.source.slice(c.start, c.end);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function wrap(name: string, original: Original, children: string): string {
|
|
72
|
+
const attributes = attributesOf(original);
|
|
73
|
+
return children || attributes ? `<${name}${attributes}>${children}</${name}>` : "";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---- w:rPr -------------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
/** CT_ParaRPr / CT_RPr child order (ECMA-376 Part 1, 17.3.1.29 and 17.3.2.28). */
|
|
79
|
+
const RPR_ORDER = [
|
|
80
|
+
"ins",
|
|
81
|
+
"del",
|
|
82
|
+
"moveFrom",
|
|
83
|
+
"moveTo",
|
|
84
|
+
"rStyle",
|
|
85
|
+
"rFonts",
|
|
86
|
+
"b",
|
|
87
|
+
"bCs",
|
|
88
|
+
"i",
|
|
89
|
+
"iCs",
|
|
90
|
+
"caps",
|
|
91
|
+
"smallCaps",
|
|
92
|
+
"strike",
|
|
93
|
+
"dstrike",
|
|
94
|
+
"outline",
|
|
95
|
+
"shadow",
|
|
96
|
+
"emboss",
|
|
97
|
+
"imprint",
|
|
98
|
+
"noProof",
|
|
99
|
+
"snapToGrid",
|
|
100
|
+
"vanish",
|
|
101
|
+
"webHidden",
|
|
102
|
+
"color",
|
|
103
|
+
"spacing",
|
|
104
|
+
"w",
|
|
105
|
+
"kern",
|
|
106
|
+
"position",
|
|
107
|
+
"sz",
|
|
108
|
+
"szCs",
|
|
109
|
+
"highlight",
|
|
110
|
+
"u",
|
|
111
|
+
"effect",
|
|
112
|
+
"bdr",
|
|
113
|
+
"shd",
|
|
114
|
+
"fitText",
|
|
115
|
+
"vertAlign",
|
|
116
|
+
"rtl",
|
|
117
|
+
"cs",
|
|
118
|
+
"em",
|
|
119
|
+
"lang",
|
|
120
|
+
"eastAsianLayout",
|
|
121
|
+
"specVanish",
|
|
122
|
+
"oMath",
|
|
123
|
+
"rPrChange",
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
/** Mark type → the children it is written as. */
|
|
127
|
+
const MARK_CHILDREN: Record<string, readonly string[]> = {
|
|
128
|
+
characterStyle: ["rStyle"],
|
|
129
|
+
font: ["rFonts"],
|
|
130
|
+
bold: ["b", "bCs"],
|
|
131
|
+
italic: ["i", "iCs"],
|
|
132
|
+
caps: ["caps"],
|
|
133
|
+
smallCaps: ["smallCaps"],
|
|
134
|
+
strike: ["strike"],
|
|
135
|
+
doubleStrike: ["dstrike"],
|
|
136
|
+
hidden: ["vanish"],
|
|
137
|
+
color: ["color"],
|
|
138
|
+
characterSpacing: ["spacing"],
|
|
139
|
+
fontSize: ["sz", "szCs"],
|
|
140
|
+
highlight: ["highlight"],
|
|
141
|
+
underline: ["u"],
|
|
142
|
+
shading: ["shd"],
|
|
143
|
+
verticalAlign: ["vertAlign"],
|
|
144
|
+
formatChange: ["rPrChange"],
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
function markChildren(m: MarkJSON | undefined, type: string, ns: Namespaces): string {
|
|
148
|
+
if (!m) return "";
|
|
149
|
+
const a = (m.attrs ?? {}) as Json;
|
|
150
|
+
const str = (k: string) => (a[k] === null || a[k] === undefined ? undefined : String(a[k]));
|
|
151
|
+
switch (type) {
|
|
152
|
+
case "characterStyle":
|
|
153
|
+
return val("rStyle", str("id"));
|
|
154
|
+
case "font":
|
|
155
|
+
return el("w:rFonts", {
|
|
156
|
+
"w:ascii": str("ascii"),
|
|
157
|
+
"w:hAnsi": str("hAnsi"),
|
|
158
|
+
"w:eastAsia": str("eastAsia"),
|
|
159
|
+
"w:cs": str("cs"),
|
|
160
|
+
"w:asciiTheme": str("asciiTheme"),
|
|
161
|
+
"w:hAnsiTheme": str("hAnsiTheme"),
|
|
162
|
+
"w:eastAsiaTheme": str("eastAsiaTheme"),
|
|
163
|
+
"w:cstheme": str("csTheme"),
|
|
164
|
+
"w:hint": str("hint"),
|
|
165
|
+
});
|
|
166
|
+
case "bold":
|
|
167
|
+
case "italic": {
|
|
168
|
+
const local = type === "bold" ? "b" : "i";
|
|
169
|
+
const value = a.value === undefined ? true : (a.value as boolean | null);
|
|
170
|
+
return (
|
|
171
|
+
onOff(local, value) + onOff(`${local}Cs`, a.complexScript as boolean | null | undefined)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
case "caps":
|
|
175
|
+
case "smallCaps":
|
|
176
|
+
case "strike":
|
|
177
|
+
case "doubleStrike":
|
|
178
|
+
case "hidden": {
|
|
179
|
+
const local = (MARK_CHILDREN[type] as readonly string[])[0] as string;
|
|
180
|
+
return onOff(local, a.value === undefined ? true : (a.value as boolean));
|
|
181
|
+
}
|
|
182
|
+
case "color":
|
|
183
|
+
return el("w:color", {
|
|
184
|
+
"w:val": str("value"),
|
|
185
|
+
"w:themeColor": str("themeColor"),
|
|
186
|
+
"w:themeTint": str("themeTint"),
|
|
187
|
+
"w:themeShade": str("themeShade"),
|
|
188
|
+
});
|
|
189
|
+
case "characterSpacing":
|
|
190
|
+
return val("spacing", str("value"));
|
|
191
|
+
case "fontSize":
|
|
192
|
+
return val("sz", str("value")) + val("szCs", str("complexScript"));
|
|
193
|
+
case "highlight":
|
|
194
|
+
return val("highlight", str("value"));
|
|
195
|
+
case "underline":
|
|
196
|
+
return el("w:u", { "w:val": str("style") ?? "single", "w:color": str("color") });
|
|
197
|
+
case "shading":
|
|
198
|
+
return el("w:shd", {
|
|
199
|
+
"w:val": str("pattern"),
|
|
200
|
+
"w:color": str("color") ?? "auto",
|
|
201
|
+
"w:fill": str("fill") ?? "auto",
|
|
202
|
+
});
|
|
203
|
+
case "verticalAlign":
|
|
204
|
+
return val("vertAlign", str("value"));
|
|
205
|
+
case "formatChange": {
|
|
206
|
+
const previous = runPropertiesXml(
|
|
207
|
+
(a.previous as MarkJSON[]) ?? [],
|
|
208
|
+
undefined,
|
|
209
|
+
ns,
|
|
210
|
+
"w:rPr",
|
|
211
|
+
true,
|
|
212
|
+
undefined,
|
|
213
|
+
true,
|
|
214
|
+
);
|
|
215
|
+
return el(
|
|
216
|
+
"w:rPrChange",
|
|
217
|
+
{ "w:id": str("id"), "w:author": str("author") ?? "", "w:date": str("date") },
|
|
218
|
+
previous || "<w:rPr/>",
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
default:
|
|
222
|
+
return "";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const RUN_MODELED = new Set(Object.values(MARK_CHILDREN).flat());
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A run's properties as `w:rPr`. `ext` is the `w:rPr` they were read from, if any. `always`
|
|
230
|
+
* writes `<w:rPr/>` even when empty. With `properties` (a style's, a level's, the defaults', a
|
|
231
|
+
* paragraph mark's, a recorded change's previous properties) the model holds no formatting
|
|
232
|
+
* change: a `w:rPrChange` stays as the original has it.
|
|
233
|
+
*/
|
|
234
|
+
export function runPropertiesXml(
|
|
235
|
+
marks: readonly MarkJSON[],
|
|
236
|
+
ext: string | undefined,
|
|
237
|
+
ns: Namespaces,
|
|
238
|
+
name = "w:rPr",
|
|
239
|
+
always = false,
|
|
240
|
+
extra?: { modeled: ReadonlySet<string>; generated: Map<string, string> },
|
|
241
|
+
properties = false,
|
|
242
|
+
): string {
|
|
243
|
+
const original = ext ? fragment(ext, ns, "rPr") : undefined;
|
|
244
|
+
const owned = (type: string) =>
|
|
245
|
+
Object.hasOwn(MARK_CHILDREN, type) && !(properties && type === "formatChange");
|
|
246
|
+
const current = canonicalMarks(marks.filter((m) => owned(m.type)));
|
|
247
|
+
const reread = original
|
|
248
|
+
? canonicalMarks(
|
|
249
|
+
runProperties(original.element, reading(original)).filter((m) => owned(m.type)),
|
|
250
|
+
)
|
|
251
|
+
: [];
|
|
252
|
+
if (original && same(current, reread) && !extra) return ext as string;
|
|
253
|
+
const generated = new Map(extra?.generated);
|
|
254
|
+
for (const [type, locals] of Object.entries(MARK_CHILDREN)) {
|
|
255
|
+
if (!owned(type)) continue;
|
|
256
|
+
const now = current.find((m) => m.type === type);
|
|
257
|
+
const before = reread.find((m) => m.type === type);
|
|
258
|
+
const text = child(original, same(now, before), locals, () => markChildren(now, type, ns));
|
|
259
|
+
generated.set(locals[0] as string, text);
|
|
260
|
+
for (const local of locals.slice(1)) generated.set(local, "");
|
|
261
|
+
}
|
|
262
|
+
const modeled = new Set([...RUN_MODELED, ...(extra?.modeled ?? [])]);
|
|
263
|
+
if (properties) modeled.delete("rPrChange");
|
|
264
|
+
const children = merged(RPR_ORDER, modeled, generated, original);
|
|
265
|
+
return children || attributesOf(original) || always
|
|
266
|
+
? `<${name}${attributesOf(original)}>${children}</${name}>`
|
|
267
|
+
: "";
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ---- w:pPr -------------------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
/** CT_PPr child order (17.3.1.26). */
|
|
273
|
+
const PPR_ORDER = [
|
|
274
|
+
"pStyle",
|
|
275
|
+
"keepNext",
|
|
276
|
+
"keepLines",
|
|
277
|
+
"pageBreakBefore",
|
|
278
|
+
"framePr",
|
|
279
|
+
"widowControl",
|
|
280
|
+
"numPr",
|
|
281
|
+
"suppressLineNumbers",
|
|
282
|
+
"pBdr",
|
|
283
|
+
"shd",
|
|
284
|
+
"tabs",
|
|
285
|
+
"suppressAutoHyphens",
|
|
286
|
+
"kinsoku",
|
|
287
|
+
"wordWrap",
|
|
288
|
+
"overflowPunct",
|
|
289
|
+
"topLinePunct",
|
|
290
|
+
"autoSpaceDE",
|
|
291
|
+
"autoSpaceDN",
|
|
292
|
+
"bidi",
|
|
293
|
+
"adjustRightInd",
|
|
294
|
+
"snapToGrid",
|
|
295
|
+
"spacing",
|
|
296
|
+
"ind",
|
|
297
|
+
"contextualSpacing",
|
|
298
|
+
"mirrorIndents",
|
|
299
|
+
"suppressOverlap",
|
|
300
|
+
"jc",
|
|
301
|
+
"textDirection",
|
|
302
|
+
"textAlignment",
|
|
303
|
+
"textboxTightWrap",
|
|
304
|
+
"outlineLvl",
|
|
305
|
+
"divId",
|
|
306
|
+
"cnfStyle",
|
|
307
|
+
"rPr",
|
|
308
|
+
"sectPr",
|
|
309
|
+
"pPrChange",
|
|
310
|
+
];
|
|
311
|
+
|
|
312
|
+
/** Paragraph attribute → its child (the paragraph mark's `w:rPr` holds two attributes). */
|
|
313
|
+
const PARAGRAPH_CHILDREN: Record<string, string> = {
|
|
314
|
+
style: "pStyle",
|
|
315
|
+
keepNext: "keepNext",
|
|
316
|
+
keepLines: "keepLines",
|
|
317
|
+
pageBreakBefore: "pageBreakBefore",
|
|
318
|
+
widowControl: "widowControl",
|
|
319
|
+
numbering: "numPr",
|
|
320
|
+
borders: "pBdr",
|
|
321
|
+
shading: "shd",
|
|
322
|
+
tabs: "tabs",
|
|
323
|
+
spacing: "spacing",
|
|
324
|
+
indent: "ind",
|
|
325
|
+
contextualSpacing: "contextualSpacing",
|
|
326
|
+
justification: "jc",
|
|
327
|
+
outlineLevel: "outlineLvl",
|
|
328
|
+
section: "sectPr",
|
|
329
|
+
formatChange: "pPrChange",
|
|
330
|
+
};
|
|
331
|
+
const PARAGRAPH_MODELED = new Set([...Object.values(PARAGRAPH_CHILDREN), "rPr"]);
|
|
332
|
+
|
|
333
|
+
const BORDER_SIDES = ["top", "left", "bottom", "right", "between", "bar"];
|
|
334
|
+
|
|
335
|
+
function paragraphChild(name: string, value: unknown, ns: Namespaces): string {
|
|
336
|
+
if (value === undefined || value === null) return "";
|
|
337
|
+
const v = value as Json;
|
|
338
|
+
const s = (x: unknown) => (x === undefined || x === null ? undefined : String(x));
|
|
339
|
+
switch (name) {
|
|
340
|
+
case "style":
|
|
341
|
+
return val("pStyle", s(value));
|
|
342
|
+
case "keepNext":
|
|
343
|
+
case "keepLines":
|
|
344
|
+
case "pageBreakBefore":
|
|
345
|
+
case "widowControl":
|
|
346
|
+
case "contextualSpacing":
|
|
347
|
+
return onOff(name, value as boolean);
|
|
348
|
+
case "numbering":
|
|
349
|
+
return el("w:numPr", {}, val("ilvl", s(v.level)) + val("numId", s(v.id)));
|
|
350
|
+
case "borders":
|
|
351
|
+
return el(
|
|
352
|
+
"w:pBdr",
|
|
353
|
+
{},
|
|
354
|
+
BORDER_SIDES.map((side) => {
|
|
355
|
+
const b = v[side] as Json | undefined;
|
|
356
|
+
return b
|
|
357
|
+
? el(`w:${side}`, {
|
|
358
|
+
"w:val": s(b.style),
|
|
359
|
+
"w:sz": s(b.size),
|
|
360
|
+
"w:space": s(b.space),
|
|
361
|
+
"w:color": s(b.color),
|
|
362
|
+
"w:themeColor": s(b.themeColor),
|
|
363
|
+
})
|
|
364
|
+
: "";
|
|
365
|
+
}).join(""),
|
|
366
|
+
);
|
|
367
|
+
case "shading":
|
|
368
|
+
return el("w:shd", {
|
|
369
|
+
"w:val": s(v.pattern),
|
|
370
|
+
"w:color": s(v.color),
|
|
371
|
+
"w:fill": s(v.fill),
|
|
372
|
+
"w:themeFill": s(v.themeFill),
|
|
373
|
+
});
|
|
374
|
+
case "tabs":
|
|
375
|
+
return el(
|
|
376
|
+
"w:tabs",
|
|
377
|
+
{},
|
|
378
|
+
(value as Json[])
|
|
379
|
+
.map((t) =>
|
|
380
|
+
el("w:tab", { "w:val": s(t.kind), "w:leader": s(t.leader), "w:pos": s(t.position) }),
|
|
381
|
+
)
|
|
382
|
+
.join(""),
|
|
383
|
+
);
|
|
384
|
+
case "spacing":
|
|
385
|
+
return el("w:spacing", {
|
|
386
|
+
"w:before": s(v.before),
|
|
387
|
+
"w:beforeAutospacing": v.beforeAutospacing as boolean | undefined,
|
|
388
|
+
"w:after": s(v.after),
|
|
389
|
+
"w:afterAutospacing": v.afterAutospacing as boolean | undefined,
|
|
390
|
+
"w:line": s(v.line),
|
|
391
|
+
"w:lineRule": s(v.lineRule),
|
|
392
|
+
});
|
|
393
|
+
case "indent":
|
|
394
|
+
return el("w:ind", {
|
|
395
|
+
"w:left": s(v.left),
|
|
396
|
+
"w:right": s(v.right),
|
|
397
|
+
"w:hanging": s(v.hanging),
|
|
398
|
+
"w:firstLine": s(v.firstLine),
|
|
399
|
+
});
|
|
400
|
+
case "justification":
|
|
401
|
+
return val("jc", s(value));
|
|
402
|
+
case "outlineLevel":
|
|
403
|
+
return val("outlineLvl", s(value));
|
|
404
|
+
case "section":
|
|
405
|
+
return sectionXml(v, ns);
|
|
406
|
+
case "formatChange": {
|
|
407
|
+
const previous = paragraphPropertiesXml((v.previous as Json) ?? {}, ns, "w:pPr", true, true);
|
|
408
|
+
return el(
|
|
409
|
+
"w:pPrChange",
|
|
410
|
+
{ "w:id": s(v.id), "w:author": s(v.author) ?? "", "w:date": s(v.date) },
|
|
411
|
+
previous || "<w:pPr/>",
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
default:
|
|
415
|
+
return "";
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** The paragraph mark's `w:rPr`: its tracked insertion or deletion, then its run properties. */
|
|
420
|
+
function markRunProperties(attrs: Json, original: Original, ns: Namespaces): string {
|
|
421
|
+
const rPr = original?.element.children.find(
|
|
422
|
+
(c): c is XmlElement => c.kind === "element" && c.ns === W && c.local === "rPr",
|
|
423
|
+
);
|
|
424
|
+
const ext = rPr && original ? original.source.slice(rPr.start, rPr.end) : undefined;
|
|
425
|
+
const revision = (attrs.revision ?? {}) as Json;
|
|
426
|
+
const change = (local: string, r: Json | undefined) =>
|
|
427
|
+
r
|
|
428
|
+
? el(`w:${local}`, {
|
|
429
|
+
"w:id": String(r.id),
|
|
430
|
+
"w:author": String(r.author ?? ""),
|
|
431
|
+
"w:date": r.date as string | undefined,
|
|
432
|
+
})
|
|
433
|
+
: "";
|
|
434
|
+
const rereadRevision =
|
|
435
|
+
rPr && original
|
|
436
|
+
? (paragraphProperties(original.element, reading(original), true).revision ?? {})
|
|
437
|
+
: {};
|
|
438
|
+
const generated = new Map<string, string>();
|
|
439
|
+
const unchanged = same(attrs.revision ?? {}, rereadRevision);
|
|
440
|
+
if (unchanged && rPr && original) {
|
|
441
|
+
// As written, moves included.
|
|
442
|
+
const sub = { element: rPr, source: original.source };
|
|
443
|
+
for (const local of ["ins", "del", "moveFrom", "moveTo"])
|
|
444
|
+
generated.set(
|
|
445
|
+
local,
|
|
446
|
+
child(sub, true, [local], () => ""),
|
|
447
|
+
);
|
|
448
|
+
} else {
|
|
449
|
+
generated.set("ins", change("ins", revision.insertion as Json | undefined));
|
|
450
|
+
generated.set("del", change("del", revision.deletion as Json | undefined));
|
|
451
|
+
generated.set("moveFrom", "");
|
|
452
|
+
generated.set("moveTo", "");
|
|
453
|
+
}
|
|
454
|
+
return runPropertiesXml(
|
|
455
|
+
(attrs.markFormat as MarkJSON[]) ?? [],
|
|
456
|
+
ext,
|
|
457
|
+
ns,
|
|
458
|
+
"w:rPr",
|
|
459
|
+
false,
|
|
460
|
+
{ modeled: new Set(["ins", "del", "moveFrom", "moveTo"]), generated },
|
|
461
|
+
true,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** What only a paragraph's `w:pPr` has: a style's, a level's, the defaults' keep theirs as written. */
|
|
466
|
+
const PARAGRAPH_ONLY = new Set(["section", "formatChange", "revision", "markFormat"]);
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Paragraph properties as `w:pPr`; empty when there are none. `ext` is the `w:pPr` they were
|
|
470
|
+
* read from. With `properties` (a style's, a level's, the defaults', a recorded change's
|
|
471
|
+
* previous properties) the model does not hold the mark's run properties, a section or a
|
|
472
|
+
* formatting change there: those stay as the original has them.
|
|
473
|
+
*/
|
|
474
|
+
export function paragraphPropertiesXml(
|
|
475
|
+
attrs: Json,
|
|
476
|
+
ns: Namespaces,
|
|
477
|
+
name = "w:pPr",
|
|
478
|
+
always = false,
|
|
479
|
+
properties = false,
|
|
480
|
+
): string {
|
|
481
|
+
const ext = attrs.ext as string | undefined;
|
|
482
|
+
const original = ext ? fragment(ext, ns, "pPr") : undefined;
|
|
483
|
+
const keep = ([k]: [string, unknown]) =>
|
|
484
|
+
k !== "attributes" && !(properties && PARAGRAPH_ONLY.has(k));
|
|
485
|
+
const current = canonicalParagraph(Object.fromEntries(Object.entries(attrs).filter(keep)));
|
|
486
|
+
const reread = original
|
|
487
|
+
? canonicalParagraph(
|
|
488
|
+
Object.fromEntries(
|
|
489
|
+
Object.entries(
|
|
490
|
+
paragraphProperties(original.element, reading(original), !properties),
|
|
491
|
+
).filter(keep),
|
|
492
|
+
),
|
|
493
|
+
)
|
|
494
|
+
: {};
|
|
495
|
+
if (original && same(current, reread)) return ext as string;
|
|
496
|
+
const generated = new Map<string, string>();
|
|
497
|
+
for (const [prop, local] of Object.entries(PARAGRAPH_CHILDREN)) {
|
|
498
|
+
if (properties && PARAGRAPH_ONLY.has(prop)) continue;
|
|
499
|
+
generated.set(
|
|
500
|
+
local,
|
|
501
|
+
child(original, same(current[prop], reread[prop]), [local], () =>
|
|
502
|
+
paragraphChild(prop, current[prop], ns),
|
|
503
|
+
),
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
if (!properties) generated.set("rPr", markRunProperties(current, original, ns));
|
|
507
|
+
const modeled = properties
|
|
508
|
+
? new Set([...PARAGRAPH_MODELED].filter((l) => !["rPr", "sectPr", "pPrChange"].includes(l)))
|
|
509
|
+
: PARAGRAPH_MODELED;
|
|
510
|
+
const children = merged(PPR_ORDER, modeled, generated, original);
|
|
511
|
+
return children || attributesOf(original) || always
|
|
512
|
+
? `<${name}${attributesOf(original)}>${children}</${name}>`
|
|
513
|
+
: "";
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ---- w:sectPr ------------------------------------------------------------------------------
|
|
517
|
+
|
|
518
|
+
/** CT_SectPr child order (17.6.17). */
|
|
519
|
+
const SECT_ORDER = [
|
|
520
|
+
"headerReference",
|
|
521
|
+
"footerReference",
|
|
522
|
+
"footnotePr",
|
|
523
|
+
"endnotePr",
|
|
524
|
+
"type",
|
|
525
|
+
"pgSz",
|
|
526
|
+
"pgMar",
|
|
527
|
+
"paperSrc",
|
|
528
|
+
"pgBorders",
|
|
529
|
+
"lnNumType",
|
|
530
|
+
"pgNumType",
|
|
531
|
+
"cols",
|
|
532
|
+
"formProt",
|
|
533
|
+
"vAlign",
|
|
534
|
+
"noEndnote",
|
|
535
|
+
"titlePg",
|
|
536
|
+
"textDirection",
|
|
537
|
+
"bidi",
|
|
538
|
+
"rtlGutter",
|
|
539
|
+
"docGrid",
|
|
540
|
+
"printerSettings",
|
|
541
|
+
"sectPrChange",
|
|
542
|
+
];
|
|
543
|
+
const SECTION_CHILDREN: Record<string, readonly string[]> = {
|
|
544
|
+
headers: ["headerReference"],
|
|
545
|
+
footers: ["footerReference"],
|
|
546
|
+
type: ["type"],
|
|
547
|
+
pageSize: ["pgSz"],
|
|
548
|
+
margins: ["pgMar"],
|
|
549
|
+
titlePage: ["titlePg"],
|
|
550
|
+
};
|
|
551
|
+
const SECTION_MODELED = new Set(Object.values(SECTION_CHILDREN).flat());
|
|
552
|
+
|
|
553
|
+
function sectionChild(name: string, value: unknown): string {
|
|
554
|
+
if (value === undefined || value === null) return "";
|
|
555
|
+
const v = value as Json;
|
|
556
|
+
const s = (x: unknown) => (x === undefined || x === null ? undefined : String(x));
|
|
557
|
+
const references = (local: string) =>
|
|
558
|
+
Object.entries(v)
|
|
559
|
+
.map(([type, id]) => el(`w:${local}`, { "w:type": type, "r:id": s(id) }))
|
|
560
|
+
.join("");
|
|
561
|
+
switch (name) {
|
|
562
|
+
case "headers":
|
|
563
|
+
return references("headerReference");
|
|
564
|
+
case "footers":
|
|
565
|
+
return references("footerReference");
|
|
566
|
+
case "type":
|
|
567
|
+
return val("type", s(value));
|
|
568
|
+
case "pageSize":
|
|
569
|
+
return el("w:pgSz", { "w:w": s(v.width), "w:h": s(v.height), "w:orient": s(v.orientation) });
|
|
570
|
+
case "margins":
|
|
571
|
+
// CT_PageMar requires all seven: a missing one takes Word's default.
|
|
572
|
+
return el("w:pgMar", {
|
|
573
|
+
"w:top": s(v.top ?? 1440),
|
|
574
|
+
"w:right": s(v.right ?? 1440),
|
|
575
|
+
"w:bottom": s(v.bottom ?? 1440),
|
|
576
|
+
"w:left": s(v.left ?? 1440),
|
|
577
|
+
"w:header": s(v.header ?? 720),
|
|
578
|
+
"w:footer": s(v.footer ?? 720),
|
|
579
|
+
"w:gutter": s(v.gutter ?? 0),
|
|
580
|
+
});
|
|
581
|
+
case "titlePage":
|
|
582
|
+
return onOff("titlePg", value as boolean);
|
|
583
|
+
default:
|
|
584
|
+
return "";
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** A section as `w:sectPr` (always written: an empty one is a section break with Word's defaults). */
|
|
589
|
+
export function sectionXml(section: Json, ns: Namespaces): string {
|
|
590
|
+
const ext = section.ext as string | undefined;
|
|
591
|
+
const original = ext ? fragment(ext, ns, "sectPr") : undefined;
|
|
592
|
+
const reread = original ? readSection(original.element, reading(original)) : {};
|
|
593
|
+
if (original && same({ ...section, ext: undefined }, { ...reread, ext: undefined }))
|
|
594
|
+
return ext as string;
|
|
595
|
+
const generated = new Map<string, string>();
|
|
596
|
+
for (const [prop, locals] of Object.entries(SECTION_CHILDREN)) {
|
|
597
|
+
generated.set(
|
|
598
|
+
locals[0] as string,
|
|
599
|
+
child(original, same(section[prop], reread[prop]), locals, () =>
|
|
600
|
+
sectionChild(prop, section[prop]),
|
|
601
|
+
),
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
const children = merged(SECT_ORDER, SECTION_MODELED, generated, original);
|
|
605
|
+
return `<w:sectPr${attributesOf(original)}>${children}</w:sectPr>`;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
export { wrap };
|