@portone/docx-editor 0.6.2 → 0.6.4

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 (107) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/comments/writing.d.ts +8 -0
  4. package/dist/docx/comments/writing.js +9 -2
  5. package/dist/docx/headersFooters.d.ts +7 -0
  6. package/dist/docx/headersFooters.js +8 -15
  7. package/dist/docx/identities.d.ts +9 -2
  8. package/dist/docx/identities.js +45 -12
  9. package/dist/docx/importDocx.js +2 -0
  10. package/dist/docx/importParagraph.d.ts +4 -0
  11. package/dist/docx/importParagraph.js +2 -0
  12. package/dist/docx/importPolicy.d.ts +2 -2
  13. package/dist/docx/importPolicy.js +12 -5
  14. package/dist/docx/importPreserved.d.ts +3 -3
  15. package/dist/docx/importSdtBlock.d.ts +23 -0
  16. package/dist/docx/importSdtBlock.js +33 -0
  17. package/dist/docx/importTable.js +35 -24
  18. package/dist/docx/invariants.js +60 -3
  19. package/dist/docx/notes/numbering.js +4 -4
  20. package/dist/docx/notes/references.d.ts +0 -2
  21. package/dist/docx/notes/references.js +2 -7
  22. package/dist/docx/numberingPlanner.d.ts +3 -0
  23. package/dist/docx/numberingPlanner.js +8 -2
  24. package/dist/docx/packageParts.d.ts +9 -0
  25. package/dist/docx/packageParts.js +16 -0
  26. package/dist/docx/packagePrefixes.d.ts +12 -0
  27. package/dist/docx/packagePrefixes.js +18 -0
  28. package/dist/docx/partPlan.js +3 -5
  29. package/dist/docx/sdt.d.ts +79 -9
  30. package/dist/docx/sdt.js +121 -26
  31. package/dist/docx/sections.d.ts +13 -13
  32. package/dist/docx/sections.js +34 -20
  33. package/dist/docx/serializeBlock.js +10 -1
  34. package/dist/docx/serializeParagraph.d.ts +2 -1
  35. package/dist/docx/serializeParagraph.js +6 -1
  36. package/dist/docx/serializeSdtBlock.d.ts +16 -0
  37. package/dist/docx/serializeSdtBlock.js +11 -0
  38. package/dist/docx/serializeTable.d.ts +8 -2
  39. package/dist/docx/serializeTable.js +14 -20
  40. package/dist/docx/story.d.ts +10 -4
  41. package/dist/docx/story.js +25 -20
  42. package/dist/docx/storyBlocks.d.ts +32 -0
  43. package/dist/docx/storyBlocks.js +65 -0
  44. package/dist/docx/storyParts.d.ts +3 -0
  45. package/dist/docx/storyParts.js +4 -1
  46. package/dist/docx/wrappers.d.ts +33 -15
  47. package/dist/docx/wrappers.js +45 -26
  48. package/dist/editor/blockControlEdits.d.ts +15 -0
  49. package/dist/editor/blockControlEdits.js +79 -0
  50. package/dist/editor/clipboard/normalizers.js +1 -1
  51. package/dist/editor/commands/lockCommands.d.ts +5 -1
  52. package/dist/editor/commands/lockCommands.js +51 -23
  53. package/dist/editor/createEditor.js +4 -0
  54. package/dist/editor/inlineControlEdits.d.ts +20 -0
  55. package/dist/editor/inlineControlEdits.js +69 -0
  56. package/dist/editor/notes/noteSurface.js +4 -3
  57. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  58. package/dist/editor/plugins/controlLifecycle.js +130 -0
  59. package/dist/editor/plugins/keymap.js +34 -5
  60. package/dist/editor/plugins/rowResize.js +2 -4
  61. package/dist/editor/stories/storyState.d.ts +2 -0
  62. package/dist/editor/stories/storyState.js +2 -0
  63. package/dist/ooxml/element.js +3 -2
  64. package/dist/ooxml/errors.d.ts +11 -0
  65. package/dist/ooxml/fragment.js +3 -2
  66. package/dist/ooxml/names.d.ts +4 -0
  67. package/dist/ooxml/names.js +8 -2
  68. package/dist/ooxml/partPrefixes.d.ts +21 -0
  69. package/dist/ooxml/partPrefixes.js +164 -0
  70. package/dist/ooxml/partSplice.d.ts +6 -1
  71. package/dist/ooxml/partSplice.js +30 -10
  72. package/dist/ooxml/tagScan.d.ts +24 -2
  73. package/dist/ooxml/tagScan.js +26 -7
  74. package/dist/ooxml/xml.d.ts +8 -0
  75. package/dist/ooxml/xml.js +27 -5
  76. package/dist/page/blockKinds.d.ts +24 -2
  77. package/dist/page/blockKinds.js +6 -1
  78. package/dist/page/kinds/index.d.ts +5 -0
  79. package/dist/page/kinds/index.js +2 -0
  80. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  81. package/dist/page/kinds/sdtBlockKind.js +143 -0
  82. package/dist/page/measureBlocks.js +4 -2
  83. package/dist/page/pageDecorations.js +3 -2
  84. package/dist/page/pageLayout.d.ts +16 -15
  85. package/dist/page/pageLayout.js +134 -67
  86. package/dist/page/usePageLayout.d.ts +2 -2
  87. package/dist/page/usePageLayout.js +5 -5
  88. package/dist/schema/attrRoles.js +56 -1
  89. package/dist/schema/clipboard.js +11 -0
  90. package/dist/schema/controlAttrs.d.ts +119 -0
  91. package/dist/schema/controlAttrs.js +137 -0
  92. package/dist/schema/controlEdges.d.ts +22 -0
  93. package/dist/schema/controlEdges.js +50 -0
  94. package/dist/schema/docxSchema.d.ts +1 -1
  95. package/dist/schema/docxSchema.js +235 -37
  96. package/dist/schema/editGuard.d.ts +1 -1
  97. package/dist/schema/guards.js +2 -0
  98. package/dist/schema/locks.d.ts +56 -8
  99. package/dist/schema/locks.js +115 -56
  100. package/dist/styles/classNames.d.ts +10 -0
  101. package/dist/styles/classNames.js +10 -0
  102. package/dist/styles.css +44 -2
  103. package/dist/table/merge.js +11 -3
  104. package/dist/table/rowResize.d.ts +9 -0
  105. package/dist/table/rowResize.js +9 -5
  106. package/dist/ui/TextMenu.js +2 -0
  107. package/package.json +3 -3
