@portone/docx-editor 0.2.0 → 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.
Files changed (256) hide show
  1. package/CHANGELOG.md +174 -0
  2. package/CONTRIBUTING.md +9 -1
  3. package/dist/DocxEditor.d.ts +6 -0
  4. package/dist/DocxEditor.js +12 -29
  5. package/dist/core.d.ts +23 -8
  6. package/dist/core.js +9 -4
  7. package/dist/docx/cloning.d.ts +38 -0
  8. package/dist/docx/cloning.js +74 -0
  9. package/dist/docx/commentOnlyChange.d.ts +14 -7
  10. package/dist/docx/commentOnlyChange.js +9 -139
  11. package/dist/docx/comments/constants.d.ts +3 -4
  12. package/dist/docx/comments/constants.js +4 -5
  13. package/dist/docx/comments/grammar.d.ts +86 -0
  14. package/dist/docx/comments/grammar.js +223 -0
  15. package/dist/docx/comments/parts.d.ts +52 -0
  16. package/dist/docx/comments/parts.js +279 -0
  17. package/dist/docx/comments/people.d.ts +11 -2
  18. package/dist/docx/comments/people.js +28 -106
  19. package/dist/docx/comments/policy.d.ts +7 -0
  20. package/dist/docx/comments/policy.js +43 -0
  21. package/dist/docx/comments/reading.d.ts +5 -3
  22. package/dist/docx/comments/reading.js +30 -29
  23. package/dist/docx/comments/writing.d.ts +19 -7
  24. package/dist/docx/comments/writing.js +105 -137
  25. package/dist/docx/documentSettings.d.ts +7 -0
  26. package/dist/docx/documentSettings.js +10 -2
  27. package/dist/docx/exportDocx.d.ts +40 -5
  28. package/dist/docx/exportDocx.js +71 -72
  29. package/dist/docx/exportRefs.d.ts +5 -2
  30. package/dist/docx/exportRefs.js +3 -1
  31. package/dist/docx/fidelity.d.ts +43 -0
  32. package/dist/docx/fidelity.js +64 -0
  33. package/dist/docx/formatting/attrs.d.ts +27 -0
  34. package/dist/docx/formatting/attrs.js +31 -0
  35. package/dist/docx/formatting/context.d.ts +31 -0
  36. package/dist/docx/formatting/context.js +42 -0
  37. package/dist/docx/formatting/direct.d.ts +6 -5
  38. package/dist/docx/formatting/direct.js +52 -81
  39. package/dist/docx/formatting/resolve.d.ts +45 -0
  40. package/dist/docx/formatting/resolve.js +140 -0
  41. package/dist/docx/formatting/runProperties.d.ts +93 -0
  42. package/dist/docx/formatting/runProperties.js +316 -0
  43. package/dist/docx/formatting/styles.js +3 -3
  44. package/dist/docx/formatting/tabStops.js +7 -22
  45. package/dist/docx/formatting.d.ts +4 -1
  46. package/dist/docx/formatting.js +4 -1
  47. package/dist/docx/headersFooters.js +13 -22
  48. package/dist/docx/identities.d.ts +67 -0
  49. package/dist/docx/identities.js +174 -0
  50. package/dist/docx/importDocx.d.ts +12 -1
  51. package/dist/docx/importDocx.js +72 -79
  52. package/dist/docx/importParagraph.d.ts +1 -1
  53. package/dist/docx/importParagraph.js +4 -3
  54. package/dist/docx/importTable.d.ts +1 -1
  55. package/dist/docx/importTable.js +17 -1
  56. package/dist/docx/invariants.d.ts +33 -0
  57. package/dist/docx/invariants.js +256 -0
  58. package/dist/docx/media.d.ts +6 -4
  59. package/dist/docx/media.js +6 -37
  60. package/dist/docx/newLists.d.ts +20 -0
  61. package/dist/docx/newLists.js +36 -0
  62. package/dist/docx/notes.js +6 -12
  63. package/dist/docx/numberingPlanner.d.ts +8 -0
  64. package/dist/docx/numberingPlanner.js +19 -0
  65. package/dist/docx/packageParts.d.ts +42 -0
  66. package/dist/docx/packageParts.js +135 -0
  67. package/dist/docx/pageGeometry.d.ts +2 -0
  68. package/dist/docx/pageGeometry.js +18 -9
  69. package/dist/docx/paraProps.d.ts +10 -22
  70. package/dist/docx/paraProps.js +75 -99
  71. package/dist/docx/partPlan.d.ts +36 -0
  72. package/dist/docx/partPlan.js +59 -0
  73. package/dist/docx/protectionPolicy.d.ts +127 -0
  74. package/dist/docx/protectionPolicy.js +169 -0
  75. package/dist/docx/relationships.d.ts +1 -1
  76. package/dist/docx/relationships.js +15 -15
  77. package/dist/docx/runProps.d.ts +9 -22
  78. package/dist/docx/runProps.js +15 -168
  79. package/dist/docx/scan.js +20 -52
  80. package/dist/docx/sdt.js +14 -37
  81. package/dist/docx/sdtProps.d.ts +8 -1
  82. package/dist/docx/sdtProps.js +10 -0
  83. package/dist/docx/serializeBlock.d.ts +2 -0
  84. package/dist/docx/serializeBlock.js +9 -7
  85. package/dist/docx/serializeParagraph.d.ts +2 -1
  86. package/dist/docx/serializeParagraph.js +25 -14
  87. package/dist/docx/serializeTable.js +63 -32
  88. package/dist/docx/session.d.ts +42 -14
  89. package/dist/docx/session.js +43 -17
  90. package/dist/docx/storyProjection.d.ts +36 -0
  91. package/dist/docx/storyProjection.js +23 -0
  92. package/dist/docx/tableFormatting/editing.js +130 -185
  93. package/dist/docx/tableFormatting/reading.d.ts +16 -6
  94. package/dist/docx/tableFormatting/reading.js +40 -20
  95. package/dist/docx/tableTemplate.js +37 -5
  96. package/dist/docx/theme.d.ts +0 -6
  97. package/dist/docx/theme.js +0 -8
  98. package/dist/download.d.ts +8 -5
  99. package/dist/download.js +2 -0
  100. package/dist/editor/clipboard/images.js +17 -3
  101. package/dist/editor/clipboard/inlineFormatting.d.ts +7 -0
  102. package/dist/editor/clipboard/inlineFormatting.js +20 -30
  103. package/dist/editor/commands/breakCommands.js +4 -3
  104. package/dist/editor/commands/canRunCommand.d.ts +2 -2
  105. package/dist/editor/commands/canRunCommand.js +1 -1
  106. package/dist/editor/commands/comments/editing.d.ts +6 -2
  107. package/dist/editor/commands/comments/editing.js +21 -36
  108. package/dist/editor/commands/exportQueries.d.ts +15 -0
  109. package/dist/editor/commands/exportQueries.js +14 -0
  110. package/dist/editor/commands/fidelityQueries.d.ts +12 -0
  111. package/dist/editor/commands/fidelityQueries.js +8 -0
  112. package/dist/editor/commands/formatting/editing.d.ts +2 -2
  113. package/dist/editor/commands/formatting/editing.js +26 -108
  114. package/dist/editor/commands/formatting/propertyCommands.d.ts +21 -0
  115. package/dist/editor/commands/formatting/propertyCommands.js +103 -0
  116. package/dist/editor/commands/formatting/shared.d.ts +3 -3
  117. package/dist/editor/commands/formatting/shared.js +5 -2
  118. package/dist/editor/commands/historyCommands.js +2 -1
  119. package/dist/editor/commands/indentCommands.js +5 -4
  120. package/dist/editor/commands/index.d.ts +11 -0
  121. package/dist/editor/commands/index.js +5 -0
  122. package/dist/editor/commands/linkCommands.js +5 -6
  123. package/dist/editor/commands/listCommands.js +8 -10
  124. package/dist/editor/commands/lockCommands.d.ts +7 -1
  125. package/dist/editor/commands/lockCommands.js +35 -37
  126. package/dist/editor/commands/paragraphCommands.js +28 -31
  127. package/dist/editor/commands/spacingCommands.js +1 -1
  128. package/dist/editor/commands/tabCommands.js +4 -3
  129. package/dist/editor/createEditor.d.ts +17 -29
  130. package/dist/editor/createEditor.js +61 -67
  131. package/dist/editor/documentStyles.d.ts +11 -35
  132. package/dist/editor/documentStyles.js +9 -52
  133. package/dist/editor/editorDocument.d.ts +56 -0
  134. package/dist/editor/editorDocument.js +74 -0
  135. package/dist/editor/externalClipboard.js +166 -33
  136. package/dist/editor/insertImage.js +4 -3
  137. package/dist/editor/insertTable.js +4 -3
  138. package/dist/editor/paragraphEdits.d.ts +11 -18
  139. package/dist/editor/paragraphEdits.js +7 -16
  140. package/dist/editor/plugins/displayDerivation.d.ts +47 -0
  141. package/dist/editor/plugins/displayDerivation.js +78 -0
  142. package/dist/editor/plugins/documentProtection.d.ts +3 -3
  143. package/dist/editor/plugins/imagePaste.js +4 -3
  144. package/dist/editor/plugins/keymap.js +18 -3
  145. package/dist/editor/plugins/lockedContent.d.ts +4 -4
  146. package/dist/editor/plugins/lockedContent.js +1 -1
  147. package/dist/editor/plugins/numberingDecorations.d.ts +5 -11
  148. package/dist/editor/plugins/numberingDecorations.js +7 -17
  149. package/dist/editor/plugins/paragraphDisplay.d.ts +7 -0
  150. package/dist/editor/plugins/paragraphDisplay.js +51 -0
  151. package/dist/editor/plugins/tabLayout.js +1 -1
  152. package/dist/editor/plugins/tableDisplay.d.ts +6 -0
  153. package/dist/editor/plugins/tableDisplay.js +16 -0
  154. package/dist/index.d.ts +2 -0
  155. package/dist/model/format.d.ts +30 -8
  156. package/dist/model/format.js +35 -22
  157. package/dist/model/tabStops.d.ts +9 -0
  158. package/dist/model/tabStops.js +18 -0
  159. package/dist/numbering/listTemplate.js +49 -10
  160. package/dist/numbering/parseNumbering.d.ts +14 -1
  161. package/dist/numbering/parseNumbering.js +35 -22
  162. package/dist/numbering/writeNumbering.d.ts +3 -4
  163. package/dist/numbering/writeNumbering.js +14 -25
  164. package/dist/ooxml/childOrder.d.ts +34 -0
  165. package/dist/ooxml/childOrder.js +496 -0
  166. package/dist/ooxml/element.d.ts +65 -0
  167. package/dist/ooxml/element.js +68 -0
  168. package/dist/ooxml/errors.d.ts +3 -2
  169. package/dist/ooxml/fragment.d.ts +53 -0
  170. package/dist/ooxml/fragment.js +76 -0
  171. package/dist/ooxml/image.d.ts +4 -0
  172. package/dist/ooxml/image.js +3 -5
  173. package/dist/ooxml/names.d.ts +27 -0
  174. package/dist/ooxml/names.js +29 -0
  175. package/dist/ooxml/partSplice.d.ts +67 -0
  176. package/dist/ooxml/partSplice.js +169 -0
  177. package/dist/ooxml/precedence.d.ts +31 -0
  178. package/dist/ooxml/precedence.js +51 -0
  179. package/dist/ooxml/props.d.ts +112 -0
  180. package/dist/ooxml/props.js +214 -0
  181. package/dist/ooxml/simpleTypes.d.ts +103 -0
  182. package/dist/ooxml/simpleTypes.js +182 -0
  183. package/dist/ooxml/tabStops.js +8 -27
  184. package/dist/ooxml/tagScan.d.ts +34 -0
  185. package/dist/ooxml/tagScan.js +108 -0
  186. package/dist/ooxml/units.d.ts +25 -13
  187. package/dist/ooxml/units.js +65 -31
  188. package/dist/ooxml/xml.d.ts +52 -0
  189. package/dist/ooxml/xml.js +64 -5
  190. package/dist/page/blockKinds.d.ts +103 -0
  191. package/dist/page/blockKinds.js +11 -0
  192. package/dist/page/kinds/index.d.ts +6 -0
  193. package/dist/page/kinds/index.js +10 -0
  194. package/dist/page/kinds/paragraphKind.d.ts +10 -0
  195. package/dist/page/kinds/paragraphKind.js +71 -0
  196. package/dist/page/kinds/tableKind.d.ts +10 -0
  197. package/dist/page/kinds/tableKind.js +178 -0
  198. package/dist/page/measureBlocks.d.ts +3 -1
  199. package/dist/page/measureBlocks.js +24 -25
  200. package/dist/page/pageDecorations.d.ts +27 -28
  201. package/dist/page/pageDecorations.js +70 -151
  202. package/dist/page/pageLayout.d.ts +10 -62
  203. package/dist/page/pageLayout.js +60 -53
  204. package/dist/page/usePageLayout.d.ts +2 -2
  205. package/dist/page/usePageLayout.js +3 -11
  206. package/dist/schema/attrRoles.d.ts +42 -0
  207. package/dist/schema/attrRoles.js +141 -0
  208. package/dist/schema/displayDerivation.d.ts +82 -0
  209. package/dist/schema/displayDerivation.js +130 -0
  210. package/dist/schema/docxSchema.d.ts +9 -0
  211. package/dist/schema/docxSchema.js +237 -114
  212. package/dist/schema/editGuard.d.ts +99 -0
  213. package/dist/schema/editGuard.js +43 -0
  214. package/dist/schema/guards.d.ts +91 -0
  215. package/dist/schema/guards.js +108 -0
  216. package/dist/schema/index.d.ts +2 -0
  217. package/dist/schema/index.js +2 -0
  218. package/dist/schema/locks.d.ts +17 -77
  219. package/dist/schema/locks.js +26 -70
  220. package/dist/schema/preservedGuards.d.ts +50 -0
  221. package/dist/schema/preservedGuards.js +80 -0
  222. package/dist/schema/protection.d.ts +17 -4
  223. package/dist/schema/protection.js +4 -2
  224. package/dist/schema/sourceEquality.d.ts +19 -0
  225. package/dist/schema/sourceEquality.js +39 -0
  226. package/dist/styles/inlineStyle.js +15 -6
  227. package/dist/table/cellFormatting.d.ts +8 -1
  228. package/dist/table/cellFormatting.js +10 -17
  229. package/dist/table/commands.d.ts +2 -2
  230. package/dist/table/commands.js +20 -18
  231. package/dist/table/format.d.ts +3 -0
  232. package/dist/table/format.js +3 -9
  233. package/dist/table/gridBorders.d.ts +14 -16
  234. package/dist/table/gridBorders.js +3 -41
  235. package/dist/table/index.d.ts +2 -2
  236. package/dist/table/merge.d.ts +4 -8
  237. package/dist/table/merge.js +5 -5
  238. package/package.json +10 -3
  239. package/dist/docx/comments/contentTypes.d.ts +0 -7
  240. package/dist/docx/comments/contentTypes.js +0 -38
  241. package/dist/docx/formatting/effectiveParagraph.d.ts +0 -15
  242. package/dist/docx/formatting/effectiveParagraph.js +0 -81
  243. package/dist/docx/propsXml.d.ts +0 -49
  244. package/dist/docx/propsXml.js +0 -302
  245. package/dist/docx/uniqueControls.d.ts +0 -14
  246. package/dist/docx/uniqueControls.js +0 -62
  247. package/dist/editor/plugins/bookmarkProtection.d.ts +0 -3
  248. package/dist/editor/plugins/bookmarkProtection.js +0 -28
  249. package/dist/editor/plugins/commentReservations.d.ts +0 -5
  250. package/dist/editor/plugins/commentReservations.js +0 -26
  251. package/dist/editor/plugins/noteProtection.d.ts +0 -3
  252. package/dist/editor/plugins/noteProtection.js +0 -25
  253. package/dist/editor/plugins/styledParagraphs.d.ts +0 -15
  254. package/dist/editor/plugins/styledParagraphs.js +0 -65
  255. package/dist/page/tableMeasurements.d.ts +0 -10
  256. package/dist/page/tableMeasurements.js +0 -115
