@folijs/docx 0.5.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +42 -14
  3. package/dist/export/balance.d.ts +17 -0
  4. package/dist/export/balance.d.ts.map +1 -0
  5. package/dist/export/balance.js +111 -0
  6. package/dist/export/balance.js.map +1 -0
  7. package/dist/export/content.d.ts +5 -0
  8. package/dist/export/content.d.ts.map +1 -1
  9. package/dist/export/content.js +155 -22
  10. package/dist/export/content.js.map +1 -1
  11. package/dist/export/controls.d.ts +18 -0
  12. package/dist/export/controls.d.ts.map +1 -0
  13. package/dist/export/controls.js +232 -0
  14. package/dist/export/controls.js.map +1 -0
  15. package/dist/export/index.d.ts.map +1 -1
  16. package/dist/export/index.js +214 -18
  17. package/dist/export/index.js.map +1 -1
  18. package/dist/export/parts.d.ts +9 -1
  19. package/dist/export/parts.d.ts.map +1 -1
  20. package/dist/export/parts.js +56 -2
  21. package/dist/export/parts.js.map +1 -1
  22. package/dist/export/pictures.d.ts +3 -0
  23. package/dist/export/pictures.d.ts.map +1 -1
  24. package/dist/export/pictures.js +15 -2
  25. package/dist/export/pictures.js.map +1 -1
  26. package/dist/export/properties.d.ts.map +1 -1
  27. package/dist/export/properties.js +56 -2
  28. package/dist/export/properties.js.map +1 -1
  29. package/dist/import/content.d.ts +11 -1
  30. package/dist/import/content.d.ts.map +1 -1
  31. package/dist/import/content.js +205 -17
  32. package/dist/import/content.js.map +1 -1
  33. package/dist/import/controls.d.ts +14 -0
  34. package/dist/import/controls.d.ts.map +1 -0
  35. package/dist/import/controls.js +123 -0
  36. package/dist/import/controls.js.map +1 -0
  37. package/dist/import/index.d.ts +10 -2
  38. package/dist/import/index.d.ts.map +1 -1
  39. package/dist/import/index.js +73 -12
  40. package/dist/import/index.js.map +1 -1
  41. package/dist/import/ooxml.d.ts +4 -0
  42. package/dist/import/ooxml.d.ts.map +1 -1
  43. package/dist/import/ooxml.js +4 -0
  44. package/dist/import/ooxml.js.map +1 -1
  45. package/dist/import/parts.js +1 -1
  46. package/dist/import/parts.js.map +1 -1
  47. package/dist/import/properties.d.ts +2 -0
  48. package/dist/import/properties.d.ts.map +1 -1
  49. package/dist/import/properties.js +55 -0
  50. package/dist/import/properties.js.map +1 -1
  51. package/dist/import/stories.d.ts +19 -0
  52. package/dist/import/stories.d.ts.map +1 -0
  53. package/dist/import/stories.js +119 -0
  54. package/dist/import/stories.js.map +1 -0
  55. package/dist/settings.d.ts +18 -4
  56. package/dist/settings.d.ts.map +1 -1
  57. package/dist/settings.js +173 -26
  58. package/dist/settings.js.map +1 -1
  59. package/package.json +3 -3
  60. package/src/export/balance.ts +113 -0
  61. package/src/export/content.ts +200 -19
  62. package/src/export/controls.ts +259 -0
  63. package/src/export/index.ts +254 -17
  64. package/src/export/parts.ts +79 -2
  65. package/src/export/pictures.ts +8 -2
  66. package/src/export/properties.ts +68 -2
  67. package/src/import/content.ts +220 -17
  68. package/src/import/controls.ts +137 -0
  69. package/src/import/index.ts +84 -11
  70. package/src/import/ooxml.ts +4 -0
  71. package/src/import/parts.ts +1 -1
  72. package/src/import/properties.ts +59 -0
  73. package/src/import/stories.ts +122 -0
  74. package/src/settings.ts +191 -27
@@ -19,6 +19,13 @@ export function sourceOf(context: Context, element: XmlElement): string {
19
19
  return context.source.slice(element.start, element.end);
20
20
  }
21
21
 
22
+ /** `object` without its undefined values. */
23
+ export function compactDefined<T extends Record<string, unknown>>(object: T): Partial<T> {
24
+ return Object.fromEntries(
25
+ Object.entries(object).filter(([, value]) => value !== undefined),
26
+ ) as Partial<T>;
27
+ }
28
+
22
29
  const ATTRIBUTE_NAME = /^[A-Za-z_][\w.-]*(:[A-Za-z_][\w.-]*)?$/;