@@ -7,6 +7,11 @@ import {
7
7
  serializeXml
8
8
  } from "../ooxml/xml.js";
9
9
  import { docxSchema } from "../schema/index.js";
10
+ import {
11
+ controlAttrs,
12
+ NO_CONTROL,
13
+ WRAPPED_CONTROL_ATTRS
14
+ } from "../schema/controlAttrs.js";
10
15
  import {
11
16
  layerTableFormat,
12
17
  NO_FORMATTING,
@@ -20,7 +25,8 @@ import {
20
25
  } from "./importParagraph.js";
21
26
  import { policyFor } from "./importPolicy.js";
22
27
  import { buildPreservedBlock } from "./importPreserved.js";
23
- import { readSdtWrapper } from "./sdt.js";
28
+ import { buildSdtBlock } from "./importSdtBlock.js";
29
+ import { controlFactsFrom, readSdtWrapper } from "./sdt.js";
24
30
  import {
25
31
  cellConditionsOf,
26
32
  cellDefaultsFor,
@@ -58,23 +64,20 @@ function readVerticalMerge(tcPr) {
58
64
  if (!vMerge) return null;
59
65
  return wAttr(vMerge, "val") === "restart" ? "restart" : "continue";
60
66
  }
61
- function readSdtCell(el) {
67
+ function readSdtSingle(el, localName) {
62
68
  const wrapper = readSdtWrapper(el);
63
69
  if (!wrapper) return null;
64
70
  const inner = elementChildren(wrapper.content);
65
- const tc = inner[0];
66
- if (inner.length !== 1 || tc.localName !== "tc") return null;
67
- if (readVerticalMerge(childByLocalName(tc, "tcPr")) === "continue") {
68
- return null;
69
- }
70
- return {
71
- el: tc,
72
- control: {
73
- prefix: wrapper.prefix,
74
- contentsLocked: wrapper.contentsLocked,
75
- deletionLocked: wrapper.deletionLocked
76
- }
77
- };
71
+ if (inner.length !== 1 || inner[0].localName !== localName) return null;
72
+ return { el: inner[0], control: controlFactsFrom(wrapper) };
73
+ }
74
+ function readSdtCell(el) {
75
+ const cell = readSdtSingle(el, "tc");
76
+ if (!cell) return null;
77
+ return readVerticalMerge(childByLocalName(cell.el, "tcPr")) === "continue" ? null : cell;
78
+ }
79
+ function readSdtRow(el) {
80
+ return readSdtSingle(el, "tr");
78
81
  }
79
82
  function trailCell(cells, gathered) {
80
83
  const xml = takeGathered(gathered);
@@ -84,7 +87,7 @@ function trailCell(cells, gathered) {
84
87
  last.trailingXml = (last.trailingXml ?? "") + xml;
85
88
  return true;
86
89
  }
87
- function readRow(el) {
90
+ function readRow(el, control) {
88
91
  const cells = [];
89
92
  const gathered = [];
90
93
  let tblPrEx = null;
@@ -114,7 +117,7 @@ function readRow(el) {
114
117
  });
115
118
  }
116
119
  if (cells.length === 0 || !trailCell(cells, gathered)) return null;
117
- return { el, tblPrEx, cells, leadingXml, trailingXml: null };
120
+ return { el, tblPrEx, cells, control, leadingXml, trailingXml: null };
118
121
  }
119
122
  function gridRevisionXml(grid) {
120
123
  const revision = childByLocalName(grid, "tblGridChange");
@@ -150,8 +153,9 @@ function readTableParts(el) {
150
153
  gathered.push(serializeXml(child));
151
154
  continue;
152
155
  }
153
- if (child.localName !== "tr") return null;
154
- const row = readRow(child);
156
+ const sdt = child.localName === "sdt" ? readSdtRow(child) : null;
157
+ if (!sdt && child.localName !== "tr") return null;
158
+ const row = readRow(sdt?.el ?? child, sdt?.control ?? null);
155
159
  if (!row) return null;
156
160
  const previous = rows.at(-1);
157
161
  if (previous) previous.trailingXml = takeGathered(gathered);
@@ -205,7 +209,7 @@ function gridWidthOf(rows) {
205
209
  const first = widths[0];
206
210
  return widths.every((width) => width === first) ? first : null;
207
211
  }
208
- function buildCellBlock(el, sources, context, placement) {
212
+ function buildCellBlock(el, sources, context, placement, level = "tc") {
209
213
  if (el.localName === "p") {
210
214
  return styledParagraph(
211
215
  buildParagraph(el, null, sources),
@@ -213,7 +217,15 @@ function buildCellBlock(el, sources, context, placement) {
213
217
  placement
214
218
  );
215
219
  }
216
- return buildPreservedBlock(el, null, "tc");
220
+ if (el.localName === "sdt") {
221
+ const control = buildSdtBlock(
222
+ el,
223
+ null,
224
+ (child) => buildCellBlock(child, sources, context, placement, "sdtContent")
225
+ );
226
+ if (control) return control;
227
+ }
228
+ return buildPreservedBlock(el, null, level);
217
229
  }
218
230
  function cellRect(draft) {
219
231
  return {
@@ -241,9 +253,7 @@ function buildCell(draft, table, sources, context) {
241
253
  tcPr: tcPr ? serializeXml(tcPr) : null,
242
254
  tcW: readTableWidth(tcPr, "tcW"),
243
255
  format: readCellFormat(tcPr, defaults),
244
- sdtPrefix: draft.control?.prefix ?? null,
245
- sdtContentsLocked: draft.control?.contentsLocked ?? false,
246
- sdtDeletionLocked: draft.control?.deletionLocked ?? false,
256
+ ...controlAttrs(WRAPPED_CONTROL_ATTRS, draft.control ?? NO_CONTROL),
247
257
  trailingXml: draft.trailingXml
248
258
  },
249
259
  blocks
@@ -263,6 +273,7 @@ function buildRow(row, drafts, table, sources, context) {
263
273
  tblPrEx: row.tblPrEx ? serializeXml(row.tblPrEx) : null,
264
274
  trPr: trPr ? serializeXml(trPr) : null,
265
275
  format: readRowFormat(trPr),
276
+ ...controlAttrs(WRAPPED_CONTROL_ATTRS, row.control ?? NO_CONTROL),
266
277
  leadingXml: row.leadingXml,
267
278
  trailingXml: row.trailingXml
268
279
  },
@@ -1,5 +1,8 @@
1
1
  // src/docx/invariants.ts
2
2
  import { spanCount, toParagraphFormat } from "../model/format.js";
3
+ import {
4
+ rootBindingConflict
5
+ } from "../ooxml/partSplice.js";
3
6
  import {
4
7
  attributeByLocalName,
5
8
  parseXml,
@@ -27,12 +30,18 @@ import {
27
30
  peoplePart
28
31
  } from "./comments/parts.js";
29
32
  import { unrecordedAuthors } from "./comments/people.js";
30
- import { currentCommentBodies } from "./comments/writing.js";
33
+ import {
34
+ COMMENT_MARKUP,
35
+ currentCommentBodies,
36
+ EXTENSIONS_MARKUP
37
+ } from "./comments/writing.js";
38
+ import { HEADER_MARKUP } from "./headersFooters.js";
31
39
  import { identityProblems, identityProblemsInStories } from "./identities.js";
32
40
  import { insertedImageSrcs } from "./media.js";
33
41
  import { canDefineNewList, newNumIds, startedLists } from "./newLists.js";
34
42
  import { firstNoteReferences } from "./notes/references.js";
35
- import { CONTENT_TYPES_PATH } from "./packageParts.js";
43
+ import { NUMBERING_MARKUP } from "./numberingPlanner.js";
44
+ import { CONTENT_TYPES_PATH, readPart, relatedPartPath } from "./packageParts.js";
36
45
  import { STORY_ENTRIES_PARTS, STORY_WRITINGS } from "./partPlanners.js";
37
46
  import { lostOriginal } from "./serializePreserved.js";
38
47
  import {
@@ -40,6 +49,7 @@ import {
40
49
  sessionOf
41
50
  } from "./session.js";
42
51
  import {
52
+ ENTRY_MARKUP,
43
53
  rewrittenStories,
44
54
  storyChangesOf,
45
55
  storyEntriesOf,
@@ -378,6 +388,52 @@ var notePartRoots = {
378
388
  (part) => storyEntriesProblems(part, doc, session)
379
389
  )
380
390
  };
391
+ function rewrittenParts(doc, session) {
392
+ const parts = [];
393
+ const add = (path, xml2, declarations) => {
394
+ if (path !== null && xml2 !== null) parts.push({ path, xml: xml2, declarations });
395
+ };
396
+ for (const { key } of rewrittenStories(STORY_WRITINGS, doc, session)) {
397
+ const { kind, id } = splitStoryKey(key);
398
+ if (kind === "header" || kind === "footer") {
399
+ add(id, readPart(session.parts, id), HEADER_MARKUP);
400
+ }
401
+ }
402
+ for (const part of STORY_ENTRIES_PARTS) {
403
+ const changed = storyChangesOf(doc, session, part.kind).some(
404
+ ({ change }) => change !== "kept"
405
+ );
406
+ const path = changed ? relatedPartPath(session.parts, session.mainPartPath, part.relType) : null;
407
+ add(path, readPart(session.parts, path), ENTRY_MARKUP);
408
+ }
409
+ const { xml, extendedXml, extendedPartPath } = session.comments;
410
+ if (commentsChanged(doc, session)) {
411
+ add(commentsPart.pathIn(session), xml, COMMENT_MARKUP);
412
+ }
413
+ if (extensionsChanged(doc, session) && (commentReferencesIn(doc).size > 0 || extendedPartPath !== null)) {
414
+ add(commentsExtendedPart.pathIn(session), extendedXml, EXTENSIONS_MARKUP);
415
+ }
416
+ if (startedLists(doc, session).defined.size > 0) {
417
+ const path = session.numberingPartPath;
418
+ add(path, readPart(session.parts, path), NUMBERING_MARKUP);
419
+ }
420
+ return parts;
421
+ }
422
+ var partRootBindings = {
423
+ name: "partRootBindings",
424
+ check: ({ doc, session }) => rewrittenParts(doc, session).flatMap(
425
+ ({ path, xml, declarations }) => {
426
+ const prefix = rootBindingConflict(xml, declarations);
427
+ return prefix === null ? [] : [
428
+ {
429
+ code: "unsupported-content",
430
+ message: `${path} binds ${prefix} to a namespace the writer cannot use`,
431
+ reason: { kind: "conflicting-part-prefix", path, prefix }
432
+ }
433
+ ];
434
+ }
435
+ )
436
+ };
381
437
  function noteKeyIn(reason) {
382
438
  const story = "story" in reason ? reason.story : null;
383
439
  if (story === null) return null;
@@ -412,7 +468,8 @@ var EXPORT_INVARIANTS = [
412
468
  storiesHaveWriters,
413
469
  mediaContentTypes,
414
470
  commentPartRoots,
415
- notePartRoots
471
+ notePartRoots,
472
+ partRootBindings
416
473
  ];
417
474
  function exportScopes(doc, session) {
418
475
  return [
@@ -13,15 +13,15 @@ import { eachNoteReference } from "./references.js";
13
13
  var MAX_LABEL_CHARS = 64;
14
14
  function referencesBySection(doc) {
15
15
  const found = [];
16
- eachNoteReference(doc, ({ node, pos, block }) => {
17
- found.push({ block, reference: { pos, node } });
16
+ eachNoteReference(doc, ({ node, pos }) => {
17
+ found.push({ pos, node });
18
18
  });
19
19
  if (found.length === 0) return [];
20
20
  const sections = sectionsOf(doc);
21
21
  const references = sections.map(() => []);
22
22
  let at = 0;
23
- for (const { block, reference } of found) {
24
- while (block > sections[at].lastBlock) at += 1;
23
+ for (const reference of found) {
24
+ while (reference.pos > sections[at].to) at += 1;
25
25
  references[at].push(reference);
26
26
  }
27
27
  return sections.map((section, index) => ({
@@ -14,8 +14,6 @@ export interface PlacedNoteReference {
14
14
  readonly node: PMNode;
15
15
  /** Where the reference stands in the document */
16
16
  readonly pos: number;
17
- /** The top-level block it stands in, by index, which is what a section covers a run of */
18
- readonly block: number;
19
17
  /** The note it names, and null where it names no kind of note or carries no id */
20
18
  readonly key: NoteKey | null;
21
19
  }
@@ -7,17 +7,12 @@ function keyOf(node) {
7
7
  }
8
8
  function eachNoteReference(doc, visit) {
9
9
  let running = true;
10
- doc.forEach((block, offset, index) => {
10
+ doc.forEach((block, offset) => {
11
11
  if (!running) return;
12
12
  block.descendants((node, pos) => {
13
13
  if (!running) return false;
14
14
  if (node.type.name !== "noteReference") return true;
15
- running = visit({
16
- node,
17
- pos: offset + 1 + pos,
18
- block: index,
19
- key: keyOf(node)
20
- }) !== false;
15
+ running = visit({ node, pos: offset + 1 + pos, key: keyOf(node) }) !== false;
21
16
  return false;
22
17
  });
23
18
  });
@@ -7,5 +7,8 @@
7
7
  * declaration of what it holds asked of the context, so the first list a document takes costs it
8
8
  * nothing outside this planner. Nothing is written when no list was started.
9
9
  */
10
+ import { type RootDeclarations } from "../ooxml/partSplice";
10
11
  import type { PartPlanner } from "./partPlan";
12
+ /** What a numbering part takes a definition: every list this writer splices in is spelled under `w` */
13
+ export declare const NUMBERING_MARKUP: RootDeclarations;
11
14
  export declare const numberingPlanner: PartPlanner;
@@ -2,7 +2,9 @@
2
2
  import { addListDefinitions } from "../numbering/writeNumbering.js";
3
3
  import { elementXml, xmlnsAttr } from "../ooxml/element.js";
4
4
  import { NAMESPACES, wName } from "../ooxml/names.js";
5
- import { ensureRootDeclarations } from "../ooxml/partSplice.js";
5
+ import {
6
+ ensureRootDeclarations
7
+ } from "../ooxml/partSplice.js";
6
8
  import { decodeUtf8, encodeUtf8 } from "../ooxml/xml.js";
7
9
  import {
8
10
  NUMBERING_CONTENT_TYPE,
@@ -12,6 +14,9 @@ import {
12
14
  } from "./newLists.js";
13
15
  import { availablePartPath } from "./packageParts.js";
14
16
  import { directoryOf } from "./relationships.js";
17
+ var NUMBERING_MARKUP = {
18
+ namespaces: { w: NAMESPACES.w }
19
+ };
15
20
  function emptyNumberingPart() {
16
21
  return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + elementXml(wName("numbering"), [xmlnsAttr("w")]);
17
22
  }
@@ -40,11 +45,12 @@ var numberingPlanner = {
40
45
  const { text, hadBom } = decodeUtf8(original.bytes);
41
46
  const rewritten = ensureRootDeclarations(
42
47
  addListDefinitions(text, defined),
43
- { namespaces: { w: NAMESPACES.w } }
48
+ NUMBERING_MARKUP
44
49
  );
45
50
  return /* @__PURE__ */ new Map([[original.path, encodeUtf8(rewritten, hadBom)]]);
46
51
  }
47
52
  };
48
53
  export {
54
+ NUMBERING_MARKUP,
49
55
  numberingPlanner
50
56
  };
@@ -39,4 +39,13 @@ export interface ContentTypeWriter {
39
39
  }
40
40
  /** XML parts named by OPC content types, even when their URI has no .xml suffix. */
41
41
  export declare function declaredXmlParts(types: Uint8Array, paths: Iterable<string>): ReadonlySet<string>;
42
+ /**
43
+ * Which of these parts hold markup: the ones named as XML or as relationships, and the ones the
44
+ * content types declare as XML under a name of their own. Media bytes are neither.
45
+ *
46
+ * The open respells the namespaces of these parts and the export reads back the ones it wrote, so
47
+ * a part one of them took for markup and the other did not would be spelled one way and checked
48
+ * another.
49
+ */
50
+ export declare function markupParts(paths: Iterable<string>, contentTypes?: Uint8Array): ReadonlySet<string>;
42
51
  export declare function contentTypeWriter(parts: ReadonlyMap<string, Uint8Array>): ContentTypeWriter;
@@ -75,6 +75,21 @@ function declaredXmlParts(types, paths) {
75
75
  })
76
76
  );
77
77
  }
78
+ var MARKUP_NAME = /\.(?:xml|rels)$/i;
79
+ function markupParts(paths, contentTypes) {
80
+ const named = /* @__PURE__ */ new Set();
81
+ const rest = [];
82
+ for (const path of paths) {
83
+ if (MARKUP_NAME.test(path)) named.add(path);
84
+ else rest.push(path);
85
+ }
86
+ if (contentTypes === void 0 || rest.length === 0) return named;
87
+ try {
88
+ for (const path of declaredXmlParts(contentTypes, rest)) named.add(path);
89
+ } catch {
90
+ }
91
+ return named;
92
+ }
78
93
  function contentTypeWriter(parts) {
79
94
  const overrides = /* @__PURE__ */ new Map();
80
95
  const defaults = /* @__PURE__ */ new Map();
@@ -130,6 +145,7 @@ export {
130
145
  availablePartPath,
131
146
  contentTypeWriter,
132
147
  declaredXmlParts,
148
+ markupParts,
133
149
  readPart,
134
150
  relatedPartPath
135
151
  };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Spelling a whole package under the prefixes this editor writes, as it is opened.
3
+ * `spec/notes/conformance.md` holds the decision and what it rests on.
4
+ *
5
+ * Every reader and every writer re-reads its part out of the one map the session holds, so the
6
+ * rewrite belongs to that map rather than to any one of them: the main part, the comments, the
7
+ * notes, each header and footer and the numbering then all arrive spelled alike.
8
+ *
9
+ * Which parts hold markup is `docx/packageParts`' answer, the one the export reads the parts it
10
+ * wrote back by. A part that comes back unrewritten is never re-encoded.
11
+ */
12
+ export declare function normalizePackagePrefixes(parts: Map<string, Uint8Array>): void;
@@ -0,0 +1,18 @@
1
+ // src/docx/packagePrefixes.ts
2
+ import { withEditorPrefixes } from "../ooxml/partPrefixes.js";
3
+ import { decodeUtf8Strictly, encodeUtf8 } from "../ooxml/xml.js";
4
+ import { CONTENT_TYPES_PATH, markupParts } from "./packageParts.js";
5
+ function normalizePackagePrefixes(parts) {
6
+ const markup = markupParts(parts.keys(), parts.get(CONTENT_TYPES_PATH));
7
+ for (const [path, bytes] of parts) {
8
+ if (!markup.has(path)) continue;
9
+ const decoded = decodeUtf8Strictly(bytes);
10
+ if (decoded === null) continue;
11
+ const rewritten = withEditorPrefixes(decoded.text);
12
+ if (rewritten !== decoded.text)
13
+ parts.set(path, encodeUtf8(rewritten, decoded.hadBom));
14
+ }
15
+ }
16
+ export {
17
+ normalizePackagePrefixes
18
+ };
@@ -3,7 +3,7 @@ import { DocxExportError } from "../ooxml/errors.js";
3
3
  import { decodeUtf8, parseXml } from "../ooxml/xml.js";
4
4
  import {
5
5
  CONTENT_TYPES_PATH,
6
- declaredXmlParts
6
+ markupParts
7
7
  } from "./packageParts.js";
8
8
  import { relsPathOf } from "./relationships.js";
9
9
  function runPartPlanners(planners, doc, session, context, prior = /* @__PURE__ */ new Map()) {
@@ -36,12 +36,10 @@ function runPartPlanners(planners, doc, session, context, prior = /* @__PURE__ *
36
36
  }
37
37
  return parts;
38
38
  }
39
- var XML_PART = /\.(?:xml|rels)$/i;
40
39
  function assertPartsParse(replacements, contentTypes) {
41
- const paths = [...replacements.keys()];
42
- const declared = contentTypes && paths.some((path) => !XML_PART.test(path)) ? declaredXmlParts(contentTypes, paths) : /* @__PURE__ */ new Set();
40
+ const markup = markupParts(replacements.keys(), contentTypes);
43
41
  for (const [path, bytes] of replacements) {
44
- if (!XML_PART.test(path) && !declared.has(path)) continue;
42
+ if (!markup.has(path)) continue;
45
43
  try {
46
44
  parseXml(decodeUtf8(bytes).text);
47
45
  } catch (cause) {
@@ -7,22 +7,92 @@
7
7
  * and stands last. Everything else is null, which leaves whatever held the control preserved
8
8
  * together with its original fragment.
9
9
  *
10
- * What sits inside the control is the caller's business. A whole cell (`docx/importTable`) and
11
- * a stretch of runs inside a paragraph (`docx/importParagraph`) each read the content their
12
- * own way, and the wrapper they put back on export is the same string in both cases.
10
+ * What sits inside the control is the caller's business. A whole cell or row
11
+ * (`docx/importTable`), the blocks under a body or a cell (`docx/importSdtBlock`) and a stretch of
12
+ * runs inside a paragraph (`docx/importParagraph`) each read the content their own way, and the
13
+ * wrapper they put back on export is the same string in all of them.
13
14
  */
14
- /** The opening of a content control taken apart, and the content it wraps */
15
- export interface SdtWrapper {
15
+ import type { Node as PMNode } from "prosemirror-model";
16
+ import type { ControlFacts, ControlFlags } from "../schema/controlAttrs";
17
+ /**
18
+ * What a control's `w:sdtPr` states, which is all the editor reads off a control besides the XML
19
+ * it goes back out as. The carriers write it under names of their own (`schema/controlAttrs`).
20
+ */
21
+ export type SdtFacts = ControlFlags;
22
+ /** The opening of a content control taken apart, the content it wraps, and what it says */
23
+ export interface SdtWrapper extends SdtFacts {
16
24
  /** The `<w:sdt>` opening tag followed by everything that stood ahead of `<w:sdtContent>` */
17
25
  prefix: string;
18
26
  content: Element;
19
- /** Whether the control says its contents may not be edited */
20
- contentsLocked: boolean;
21
- /** Whether the control says it may not be deleted, not even whole */
22
- deletionLocked: boolean;
23
27
  }
24
28
  /** Takes a `w:sdt` apart into the wrapper to put back on export and the content to read. null for a shape we do not write ourselves */
25
29
  export declare function readSdtWrapper(el: Element): SdtWrapper | null;
30
+ /**
31
+ * A `w:sdt` that carries no `w:sdtContent` at all, as the facts its carrier records. null for
32
+ * anything else, this editor's own shape of control included.
33
+ *
34
+ * `CT_Sdt` writes the content element `minOccurs="0"`, so a control stating that what it stood
35
+ * around is not there may leave it out entirely, and §17.5.2.34 makes that element a cache of
36
+ * contents rather than the statement itself. `readSdtWrapper` turns such a control down and stays
37
+ * that way: the inline mark and a wrapped cell or row all read the content it hands back, and
38
+ * there is none here for them to stand around. Everything the control carries, `w:sdtEndPr`
39
+ * included, rides in the prefix, exactly as it does when a content element follows it.
40
+ */
41
+ export declare function readEmptySdt(el: Element): ControlFacts | null;
42
+ /**
43
+ * What the carrier of this control records about it (`schema/controlAttrs`).
44
+ *
45
+ * Every carrier derives it from the wrapper here rather than from the element again, so the inline
46
+ * mark, the block container, the node a control holding nothing stands as, a wrapped cell and a
47
+ * wrapped row cannot come to disagree about what one and the same `w:sdtPr` says.
48
+ */
49
+ export declare function controlFactsFrom(wrapper: SdtWrapper): ControlFacts;
50
+ /**
51
+ * What one `w:sdt` holds, which is the question the block reader and the inline reader must
52
+ * answer the same way.
53
+ *
54
+ * A control holding nothing takes two shapes - an empty `w:sdtContent`, and no content element at
55
+ * all, which §17.5.2.34 admits because that element is a cache of what stood inside rather than
56
+ * the statement itself - and the two state the same thing. They are told apart here rather than in
57
+ * each reader, so neither level can come to read one of them as content and the other as nothing.
58
+ */
59
+ export type SdtContents = {
60
+ kind: "nothing";
61
+ facts: ControlFacts;
62
+ } | {
63
+ kind: "content";
64
+ wrapper: SdtWrapper;
65
+ };
66
+ /** What this control holds, and null for a shape this editor does not write back */
67
+ export declare function readSdtContents(el: Element): SdtContents | null;
68
+ /**
69
+ * Whether this control's content may be read as a container of blocks, which is read off the
70
+ * `w:sdtPr` alone so that a control kept whole never has its prefix serialized.
71
+ *
72
+ * Every other type - `w:richText`, which is also what a control naming no type is, `w:group`, the
73
+ * gallery and field types, and the extension types a closed `CT_SdtPr` can only carry as ignorable
74
+ * markup - says what the control means rather than what it may hold.
75
+ */
76
+ export declare function modelsBlockContent(el: Element): boolean;
77
+ /**
78
+ * The `w:sdtContent` this editor writes itself, and the control it closes after it.
79
+ *
80
+ * `readSdtWrapper` takes back apart exactly this shape, so the two stand together: a control whose
81
+ * content tag carried an attribute, or did not stand last, is one this never writes.
82
+ */
83
+ export declare const SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
84
+ /** The control's opening tag as it arrived, up to the content tag. A wrapper that lost it cannot go out */
85
+ export declare function sdtOpeningXml(prefix: unknown): string;
86
+ /** One whole control: the opening it arrived with, around content this writer owns */
87
+ export declare function sdtXml(prefix: unknown, content: string): string;
88
+ /**
89
+ * A control holding nothing, wherever it stood: the opening it arrived with around a content tag
90
+ * with nothing in it.
91
+ *
92
+ * A control that arrived writing no content element at all gains one here: §17.5.2.34 makes that
93
+ * element a cache of what stood inside, so the two shapes state the same thing.
94
+ */
95
+ export declare function serializeEmptyControl(node: PMNode): string;
26
96
  /**
27
97
  * The number Word writes on every control. Nothing reads it back and it only has to differ
28
98
  * from the other controls in the document, so a draw out of the whole 32 bit range is enough.