@@ -1,7 +1,17 @@
1
1
  // src/numbering/parseNumbering.ts
2
+ import {
3
+ ST_DecimalNumber,
4
+ ST_SignedTwipsMeasure,
5
+ ST_TwipsMeasure
6
+ } from "../ooxml/simpleTypes.js";
2
7
  import { readTabStopDirectives } from "../ooxml/tabStops.js";
3
- import { childValue, round, wAttr } from "../ooxml/units.js";
4
- import { childByLocalName, elementChildren, parseXml } from "../ooxml/xml.js";
8
+ import { childValue, twipsToPt, wAttr } from "../ooxml/units.js";
9
+ import {
10
+ childByLocalName,
11
+ elementChildren,
12
+ parseXml,
13
+ withXmlParser
14
+ } from "../ooxml/xml.js";
5
15
  var NUMBER_FORMATS = [
6
16
  "decimal",
7
17
  "bullet",
@@ -10,11 +20,6 @@ var NUMBER_FORMATS = [
10
20
  "lowerRoman"
11
21
  ];
12
22
  var EMPTY_NUMBERING = { lists: /* @__PURE__ */ new Map() };
13
- function toInteger(value) {
14
- if (value === null) return null;
15
- const parsed = Number.parseInt(value, 10);
16
- return Number.isFinite(parsed) ? parsed : null;
17
- }
18
23
  function isNumberFormat(value) {
19
24
  return NUMBER_FORMATS.some((format) => format === value);
20
25
  }
@@ -22,13 +27,17 @@ function indentOf(lvl) {
22
27
  const pPr = childByLocalName(lvl, "pPr");
23
28
  const ind = pPr ? childByLocalName(pPr, "ind") : null;
24
29
  if (!ind) return null;
25
- const startTwips = toInteger(wAttr(ind, "start") ?? wAttr(ind, "left"));
26
- const endTwips = toInteger(wAttr(ind, "end") ?? wAttr(ind, "right"));
30
+ const startTwips = ST_SignedTwipsMeasure.parse(
31
+ wAttr(ind, "start") ?? wAttr(ind, "left")
32
+ );
33
+ const endTwips = ST_SignedTwipsMeasure.parse(
34
+ wAttr(ind, "end") ?? wAttr(ind, "right")
35
+ );
27
36
  const indent = {
28
37
  startTwips,
29
38
  endTwips,
30
- hangingTwips: toInteger(wAttr(ind, "hanging")),
31
- firstLineTwips: toInteger(wAttr(ind, "firstLine"))
39
+ hangingTwips: ST_TwipsMeasure.parse(wAttr(ind, "hanging")),
40
+ firstLineTwips: ST_TwipsMeasure.parse(wAttr(ind, "firstLine"))
32
41
  };
33
42
  const empty = startTwips === null && endTwips === null && indent.hangingTwips === null && indent.firstLineTwips === null;
34
43
  return empty ? null : indent;
@@ -38,9 +47,6 @@ var NO_LEVEL_INDENT = {
38
47
  endPt: null,
39
48
  textIndentPt: null
40
49
  };
41
- function twipsToPt(twips) {
42
- return twips === null ? null : round(twips / 20);
43
- }
44
50
  function levelIndentPt(indent) {
45
51
  if (!indent) return NO_LEVEL_INDENT;
46
52
  const hangingPt = twipsToPt(indent.hangingTwips);
@@ -57,7 +63,7 @@ function readLevel(lvl) {
57
63
  return {
58
64
  format: isNumberFormat(format) ? format : "decimal",
59
65
  text: childValue(lvl, "lvlText") ?? "",
60
- start: toInteger(childValue(lvl, "start")) ?? 1,
66
+ start: ST_DecimalNumber.parse(childValue(lvl, "start")) ?? 1,
61
67
  indent: indentOf(lvl),
62
68
  ...tabStops.length === 0 ? {} : { tabStops }
63
69
  };
@@ -66,23 +72,27 @@ function readLevels(parent) {
66
72
  const levels = /* @__PURE__ */ new Map();
67
73
  for (const child of elementChildren(parent)) {
68
74
  if (child.localName !== "lvl") continue;
69
- const ilvl = toInteger(wAttr(child, "ilvl"));
75
+ const ilvl = ST_DecimalNumber.parse(wAttr(child, "ilvl"));
70
76
  if (ilvl !== null) levels.set(ilvl, readLevel(child));
71
77
  }
72
78
  return levels;
73
79
  }
74
80
  function readList(num, abstractLevels) {
75
- const abstractNumId = toInteger(childValue(num, "abstractNumId"));
81
+ const abstractNumId = ST_DecimalNumber.parse(
82
+ childValue(num, "abstractNumId")
83
+ );
76
84
  if (abstractNumId === null) return null;
77
85
  const levels = new Map(abstractLevels.get(abstractNumId) ?? []);
78
86
  for (const child of elementChildren(num)) {
79
87
  if (child.localName !== "lvlOverride") continue;
80
- const ilvl = toInteger(wAttr(child, "ilvl"));
88
+ const ilvl = ST_DecimalNumber.parse(wAttr(child, "ilvl"));
81
89
  if (ilvl === null) continue;
82
90
  const replacement = childByLocalName(child, "lvl");
83
91
  const base = replacement ? readLevel(replacement) : levels.get(ilvl);
84
92
  if (!base) continue;
85
- const startOverride = toInteger(childValue(child, "startOverride"));
93
+ const startOverride = ST_DecimalNumber.parse(
94
+ childValue(child, "startOverride")
95
+ );
86
96
  levels.set(
87
97
  ilvl,
88
98
  startOverride === null ? base : { ...base, start: startOverride }
@@ -90,19 +100,22 @@ function readList(num, abstractLevels) {
90
100
  }
91
101
  return { levels };
92
102
  }
93
- function parseNumbering(xml) {
103
+ function parseNumbering(xml, options) {
94
104
  if (xml === null) return EMPTY_NUMBERING;
105
+ return withXmlParser(options?.xmlParser, () => readNumbering(xml));
106
+ }
107
+ function readNumbering(xml) {
95
108
  const root = parseXml(xml).documentElement;
96
109
  const abstractLevels = /* @__PURE__ */ new Map();
97
110
  for (const child of elementChildren(root)) {
98
111
  if (child.localName !== "abstractNum") continue;
99
- const id = toInteger(wAttr(child, "abstractNumId"));
112
+ const id = ST_DecimalNumber.parse(wAttr(child, "abstractNumId"));
100
113
  if (id !== null) abstractLevels.set(id, readLevels(child));
101
114
  }
102
115
  const lists = /* @__PURE__ */ new Map();
103
116
  for (const child of elementChildren(root)) {
104
117
  if (child.localName !== "num") continue;
105
- const numId = toInteger(wAttr(child, "numId"));
118
+ const numId = ST_DecimalNumber.parse(wAttr(child, "numId"));
106
119
  const list = numId === null ? null : readList(child, abstractLevels);
107
120
  if (numId !== null && list) lists.set(numId, list);
108
121
  }
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * Splices the definitions of newly started lists into the original numbering.xml.
3
3
  *
4
- * Not one character of the original is altered; new elements are merely slotted in at
5
- * two places.
6
- * OOXML requires the definitions (`abstractNum`) to come before the numbers (`num`), so
7
- * the definitions go in front of the first number and the numbers go at the very end.
4
+ * Not one character of the original is altered; new elements are merely slotted in.
5
+ * OOXML requires the definitions (`abstractNum`) to come before the numbers (`num`), and
6
+ * `CHILD_ORDER.numbering` is what places each where it belongs among what the part holds.
8
7
  */
9
8
  /**
10
9
  * numbering.xml with a standard template definition added for every list number.
@@ -1,18 +1,8 @@
1
1
  // src/numbering/writeNumbering.ts
2
- import { DocxExportError } from "../ooxml/errors.js";
2
+ import { splicePart } from "../ooxml/partSplice.js";
3
3
  import { wAttr } from "../ooxml/units.js";
4
4
  import { elementChildren, parseXml } from "../ooxml/xml.js";
5
5
  import { abstractNumXml, listKindOf, numXml } from "./listTemplate.js";
6
- function openTagAt(xml, name) {
7
- const match = new RegExp(`<(?:[A-Za-z_][\\w.-]*:)?${name}[\\s/>]`).exec(xml);
8
- return match ? match.index : null;
9
- }
10
- function closeTagAt(xml, name) {
11
- const matches = Array.from(
12
- xml.matchAll(new RegExp(`</(?:[A-Za-z_][\\w.-]*:)?${name}\\s*>`, "g"))
13
- );
14
- return matches.at(-1)?.index ?? null;
15
- }
16
6
  function maxAbstractNumId(xml) {
17
7
  let max = 0;
18
8
  for (const child of elementChildren(parseXml(xml).documentElement)) {
@@ -24,25 +14,24 @@ function maxAbstractNumId(xml) {
24
14
  }
25
15
  function addListDefinitions(xml, numIds) {
26
16
  if (numIds.length === 0) return xml;
27
- const rootEnd = closeTagAt(xml, "numbering");
28
- if (rootEnd === null) {
29
- throw new DocxExportError(
30
- "malformed-xml",
31
- "no closing tag found in numbering.xml"
32
- );
33
- }
34
- const numsAt = openTagAt(xml, "numIdMacAtCleanup") ?? rootEnd;
35
- const definitionsAt = openTagAt(xml, "num") ?? numsAt;
36
17
  const firstAbstractNumId = maxAbstractNumId(xml) + 1;
37
18
  const additions = [...numIds].sort((a, b) => a - b).map((numId, index) => ({
38
19
  numId,
39
20
  abstractNumId: firstAbstractNumId + index
40
21
  }));
41
- const definitions = additions.map(
42
- (added) => abstractNumXml(added.abstractNumId, listKindOf(added.numId))
43
- ).join("");
44
- const nums = additions.map((added) => numXml(added.numId, added.abstractNumId)).join("");
45
- return xml.slice(0, definitionsAt) + definitions + xml.slice(definitionsAt, numsAt) + nums + xml.slice(numsAt);
22
+ return splicePart(xml, {
23
+ root: "numbering",
24
+ insert: [
25
+ ...additions.map((added) => ({
26
+ name: "abstractNum",
27
+ xml: abstractNumXml(added.abstractNumId, listKindOf(added.numId))
28
+ })),
29
+ ...additions.map((added) => ({
30
+ name: "num",
31
+ xml: numXml(added.numId, added.abstractNumId)
32
+ }))
33
+ ]
34
+ });
46
35
  }
47
36
  export {
48
37
  addListDefinitions
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The order the children of one element go in, per parent.
3
+ *
4
+ * A writer that swaps one child of a formatting fragment has to put a child that was not there
5
+ * into the spot the format lays down, and `wml.xsd` is what lays it down. Each entry below names
6
+ * the complex type it mirrors, and `childOrder.test.ts` reads that type out of the committed
7
+ * schema and compares it, so an entry cannot drift from the format it claims to follow.
8
+ *
9
+ * Where the type is a sequence the order is the schema's and nothing else. Where it is a choice
10
+ * the schema fixes no order among the alternatives, so the order here is this package's
11
+ * convention: the schema's own document order. Mutually exclusive alternatives (a content
12
+ * control's type, a cell's markup record) are listed in that same order, since only one of them
13
+ * can ever stand there.
14
+ *
15
+ * A name is the local part alone, as `Props` carries it, which is also how the two children
16
+ * `w:settings` takes from other namespaces (`m:mathPr`, `sl:schemaLibrary`) are spelled here.
17
+ */
18
+ interface ChildOrder {
19
+ readonly type: string;
20
+ readonly order: readonly string[];
21
+ }
22
+ export declare const CHILD_ORDER: Readonly<Record<string, ChildOrder>>;
23
+ /**
24
+ * The order the children of this parent go in.
25
+ *
26
+ * A nested parent is asked for first (`pPr/rPr` is CT_ParaRPr, which puts a track-change record
27
+ * ahead of what a run's own `w:rPr` holds), so naming the enclosing element is what picks the
28
+ * right one of two entries for the same element name.
29
+ *
30
+ * Throws for a parent no entry covers, because a writer editing an element this registry has
31
+ * never seen would otherwise place its children by nothing at all.
32
+ */
33
+ export declare function childOrderOf(name: string, parent?: string): readonly string[];
34
+ export {};