@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/dist/settings.js
CHANGED
|
@@ -1,18 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const element = settings?.children.find((c) => c.kind === "element" && isW(c, "trackRevisions"));
|
|
1
|
+
import { child, isW, toggle, W } from "./import/ooxml.js";
|
|
2
|
+
import { parseXml } from "./xml.js";
|
|
3
|
+
const elementsOf = (parent) => parent.children.filter((c) => c.kind === "element");
|
|
4
|
+
const valOf = (element, name = "val") => element?.attributes.find((a) => a.ns === W && a.local === name)?.value;
|
|
5
|
+
/** Whether the on-off element `local` of `settings` is on. */
|
|
6
|
+
function isOn(settings, local) {
|
|
7
|
+
const element = settings && elementsOf(settings).find((c) => isW(c, local));
|
|
9
8
|
if (!element)
|
|
10
9
|
return false;
|
|
11
|
-
const value = element
|
|
10
|
+
const value = valOf(element);
|
|
12
11
|
return value === undefined || !["false", "0", "off"].includes(value);
|
|
13
12
|
}
|
|
14
|
-
/**
|
|
15
|
-
const
|
|
13
|
+
/** Whether `settings` (the `w:settings` root) turns tracked changes on. */
|
|
14
|
+
export const trackRevisionsOf = (settings) => isOn(settings, "trackRevisions");
|
|
15
|
+
const POSITIONS = new Set(["pageBottom", "beneathText", "sectEnd", "docEnd"]);
|
|
16
|
+
const RESTARTS = new Set(["continuous", "eachSect", "eachPage"]);
|
|
17
|
+
/** A `w:footnotePr` or `w:endnotePr` of the settings (its separators stay in the file). */
|
|
18
|
+
function noteProperties(element) {
|
|
19
|
+
const value = (local) => valOf(element && elementsOf(element).find((c) => isW(c, local)));
|
|
20
|
+
const position = value("pos");
|
|
21
|
+
const format = value("numFmt");
|
|
22
|
+
const start = Number(value("numStart"));
|
|
23
|
+
const restart = value("numRestart");
|
|
24
|
+
return {
|
|
25
|
+
...(position && POSITIONS.has(position)
|
|
26
|
+
? { position: position }
|
|
27
|
+
: {}),
|
|
28
|
+
...(format && /^[\w-]+$/.test(format) ? { format } : {}),
|
|
29
|
+
...(Number.isInteger(start) && start >= 0 && value("numStart") ? { start } : {}),
|
|
30
|
+
...(restart && RESTARTS.has(restart)
|
|
31
|
+
? { restart: restart }
|
|
32
|
+
: {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** What the document's `settings` model holds of the part, undefined when nothing. */
|
|
36
|
+
export function readSettings(settings) {
|
|
37
|
+
if (!settings)
|
|
38
|
+
return undefined;
|
|
39
|
+
const notes = (local) => noteProperties(elementsOf(settings).find((c) => isW(c, local)));
|
|
40
|
+
const footnotes = notes("footnotePr");
|
|
41
|
+
const endnotes = notes("endnotePr");
|
|
42
|
+
const compat = child(settings, "compat");
|
|
43
|
+
const spacing = compat && child(compat, "doNotUseHTMLParagraphAutoSpacing");
|
|
44
|
+
const out = {
|
|
45
|
+
...(spacing && toggle(spacing) ? { doNotUseHTMLParagraphAutoSpacing: true } : {}),
|
|
46
|
+
...(isOn(settings, "evenAndOddHeaders") ? { evenAndOddHeaders: true } : {}),
|
|
47
|
+
...(Object.keys(footnotes).length ? { footnotes } : {}),
|
|
48
|
+
...(Object.keys(endnotes).length ? { endnotes } : {}),
|
|
49
|
+
};
|
|
50
|
+
return Object.keys(out).length ? out : undefined;
|
|
51
|
+
}
|
|
52
|
+
/** `w:settings`' children in their order (ECMA-376 CT_Settings), as far as Foli writes them. */
|
|
53
|
+
const ORDER = [
|
|
16
54
|
"writeProtection",
|
|
17
55
|
"view",
|
|
18
56
|
"zoom",
|
|
@@ -44,19 +82,66 @@ const BEFORE = new Set([
|
|
|
44
82
|
"documentType",
|
|
45
83
|
"mailMerge",
|
|
46
84
|
"revisionView",
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
85
|
+
"trackRevisions",
|
|
86
|
+
"doNotTrackMoves",
|
|
87
|
+
"doNotTrackFormatting",
|
|
88
|
+
"documentProtection",
|
|
89
|
+
"autoFormatOverride",
|
|
90
|
+
"styleLockTheme",
|
|
91
|
+
"styleLockQFSet",
|
|
92
|
+
"defaultTabStop",
|
|
93
|
+
"autoHyphenation",
|
|
94
|
+
"consecutiveHyphenLimit",
|
|
95
|
+
"hyphenationZone",
|
|
96
|
+
"doNotHyphenateCaps",
|
|
97
|
+
"showEnvelope",
|
|
98
|
+
"summaryLength",
|
|
99
|
+
"clickAndTypeStyle",
|
|
100
|
+
"defaultTableStyle",
|
|
101
|
+
"evenAndOddHeaders",
|
|
102
|
+
"bookFoldRevPrinting",
|
|
103
|
+
"bookFoldPrinting",
|
|
104
|
+
"bookFoldPrintingSheets",
|
|
105
|
+
"drawingGridHorizontalSpacing",
|
|
106
|
+
"drawingGridVerticalSpacing",
|
|
107
|
+
"displayHorizontalDrawingGridEvery",
|
|
108
|
+
"displayVerticalDrawingGridEvery",
|
|
109
|
+
"doNotUseMarginsForDrawingGridOrigin",
|
|
110
|
+
"drawingGridHorizontalOrigin",
|
|
111
|
+
"drawingGridVerticalOrigin",
|
|
112
|
+
"doNotShadeFormData",
|
|
113
|
+
"noPunctuationKerning",
|
|
114
|
+
"characterSpacingControl",
|
|
115
|
+
"printTwoOnOne",
|
|
116
|
+
"strictFirstAndLastChars",
|
|
117
|
+
"noLineBreaksAfter",
|
|
118
|
+
"noLineBreaksBefore",
|
|
119
|
+
"savePreviewPicture",
|
|
120
|
+
"doNotValidateAgainstSchema",
|
|
121
|
+
"saveInvalidXml",
|
|
122
|
+
"ignoreMixedContent",
|
|
123
|
+
"alwaysShowPlaceholderText",
|
|
124
|
+
"doNotDemarcateInvalidXml",
|
|
125
|
+
"saveXmlDataOnly",
|
|
126
|
+
"useXSLTWhenSaving",
|
|
127
|
+
"saveThroughXslt",
|
|
128
|
+
"showXMLTags",
|
|
129
|
+
"alwaysMergeEmptyNamespace",
|
|
130
|
+
"updateFields",
|
|
131
|
+
"hdrShapeDefaults",
|
|
132
|
+
"footnotePr",
|
|
133
|
+
"endnotePr",
|
|
134
|
+
];
|
|
135
|
+
/** `source` with the child `local` of `root` being `element` ("": none), in its place. */
|
|
136
|
+
function placed(source, root, local, element) {
|
|
137
|
+
const children = elementsOf(root);
|
|
138
|
+
const existing = children.find((c) => isW(c, local));
|
|
57
139
|
if (existing)
|
|
58
|
-
return source.slice(0, existing.start) +
|
|
59
|
-
|
|
140
|
+
return source.slice(0, existing.start) + element + source.slice(existing.end);
|
|
141
|
+
if (!element)
|
|
142
|
+
return source;
|
|
143
|
+
const before = new Set(ORDER.slice(0, ORDER.indexOf(local)));
|
|
144
|
+
const after = children.filter((c) => c.ns === W && before.has(c.local)).at(-1);
|
|
60
145
|
if (after)
|
|
61
146
|
return source.slice(0, after.end) + element + source.slice(after.end);
|
|
62
147
|
if (root.openEnd === root.end) {
|
|
@@ -65,8 +150,70 @@ export function withTrackRevisions(source, root, on) {
|
|
|
65
150
|
}
|
|
66
151
|
return source.slice(0, root.openEnd) + element + source.slice(root.openEnd);
|
|
67
152
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
153
|
+
const prefixOf = (root) => (root.prefix ? `${root.prefix}:` : "");
|
|
154
|
+
/** `source` (a settings part, `root` its `w:settings`) with tracked changes `on`. */
|
|
155
|
+
export function withTrackRevisions(source, root, on) {
|
|
156
|
+
if (trackRevisionsOf(root) === on)
|
|
157
|
+
return source;
|
|
158
|
+
return placed(source, root, "trackRevisions", on ? `<${prefixOf(root)}trackRevisions/>` : "");
|
|
159
|
+
}
|
|
160
|
+
/** A note properties element: `kept` (its separators, verbatim) after the properties. */
|
|
161
|
+
function notesXml(prefix, local, v, kept = "") {
|
|
162
|
+
const value = (name, x) => x === undefined ? "" : `<${prefix}${name} ${prefix}val="${x}"/>`;
|
|
163
|
+
const children = value("pos", v.position) +
|
|
164
|
+
value("numFmt", v.format) +
|
|
165
|
+
value("numStart", v.start) +
|
|
166
|
+
value("numRestart", v.restart) +
|
|
167
|
+
kept;
|
|
168
|
+
return children ? `<${prefix}${local}>${children}</${prefix}${local}>` : "";
|
|
169
|
+
}
|
|
170
|
+
const NOTES = [
|
|
171
|
+
["footnotes", "footnotePr", "footnote"],
|
|
172
|
+
["endnotes", "endnotePr", "endnote"],
|
|
173
|
+
];
|
|
174
|
+
/** The same properties, whatever their order. */
|
|
175
|
+
const sameNotes = (a, b) => a.position === b.position &&
|
|
176
|
+
a.format === b.format &&
|
|
177
|
+
a.start === b.start &&
|
|
178
|
+
a.restart === b.restart;
|
|
179
|
+
/**
|
|
180
|
+
* `source` saying what `settings` says. What they leave out stays as the file has it, and a part
|
|
181
|
+
* that already says it is returned as it is.
|
|
182
|
+
*/
|
|
183
|
+
export function withSettings(source, root, settings) {
|
|
184
|
+
let text = source;
|
|
185
|
+
let current = root;
|
|
186
|
+
const put = (local, element) => {
|
|
187
|
+
text = placed(text, current, local, element);
|
|
188
|
+
current = parseXml(text).root;
|
|
189
|
+
};
|
|
190
|
+
const prefix = prefixOf(root);
|
|
191
|
+
const even = settings.evenAndOddHeaders;
|
|
192
|
+
if (even !== undefined && even !== isOn(current, "evenAndOddHeaders"))
|
|
193
|
+
put("evenAndOddHeaders", even ? `<${prefix}evenAndOddHeaders/>` : "");
|
|
194
|
+
for (const [key, local, separator] of NOTES) {
|
|
195
|
+
const wanted = settings[key];
|
|
196
|
+
if (!wanted)
|
|
197
|
+
continue;
|
|
198
|
+
const element = elementsOf(current).find((c) => isW(c, local));
|
|
199
|
+
if (sameNotes(wanted, noteProperties(element)))
|
|
200
|
+
continue;
|
|
201
|
+
const kept = (element ? elementsOf(element) : [])
|
|
202
|
+
.filter((c) => isW(c, separator))
|
|
203
|
+
.map((c) => text.slice(c.start, c.end))
|
|
204
|
+
.join("");
|
|
205
|
+
put(local, notesXml(prefix, local, wanted, kept));
|
|
206
|
+
}
|
|
207
|
+
return text;
|
|
208
|
+
}
|
|
209
|
+
/** A new settings part. */
|
|
210
|
+
export function newSettingsXml(trackRevisions, settings = {}) {
|
|
211
|
+
const children = (trackRevisions ? "<w:trackRevisions/>" : "") +
|
|
212
|
+
(settings.evenAndOddHeaders ? "<w:evenAndOddHeaders/>" : "") +
|
|
213
|
+
NOTES.map(([key, local]) => notesXml("w:", local, settings[key] ?? {})).join("") +
|
|
214
|
+
(settings.doNotUseHTMLParagraphAutoSpacing
|
|
215
|
+
? "<w:compat><w:doNotUseHTMLParagraphAutoSpacing/></w:compat>"
|
|
216
|
+
: "");
|
|
217
|
+
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:settings xmlns:w="${W}">${children}</w:settings>`;
|
|
71
218
|
}
|
|
72
219
|
//# sourceMappingURL=settings.js.map
|
package/dist/settings.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"settings.js","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"settings.js","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAmB,MAAM,UAAU,CAAC;AAErD,MAAM,UAAU,GAAG,CAAC,MAAkB,EAAE,EAAE,CACxC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAmB,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AACvE,MAAM,KAAK,GAAG,CAAC,OAA+B,EAAE,IAAI,GAAG,KAAK,EAAE,EAAE,CAC9D,OAAO,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,KA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AAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;aAChC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;aACtC,IAAI,CAAC,EAAE,CAAC,CAAC;QACZ,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,2BAA2B;AAC3B,MAAM,UAAU,cAAc,CAAC,cAAuB,EAAE,QAAQ,GAAqB,EAAE;IACrF,MAAM,QAAQ,GACZ,CAAC,cAAc,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7C,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;QAChF,CAAC,QAAQ,CAAC,gCAAgC;YACxC,CAAC,CAAC,4DAA4D;YAC9D,CAAC,CAAC,EAAE,CAAC,CAAC;IACV,OAAO,mFAAmF,CAAC,KAAK,QAAQ,eAAe,CAAC;AAC1H,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@folijs/docx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DOCX for the Foli document engine: import to Foli's Word model (schema
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "DOCX for the Foli document engine: import to Foli's Word model (schema v5) and lossless export, the package (zip, parts, relationships) and an XML reader that keeps the exact source text. No DOM.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"!src/testing"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@folijs/core": "^0.
|
|
29
|
+
"@folijs/core": "^0.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "^20.19.43",
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fields and inline content controls as an edit can leave them, made writable (ADR-0007). A field
|
|
3
|
+
* part without its partner is dropped, with the code of a field that lost its end, so the code
|
|
4
|
+
* never becomes text; the result stays. An inline control a paragraph split runs over becomes one
|
|
5
|
+
* control per paragraph, as `w:sdt` cannot cross a paragraph; ends without a partner are dropped
|
|
6
|
+
* when written.
|
|
7
|
+
*/
|
|
8
|
+
import type { FoliDocument, NodeJSON } from "@folijs/core";
|
|
9
|
+
import type { DocxPackage } from "../package.js";
|
|
10
|
+
|
|
11
|
+
function paragraphsOf(nodes: readonly NodeJSON[], out: NodeJSON[] = []): NodeJSON[] {
|
|
12
|
+
for (const node of nodes) {
|
|
13
|
+
if (node.type === "paragraph") out.push(node);
|
|
14
|
+
else if (node.content) paragraphsOf(node.content, out);
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function balanceFields(paragraphs: readonly NodeJSON[]): void {
|
|
20
|
+
const drop = new Set<NodeJSON>();
|
|
21
|
+
const open: NodeJSON[][] = [];
|
|
22
|
+
for (const paragraph of paragraphs) {
|
|
23
|
+
for (const node of paragraph.content ?? []) {
|
|
24
|
+
if (node.type === "fieldStart") open.push([node]);
|
|
25
|
+
else if (node.type === "fieldCode" || node.type === "fieldSeparator") {
|
|
26
|
+
const field = open.at(-1);
|
|
27
|
+
if (field) field.push(node);
|
|
28
|
+
else drop.add(node);
|
|
29
|
+
} else if (node.type === "fieldEnd" && !open.pop()) drop.add(node);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const field of open) for (const node of field) drop.add(node);
|
|
33
|
+
if (!drop.size) return;
|
|
34
|
+
for (const paragraph of paragraphs)
|
|
35
|
+
if (paragraph.content) paragraph.content = paragraph.content.filter((n) => !drop.has(n));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function balanceControls(paragraphs: readonly NodeJSON[]): void {
|
|
39
|
+
const endsLeft = new Map<unknown, number>();
|
|
40
|
+
const ends = (paragraph: NodeJSON) =>
|
|
41
|
+
(paragraph.content ?? []).filter((n) => n.type === "controlEnd");
|
|
42
|
+
for (const paragraph of paragraphs)
|
|
43
|
+
for (const end of ends(paragraph))
|
|
44
|
+
endsLeft.set(end.attrs?.id, (endsLeft.get(end.attrs?.id) ?? 0) + 1);
|
|
45
|
+
if (!endsLeft.size) return;
|
|
46
|
+
let open: unknown[] = [];
|
|
47
|
+
for (const paragraph of paragraphs) {
|
|
48
|
+
const content: NodeJSON[] = open.map(
|
|
49
|
+
(id) => ({ type: "controlStart", attrs: { id } }) as NodeJSON,
|
|
50
|
+
);
|
|
51
|
+
const here = [...open];
|
|
52
|
+
for (const node of paragraph.content ?? []) {
|
|
53
|
+
content.push(node);
|
|
54
|
+
const id = node.attrs?.id;
|
|
55
|
+
if (node.type === "controlStart") here.push(id);
|
|
56
|
+
else if (node.type === "controlEnd") {
|
|
57
|
+
endsLeft.set(id, (endsLeft.get(id) as number) - 1);
|
|
58
|
+
const at = here.lastIndexOf(id);
|
|
59
|
+
if (at >= 0) here.splice(at, 1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Innermost first; a control with no end further on has lost it, and its start is dropped.
|
|
63
|
+
open = here.filter((id) => (endsLeft.get(id) ?? 0) > 0);
|
|
64
|
+
for (const id of [...open].reverse())
|
|
65
|
+
content.push({ type: "controlEnd", attrs: { id } } as NodeJSON);
|
|
66
|
+
if (content.length) paragraph.content = content;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether the field characters of a part's XML open and close properly. */
|
|
71
|
+
function fieldsWhole(xml: string): boolean {
|
|
72
|
+
let depth = 0;
|
|
73
|
+
for (const [, type] of xml.matchAll(/fldCharType\s*=\s*["'](begin|separate|end)["']/g)) {
|
|
74
|
+
if (type === "begin") depth++;
|
|
75
|
+
else if (depth === 0) return false;
|
|
76
|
+
else if (type === "end") depth--;
|
|
77
|
+
}
|
|
78
|
+
return depth === 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether content kept verbatim among `nodes` holds field characters. */
|
|
82
|
+
function holdsVerbatimField(nodes: readonly NodeJSON[]): boolean {
|
|
83
|
+
return nodes.some(
|
|
84
|
+
(node) =>
|
|
85
|
+
(typeof node.attrs?.xml === "string" && node.attrs.xml.includes("fldChar")) ||
|
|
86
|
+
holdsVerbatimField(node.content ?? []),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `doc` (a validated copy, changed in place) with its fields and inline controls writable. Field
|
|
92
|
+
* parts are only repaired where an edit can be what broke them: not in a package holding broken
|
|
93
|
+
* fields of its own (Word reads those in its way), nor where verbatim content holds field
|
|
94
|
+
* characters, whose partners the model cannot see.
|
|
95
|
+
*/
|
|
96
|
+
export function balanced(doc: FoliDocument, original?: DocxPackage): FoliDocument {
|
|
97
|
+
const packageWhole =
|
|
98
|
+
!original ||
|
|
99
|
+
original.parts
|
|
100
|
+
.filter((part) => /^\/word\/[^/]+\.xml$/.test(part.name))
|
|
101
|
+
.every((part) => fieldsWhole(original.text(part.name)));
|
|
102
|
+
const trees = [
|
|
103
|
+
doc.content.content ?? [],
|
|
104
|
+
...doc.comments.map((c) => c.content),
|
|
105
|
+
...Object.values(doc.stories ?? {}).map((s) => s.content.content ?? []),
|
|
106
|
+
];
|
|
107
|
+
for (const tree of trees) {
|
|
108
|
+
const paragraphs = paragraphsOf(tree);
|
|
109
|
+
if (packageWhole && !holdsVerbatimField(tree)) balanceFields(paragraphs);
|
|
110
|
+
balanceControls(paragraphs);
|
|
111
|
+
}
|
|
112
|
+
return doc;
|
|
113
|
+
}
|