23
30
 
24
31
  /**
@@ -458,9 +465,54 @@ export function paragraphProperties(
458
465
  return out;
459
466
  }
460
467
 
468
+ /** `w:footnotePr` or `w:endnotePr` of a section: how its notes are placed and numbered. */
469
+ function noteProperties(element: XmlElement | undefined, context: Context) {
470
+ if (!element) return undefined;
471
+ const value = (local: string) => attr(child(element, local), "val");
472
+ return compact({
473
+ position: value("pos"),
474
+ format: value("numFmt"),
475
+ start: whole(child(element, "numStart"), "val", context),
476
+ restart: value("numRestart"),
477
+ });
478
+ }
479
+
480
+ /** `w:cols`: `widths` from its `w:col` children, when it has them. */
481
+ function sectionColumns(cols: XmlElement | undefined, context: Context) {
482
+ if (!cols) return undefined;
483
+ const on = (local: string) => {
484
+ const value = attr(cols, local);
485
+ return value === undefined ? undefined : !["0", "false", "off"].includes(value);
486
+ };
487
+ const widths = elements(cols)
488
+ .filter((el) => isW(el, "col"))
489
+ .map((el) => compact({ width: whole(el, "w", context), space: whole(el, "space", context) }));
490
+ return compact({
491
+ count: whole(cols, "num", context),
492
+ space: whole(cols, "space", context),
493
+ equalWidth: on("equalWidth"),
494
+ separator: on("sep"),
495
+ widths: widths.length ? widths : undefined,
496
+ });
497
+ }
498
+
499
+ /** `w:sectPrChange`: who changed the section's setup, and the setup before (`w:sectPr`). */
500
+ function sectionChange(element: XmlElement | undefined, context: Context) {
501
+ if (!element) return undefined;
502
+ const { id, author, date } = revisionOf(element);
503
+ const before = child(element, "sectPr");
504
+ return compact({
505
+ id,
506
+ author,
507
+ date: date ?? undefined,
508
+ previous: before ? section(before, context) : {},
509
+ });
510
+ }
511
+
461
512
  /** `w:sectPr` as a section; every part checked on its own. */
462
513
  export function section(sectPr: XmlElement, context: Context): Record<string, unknown> {
463
514
  const pgSz = child(sectPr, "pgSz");
515
+ const pgNumType = child(sectPr, "pgNumType");
464
516
  const pgMar = child(sectPr, "pgMar");
465
517
  const references = (local: string) =>
466
518
  compact(
@@ -494,6 +546,13 @@ export function section(sectPr: XmlElement, context: Context): Record<string, un
494
546
  titlePage: titlePg ? toggle(titlePg) : undefined,
495
547
  headers: references("headerReference"),
496
548
  footers: references("footerReference"),
549
+ columns: sectionColumns(child(sectPr, "cols"), context),
550
+ pageNumbers:
551
+ pgNumType &&
552
+ compact({ start: whole(pgNumType, "start", context), format: attr(pgNumType, "fmt") }),
553
+ footnotes: noteProperties(child(sectPr, "footnotePr"), context),
554
+ endnotes: noteProperties(child(sectPr, "endnotePr"), context),
555
+ change: sectionChange(child(sectPr, "sectPrChange"), context),
497
556
  };
498
557
  const out: Record<string, unknown> = {};
499
558
  for (const [key, value] of Object.entries(parts)) {
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Headers, footers, footnotes and endnotes as stories (ADR-0007): content trees of their own,
3
+ * read from their parts with the same reader as the body. A header's or footer's id is its
4
+ * relationship id; a note's is `footnote:<w:id>` or `endnote:<w:id>`. A note the model cannot
5
+ * name stays in its part, untouched.
6
+ */
7
+ import { parseContent, type StoryJSON } from "@folijs/core";
8
+ import type { DocxPackage } from "../package.js";
9
+ import type { XmlElement } from "../xml.js";
10
+ import { blocks, type ContentContext } from "./content.js";
11
+ import { attr, elements, integer, isW, REL } from "./ooxml.js";
12
+
13
+ const NOTE_TYPES = new Set(["normal", "separator", "continuationSeparator", "continuationNotice"]);
14
+
15
+ /** The notes of a footnotes or endnotes part that the model holds, by story id. */
16
+ export function notesOf(root: XmlElement, kind: "footnote" | "endnote"): Map<string, XmlElement> {
17
+ const out = new Map<string, XmlElement>();
18
+ for (const el of elements(root)) {
19
+ if (!isW(el, kind)) continue;
20
+ const id = integer(attr(el, "id"));
21
+ const type = attr(el, "type");
22
+ const key = `${kind}:${id}`;
23
+ if (id === undefined || (type !== undefined && !NOTE_TYPES.has(type)) || out.has(key)) continue;
24
+ out.set(key, el);
25
+ }
26
+ return out;
27
+ }
28
+
29
+ const docOf = (content: StoryJSON["content"]["content"]): StoryJSON["content"] => ({
30
+ type: "doc",
31
+ content: content?.length ? content : [{ type: "paragraph" }],
32
+ });
33
+
34
+ /** Every story of the package, by id. `contextFor` gives the reading context of a part. */
35
+ export function readStories(
36
+ pkg: DocxPackage,
37
+ contextFor: (part: string, source: string) => ContentContext,
38
+ ): Record<string, StoryJSON> {
39
+ const stories: Record<string, StoryJSON> = {};
40
+ const notesRead = new Set<string>();
41
+ for (const relationship of pkg.relationships(pkg.mainDocument)) {
42
+ const part = relationship.partName ? pkg.part(relationship.partName) : undefined;
43
+ if (relationship.external || !part) continue;
44
+ const headerOrFooter =
45
+ relationship.type === REL.header
46
+ ? ["header", "hdr"]
47
+ : relationship.type === REL.footer
48
+ ? ["footer", "ftr"]
49
+ : undefined;
50
+ const note =
51
+ relationship.type === REL.footnotes
52
+ ? "footnote"
53
+ : relationship.type === REL.endnotes
54
+ ? "endnote"
55
+ : undefined;
56
+ if (!headerOrFooter && !note) continue;
57
+ // A part that is not XML (a relationship can point anywhere) stays as it is.
58
+ let xml: ReturnType<DocxPackage["xml"]>;
59
+ try {
60
+ xml = pkg.xml(part.name);
61
+ } catch {
62
+ continue;
63
+ }
64
+ const context = contextFor(part.name, xml.source);
65
+ if (headerOrFooter) {
66
+ const [kind, root] = headerOrFooter as ["header" | "footer", string];
67
+ // An id the envelope cannot hold as a key: the part stays as it is.
68
+ if (!isW(xml.root, root) || relationship.id === "__proto__") continue;
69
+ stories[relationship.id] = {
70
+ kind,
71
+ content: docOf(blocks(xml.root.children, { ...context, story: kind })),
72
+ };
73
+ } else if (note) {
74
+ // One part per kind, of the right kind: any other stays as it is.
75
+ if (notesRead.has(note) || !isW(xml.root, `${note}s`)) continue;
76
+ notesRead.add(note);
77
+ for (const [key, el] of notesOf(xml.root, note)) {
78
+ const type = attr(el, "type");
79
+ stories[key] = {
80
+ kind: note,
81
+ ...(type ? { noteType: type as StoryJSON["noteType"] & string } : {}),
82
+ content: docOf(blocks(el.children, { ...context, story: note })),
83
+ ext: xml.source.slice(el.start, el.openEnd),
84
+ };
85
+ }
86
+ }
87
+ }
88
+ return stories;
89
+ }
90
+
91
+ /** The ids of every note in the package's notes parts, those the model does not hold too. */
92
+ export function noteIds(pkg: DocxPackage): Set<string> {
93
+ const ids = new Set<string>();
94
+ for (const [type, kind] of [
95
+ [REL.footnotes, "footnote"],
96
+ [REL.endnotes, "endnote"],
97
+ ] as const) {
98
+ const name = pkg.relationships(pkg.mainDocument).find((r) => r.type === type)?.partName;
99
+ if (!name || !pkg.part(name)) continue;
100
+ let root: XmlElement;
101
+ try {
102
+ root = pkg.xml(name).root;
103
+ } catch {
104
+ continue;
105
+ }
106
+ for (const el of elements(root)) {
107
+ const id = integer(attr(el, "id"));
108
+ if (isW(el, kind) && id !== undefined) ids.add(`${kind}:${id}`);
109
+ }
110
+ }
111
+ return ids;
112
+ }
113
+
114
+ /** The stories in the form a validated document holds them, to compare with one. */
115
+ export function canonicalStories(
116
+ pkg: DocxPackage,
117
+ contextFor: (part: string, source: string) => ContentContext,
118
+ ): Record<string, StoryJSON> {
119
+ const stories = readStories(pkg, contextFor);
120
+ for (const story of Object.values(stories)) story.content = parseContent(story.content).toJSON();
121
+ return stories;
122
+ }
package/src/settings.ts CHANGED
@@ -1,22 +1,71 @@
1
1
  /**
2
- * Word's document settings part (`settings.xml`): Foli reads and writes only
3
- * `w:trackRevisions` (whether Word records edits as tracked changes); the rest is kept verbatim.
2
+ * Word's document settings part (`settings.xml`): Foli reads and writes `w:trackRevisions`
3
+ * (whether Word records edits as tracked changes), `w:evenAndOddHeaders` and the note properties
4
+ * of `w:footnotePr` and `w:endnotePr`, and reads `w:doNotUseHTMLParagraphAutoSpacing`; the rest is
5
+ * kept verbatim, and so is the part when none of them changes.
4
6
  */
5
- import { isW, W } from "./import/ooxml.js";
6
- import type { XmlElement } from "./xml.js";
7
+ import type { NotePropertiesJSON, WordSettingsJSON } from "@folijs/core";
8
+ import { child, isW, toggle, W } from "./import/ooxml.js";
9
+ import { parseXml, type XmlElement } from "./xml.js";
7
10
 
8
- /** Whether `settings` (the `w:settings` root) turns tracked changes on. */
9
- export function trackRevisionsOf(settings: XmlElement | undefined): boolean {
10
- const element = settings?.children.find(
11
- (c): c is XmlElement => c.kind === "element" && isW(c, "trackRevisions"),
12
- );
11
+ const elementsOf = (parent: XmlElement) =>
12
+ parent.children.filter((c): c is XmlElement => c.kind === "element");
13
+ const valOf = (element: XmlElement | undefined, name = "val") =>
14
+ element?.attributes.find((a) => a.ns === W && a.local === name)?.value;
15
+
16
+ /** Whether the on-off element `local` of `settings` is on. */
17
+ function isOn(settings: XmlElement | undefined, local: string): boolean {
18
+ const element = settings && elementsOf(settings).find((c) => isW(c, local));
13
19
  if (!element) return false;
14
- const value = element.attributes.find((a) => a.ns === W && a.local === "val")?.value;
20
+ const value = valOf(element);
15
21
  return value === undefined || !["false", "0", "off"].includes(value);
16
22
  }
17
23
 
18
- /** Elements `w:settings` has before `w:trackRevisions` (ECMA-376 CT_Settings, in order). */
19
- const BEFORE = new Set([
24
+ /** Whether `settings` (the `w:settings` root) turns tracked changes on. */
25
+ export const trackRevisionsOf = (settings: XmlElement | undefined): boolean =>
26
+ isOn(settings, "trackRevisions");
27
+
28
+ const POSITIONS = new Set(["pageBottom", "beneathText", "sectEnd", "docEnd"]);
29
+ const RESTARTS = new Set(["continuous", "eachSect", "eachPage"]);
30
+
31
+ /** A `w:footnotePr` or `w:endnotePr` of the settings (its separators stay in the file). */
32
+ function noteProperties(element: XmlElement | undefined): NotePropertiesJSON {
33
+ const value = (local: string) => valOf(element && elementsOf(element).find((c) => isW(c, local)));
34
+ const position = value("pos");
35
+ const format = value("numFmt");
36
+ const start = Number(value("numStart"));
37
+ const restart = value("numRestart");
38
+ return {
39
+ ...(position && POSITIONS.has(position)
40
+ ? { position: position as NotePropertiesJSON["position"] & string }
41
+ : {}),
42
+ ...(format && /^[\w-]+$/.test(format) ? { format } : {}),
43
+ ...(Number.isInteger(start) && start >= 0 && value("numStart") ? { start } : {}),
44
+ ...(restart && RESTARTS.has(restart)
45
+ ? { restart: restart as NotePropertiesJSON["restart"] & string }
46
+ : {}),
47
+ };
48
+ }
49
+
50
+ /** What the document's `settings` model holds of the part, undefined when nothing. */
51
+ export function readSettings(settings: XmlElement | undefined): WordSettingsJSON | undefined {
52
+ if (!settings) return undefined;
53
+ const notes = (local: string) => noteProperties(elementsOf(settings).find((c) => isW(c, local)));
54
+ const footnotes = notes("footnotePr");
55
+ const endnotes = notes("endnotePr");
56
+ const compat = child(settings, "compat");
57
+ const spacing = compat && child(compat, "doNotUseHTMLParagraphAutoSpacing");
58
+ const out: WordSettingsJSON = {
59
+ ...(spacing && toggle(spacing) ? { doNotUseHTMLParagraphAutoSpacing: true } : {}),
60
+ ...(isOn(settings, "evenAndOddHeaders") ? { evenAndOddHeaders: true } : {}),
61
+ ...(Object.keys(footnotes).length ? { footnotes } : {}),
62
+ ...(Object.keys(endnotes).length ? { endnotes } : {}),
63
+ };
64
+ return Object.keys(out).length ? out : undefined;
65
+ }
66
+
67
+ /** `w:settings`' children in their order (ECMA-376 CT_Settings), as far as Foli writes them. */
68
+ const ORDER = [
20
69
  "writeProtection",
21
70
  "view",
22
71
  "zoom",
@@ -48,19 +97,65 @@ const BEFORE = new Set([
48
97
  "documentType",
49
98
  "mailMerge",
50
99
  "revisionView",
51
- ]);
100
+ "trackRevisions",
101
+ "doNotTrackMoves",
102
+ "doNotTrackFormatting",
103
+ "documentProtection",
104
+ "autoFormatOverride",
105
+ "styleLockTheme",
106
+ "styleLockQFSet",
107
+ "defaultTabStop",
108
+ "autoHyphenation",
109
+ "consecutiveHyphenLimit",
110
+ "hyphenationZone",
111
+ "doNotHyphenateCaps",
112
+ "showEnvelope",
113
+ "summaryLength",
114
+ "clickAndTypeStyle",
115
+ "defaultTableStyle",
116
+ "evenAndOddHeaders",
117
+ "bookFoldRevPrinting",
118
+ "bookFoldPrinting",
119
+ "bookFoldPrintingSheets",
120
+ "drawingGridHorizontalSpacing",
121
+ "drawingGridVerticalSpacing",
122
+ "displayHorizontalDrawingGridEvery",
123
+ "displayVerticalDrawingGridEvery",
124
+ "doNotUseMarginsForDrawingGridOrigin",
125
+ "drawingGridHorizontalOrigin",
126
+ "drawingGridVerticalOrigin",
127
+ "doNotShadeFormData",
128
+ "noPunctuationKerning",
129
+ "characterSpacingControl",
130
+ "printTwoOnOne",
131
+ "strictFirstAndLastChars",
132
+ "noLineBreaksAfter",
133
+ "noLineBreaksBefore",
134
+ "savePreviewPicture",
135
+ "doNotValidateAgainstSchema",
136
+ "saveInvalidXml",
137
+ "ignoreMixedContent",
138
+ "alwaysShowPlaceholderText",
139
+ "doNotDemarcateInvalidXml",
140
+ "saveXmlDataOnly",
141
+ "useXSLTWhenSaving",
142
+ "saveThroughXslt",
143
+ "showXMLTags",
144
+ "alwaysMergeEmptyNamespace",
145
+ "updateFields",
146
+ "hdrShapeDefaults",
147
+ "footnotePr",
148
+ "endnotePr",
149
+ ];
52
150
 
53
- /** `source` (a settings part, `root` its `w:settings`) with tracked changes `on`. */
54
- export function withTrackRevisions(source: string, root: XmlElement, on: boolean): string {
55
- if (trackRevisionsOf(root) === on) return source;
56
- const children = root.children.filter((c): c is XmlElement => c.kind === "element");
57
- const existing = children.find((c) => isW(c, "trackRevisions"));
58
- const prefix = root.prefix ? `${root.prefix}:` : "";
59
- const element = `<${prefix}trackRevisions/>`;
60
- // Off: removed. On over an explicit "off": replaced.
61
- if (existing)
62
- return source.slice(0, existing.start) + (on ? element : "") + source.slice(existing.end);
63
- const after = children.filter((c) => c.ns === W && BEFORE.has(c.local)).at(-1);
151
+ /** `source` with the child `local` of `root` being `element` ("": none), in its place. */
152
+ function placed(source: string, root: XmlElement, local: string, element: string): string {
153
+ const children = elementsOf(root);
154
+ const existing = children.find((c) => isW(c, local));
155
+ if (existing) return source.slice(0, existing.start) + element + source.slice(existing.end);
156
+ if (!element) return source;
157
+ const before = new Set(ORDER.slice(0, ORDER.indexOf(local)));
158
+ const after = children.filter((c) => c.ns === W && before.has(c.local)).at(-1);
64
159
  if (after) return source.slice(0, after.end) + element + source.slice(after.end);
65
160
  if (root.openEnd === root.end) {
66
161
  // `<w:settings/>`: opened and closed.
@@ -69,7 +164,76 @@ export function withTrackRevisions(source: string, root: XmlElement, on: boolean
69
164
  return source.slice(0, root.openEnd) + element + source.slice(root.openEnd);
70
165
  }
71
166
 
72
- /** A new settings part with tracked changes on. */
73
- export function newSettingsXml(): string {
74
- return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:settings xmlns:w="${W}"><w:trackRevisions/></w:settings>`;
167
+ const prefixOf = (root: XmlElement) => (root.prefix ? `${root.prefix}:` : "");
168
+
169
+ /** `source` (a settings part, `root` its `w:settings`) with tracked changes `on`. */
170
+ export function withTrackRevisions(source: string, root: XmlElement, on: boolean): string {
171
+ if (trackRevisionsOf(root) === on) return source;
172
+ return placed(source, root, "trackRevisions", on ? `<${prefixOf(root)}trackRevisions/>` : "");
173
+ }
174
+
175
+ /** A note properties element: `kept` (its separators, verbatim) after the properties. */
176
+ function notesXml(prefix: string, local: string, v: NotePropertiesJSON, kept = ""): string {
177
+ const value = (name: string, x: string | number | undefined) =>
178
+ x === undefined ? "" : `<${prefix}${name} ${prefix}val="${x}"/>`;
179
+ const children =
180
+ value("pos", v.position) +
181
+ value("numFmt", v.format) +
182
+ value("numStart", v.start) +
183
+ value("numRestart", v.restart) +
184
+ kept;
185
+ return children ? `<${prefix}${local}>${children}</${prefix}${local}>` : "";
186
+ }
187
+
188
+ const NOTES = [
189
+ ["footnotes", "footnotePr", "footnote"],
190
+ ["endnotes", "endnotePr", "endnote"],
191
+ ] as const;
192
+
193
+ /** The same properties, whatever their order. */
194
+ const sameNotes = (a: NotePropertiesJSON, b: NotePropertiesJSON) =>
195
+ a.position === b.position &&
196
+ a.format === b.format &&
197
+ a.start === b.start &&
198
+ a.restart === b.restart;
199
+
200
+ /**
201
+ * `source` saying what `settings` says. What they leave out stays as the file has it, and a part
202
+ * that already says it is returned as it is.
203
+ */
204
+ export function withSettings(source: string, root: XmlElement, settings: WordSettingsJSON): string {
205
+ let text = source;
206
+ let current = root;
207
+ const put = (local: string, element: string) => {
208
+ text = placed(text, current, local, element);
209
+ current = parseXml(text).root;
210
+ };
211
+ const prefix = prefixOf(root);
212
+ const even = settings.evenAndOddHeaders;
213
+ if (even !== undefined && even !== isOn(current, "evenAndOddHeaders"))
214
+ put("evenAndOddHeaders", even ? `<${prefix}evenAndOddHeaders/>` : "");
215
+ for (const [key, local, separator] of NOTES) {
216
+ const wanted = settings[key];
217
+ if (!wanted) continue;
218
+ const element = elementsOf(current).find((c) => isW(c, local));
219
+ if (sameNotes(wanted, noteProperties(element))) continue;
220
+ const kept = (element ? elementsOf(element) : [])
221
+ .filter((c) => isW(c, separator))
222
+ .map((c) => text.slice(c.start, c.end))
223
+ .join("");
224
+ put(local, notesXml(prefix, local, wanted, kept));
225
+ }
226
+ return text;
227
+ }
228
+
229
+ /** A new settings part. */
230
+ export function newSettingsXml(trackRevisions: boolean, settings: WordSettingsJSON = {}): string {
231
+ const children =
232
+ (trackRevisions ? "<w:trackRevisions/>" : "") +
233
+ (settings.evenAndOddHeaders ? "<w:evenAndOddHeaders/>" : "") +
234
+ NOTES.map(([key, local]) => notesXml("w:", local, settings[key] ?? {})).join("") +
235
+ (settings.doNotUseHTMLParagraphAutoSpacing
236
+ ? "<w:compat><w:doNotUseHTMLParagraphAutoSpacing/></w:compat>"
237
+ : "");
238
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:settings xmlns:w="${W}">${children}</w:settings>`;
75
239
  }