@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
@@ -13,6 +13,12 @@ import {
13
13
  toTableFormat,
14
14
  toTableWidth
15
15
  } from "../model/format.js";
16
+ import {
17
+ ANY_ELEMENT,
18
+ ATTRIBUTES,
19
+ acceptRawXml,
20
+ ELEMENT
21
+ } from "../ooxml/fragment.js";
16
22
  import { toImageExtent, toImageSrc } from "../ooxml/image.js";
17
23
  import { editorAttributes, editorClassNames } from "../styles/classNames.js";
18
24
  import { DEFAULT_FONT_FALLBACKS } from "../styles/fontStack.js";
@@ -25,6 +31,9 @@ import {
25
31
  } from "../styles/inlineStyle.js";
26
32
  import { imageNodeSpec, runMarkSpec } from "./rendering.js";
27
33
  import { imageNodeSpec as imageNodeSpec2, runMarkSpec as runMarkSpec2 } from "./rendering.js";
34
+ function isRecord(value) {
35
+ return typeof value === "object" && value !== null;
36
+ }
28
37
  function text(value) {
29
38
  return typeof value === "string" ? value : void 0;
30
39
  }
@@ -60,9 +69,30 @@ function isPageBreak(brAttrs) {
60
69
  }
61
70
  function srcIdOf(dom) {
62
71
  const raw = dom.getAttribute("data-src");
63
- if (raw === null) return null;
64
- const parsed = Number.parseInt(raw, 10);
65
- return Number.isNaN(parsed) ? null : parsed;
72
+ return raw === null || raw === "" ? null : raw;
73
+ }
74
+ function repliesHoldTheirXml(value) {
75
+ if (!Array.isArray(value)) return true;
76
+ const replies = value;
77
+ return replies.every((reply) => {
78
+ if (!isRecord(reply)) return true;
79
+ return acceptRawXml(ELEMENT("comment"), text(reply.commentXml) ?? null) !== false && acceptRawXml(ANY_ELEMENT, text(reply.extensionXml) ?? null) !== false;
80
+ });
81
+ }
82
+ var SDT_PREFIX = {
83
+ kind: "openTag",
84
+ name: "sdt",
85
+ closedBy: "<w:sdtContent/></w:sdt>",
86
+ head: ["sdtPr", "sdtEndPr"]
87
+ };
88
+ var LINK_PREFIX = {
89
+ kind: "openTag",
90
+ name: "hyperlink",
91
+ closedBy: "</w:hyperlink>",
92
+ head: []
93
+ };
94
+ function rawXml(dom, attribute, shape) {
95
+ return acceptRawXml(shape, dom.getAttribute(attribute));
66
96
  }
67
97
  var TABLE_PLACEHOLDER = "Table (unsupported layout, original is preserved)";
68
98
  var UNKNOWN_PLACEHOLDER = "Unsupported content (not editable, original is preserved)";
@@ -84,7 +114,7 @@ var docxSchema = new Schema({
84
114
  nodes: {
85
115
  doc: { content: "block+" },
86
116
  paragraph: {
87
- group: "block",
117
+ group: "block modelled",
88
118
  content: "inline*",
89
119
  // Leading and trailing whitespace carries meaning in docx text, so it is not stripped when the DOM is read back
90
120
  whitespace: "pre",
@@ -94,13 +124,11 @@ var docxSchema = new Schema({
94
124
  pAttrs: { default: null },
95
125
  /** The whole `<w:pPr>...</w:pPr>` XML. null when there is none */
96
126
  pPr: { default: null },
97
- /** The display values derived from reading pPr */
127
+ /** Derived paragraph formatting; see `./attrRoles`. */
98
128
  format: { default: null },
99
129
  /**
100
- * The character formatting the style this paragraph wears lays down, drawn as the
101
- * paragraph's own CSS so that text carrying no run of its own inherits it.
102
- * It is derived from the style table the same way `format` is, and like `format` it never
103
- * goes back into the document.
130
+ * Derived character formatting drawn on the paragraph so unmarked text inherits it.
131
+ * Its comparison role and provenance are declared in `./attrRoles`.
104
132
  */
105
133
  styleRun: { default: null }
106
134
  },
@@ -112,7 +140,7 @@ var docxSchema = new Schema({
112
140
  {
113
141
  class: editorClassNames.paragraph,
114
142
  style: paragraphStyle(format, styleRun),
115
- "data-src": node.attrs.srcId === null ? void 0 : `${node.attrs.srcId}`,
143
+ "data-src": text(node.attrs.srcId),
116
144
  "data-pattrs": text(node.attrs.pAttrs),
117
145
  "data-ppr": text(node.attrs.pPr),
118
146
  "data-fmt": formatJson(format),
@@ -125,15 +153,22 @@ var docxSchema = new Schema({
125
153
  parseDOM: [
126
154
  {
127
155
  tag: `p.${editorClassNames.paragraph}`,
128
- getAttrs: (dom) => ({
129
- srcId: srcIdOf(dom),
130
- pAttrs: dom.getAttribute("data-pattrs"),
131
- pPr: dom.getAttribute("data-ppr"),
132
- format: toParagraphFormat(parseJson(dom.getAttribute("data-fmt"))),
133
- styleRun: toRunFormat(
134
- parseJson(dom.getAttribute("data-style-run"))
135
- )
136
- })
156
+ getAttrs: (dom) => {
157
+ const pAttrs = rawXml(dom, "data-pattrs", ATTRIBUTES);
158
+ const pPr = rawXml(dom, "data-ppr", ELEMENT("pPr"));
159
+ if (pAttrs === false || pPr === false) return false;
160
+ return {
161
+ srcId: srcIdOf(dom),
162
+ pAttrs,
163
+ pPr,
164
+ format: toParagraphFormat(
165
+ parseJson(dom.getAttribute("data-fmt"))
166
+ ),
167
+ styleRun: toRunFormat(
168
+ parseJson(dom.getAttribute("data-style-run"))
169
+ )
170
+ };
171
+ }
137
172
  }
138
173
  ]
139
174
  },
@@ -142,7 +177,7 @@ var docxSchema = new Schema({
142
177
  * edited table is written back the new XML is built from those two.
143
178
  */
144
179
  table: {
145
- group: "block",
180
+ group: "block modelled",
146
181
  content: "tableRow+",
147
182
  tableRole: "table",
148
183
  isolating: true,
@@ -156,6 +191,13 @@ var docxSchema = new Schema({
156
191
  tblW: { default: null },
157
192
  /** The `w:gridCol` widths (dxa) in order */
158
193
  gridCols: { default: [] },
194
+ /**
195
+ * The whole `<w:tblGridChange>...</w:tblGridChange>` XML, the record of the grid this
196
+ * table had before it was last revised. Carried as it arrived because the grid around it
197
+ * is rebuilt from `gridCols`, and CT_TblGrid takes it after the columns however wide
198
+ * those turn out to be (ECMA-376 Part 1 17.4.48)
199
+ */
200
+ gridChange: { default: null },
159
201
  format: { default: null },
160
202
  /** The lines between cells the table style laid down, so an edit can derive them again */
161
203
  styleInside: { default: null },
@@ -169,11 +211,12 @@ var docxSchema = new Schema({
169
211
  const attrs = {
170
212
  class: editorClassNames.table,
171
213
  style: tableStyle(format, width),
172
- "data-src": node.attrs.srcId === null ? void 0 : `${node.attrs.srcId}`,
214
+ "data-src": text(node.attrs.srcId),
173
215
  "data-tblattrs": text(node.attrs.tblAttrs),
174
216
  "data-tblpr": text(node.attrs.tblPr),
175
217
  "data-tblw": formatJson(width),
176
218
  "data-cols": numberListText(gridCols),
219
+ "data-gridchange": text(node.attrs.gridChange),
177
220
  "data-fmt": formatJson(format),
178
221
  "data-style-inside": formatJson(
179
222
  toInsideBorders(node.attrs.styleInside)
@@ -195,20 +238,33 @@ var docxSchema = new Schema({
195
238
  { tag: "colgroup", ignore: true },
196
239
  {
197
240
  tag: `table.${editorClassNames.table}`,
198
- getAttrs: (dom) => ({
199
- srcId: srcIdOf(dom),
200
- tblAttrs: dom.getAttribute("data-tblattrs"),
201
- tblPr: dom.getAttribute("data-tblpr"),
202
- tblW: toTableWidth(parseJson(dom.getAttribute("data-tblw"))),
203
- gridCols: parseNumberList(dom.getAttribute("data-cols")),
204
- format: toTableFormat(parseJson(dom.getAttribute("data-fmt"))),
205
- styleInside: toInsideBorders(
206
- parseJson(dom.getAttribute("data-style-inside"))
207
- ),
208
- styleCellMargins: toCellMargins(
209
- parseJson(dom.getAttribute("data-style-margins"))
210
- )
211
- })
241
+ getAttrs: (dom) => {
242
+ const tblAttrs = rawXml(dom, "data-tblattrs", ATTRIBUTES);
243
+ const tblPr = rawXml(dom, "data-tblpr", ELEMENT("tblPr"));
244
+ const gridChange = rawXml(
245
+ dom,
246
+ "data-gridchange",
247
+ ELEMENT("tblGridChange")
248
+ );
249
+ if (tblAttrs === false || tblPr === false || gridChange === false) {
250
+ return false;
251
+ }
252
+ return {
253
+ srcId: srcIdOf(dom),
254
+ tblAttrs,
255
+ tblPr,
256
+ tblW: toTableWidth(parseJson(dom.getAttribute("data-tblw"))),
257
+ gridCols: parseNumberList(dom.getAttribute("data-cols")),
258
+ gridChange,
259
+ format: toTableFormat(parseJson(dom.getAttribute("data-fmt"))),
260
+ styleInside: toInsideBorders(
261
+ parseJson(dom.getAttribute("data-style-inside"))
262
+ ),
263
+ styleCellMargins: toCellMargins(
264
+ parseJson(dom.getAttribute("data-style-margins"))
265
+ )
266
+ };
267
+ }
212
268
  }
213
269
  ]
214
270
  },
@@ -242,12 +298,20 @@ var docxSchema = new Schema({
242
298
  parseDOM: [
243
299
  {
244
300
  tag: "tr",
245
- getAttrs: (dom) => ({
246
- trAttrs: dom.getAttribute("data-trattrs"),
247
- tblPrEx: dom.getAttribute("data-tblprex"),
248
- trPr: dom.getAttribute("data-trpr"),
249
- format: toRowFormat(parseJson(dom.getAttribute("data-fmt")))
250
- })
301
+ getAttrs: (dom) => {
302
+ const trAttrs = rawXml(dom, "data-trattrs", ATTRIBUTES);
303
+ const tblPrEx = rawXml(dom, "data-tblprex", ELEMENT("tblPrEx"));
304
+ const trPr = rawXml(dom, "data-trpr", ELEMENT("trPr"));
305
+ if (trAttrs === false || tblPrEx === false || trPr === false) {
306
+ return false;
307
+ }
308
+ return {
309
+ trAttrs,
310
+ tblPrEx,
311
+ trPr,
312
+ format: toRowFormat(parseJson(dom.getAttribute("data-fmt")))
313
+ };
314
+ }
251
315
  }
252
316
  ]
253
317
  },
@@ -309,20 +373,28 @@ var docxSchema = new Schema({
309
373
  parseDOM: [
310
374
  {
311
375
  tag: "td",
312
- getAttrs: (dom) => ({
313
- colspan: parseInt10(dom.getAttribute("colspan"), 1),
314
- rowspan: parseInt10(dom.getAttribute("rowspan"), 1),
315
- colwidth: toColWidth(
316
- parseNumberList(dom.getAttribute("data-colwidth"))
317
- ),
318
- tcAttrs: dom.getAttribute("data-tcattrs"),
319
- tcPr: dom.getAttribute("data-tcpr"),
320
- tcW: toTableWidth(parseJson(dom.getAttribute("data-tcw"))),
321
- format: toCellFormat(parseJson(dom.getAttribute("data-fmt"))),
322
- sdtPrefix: dom.getAttribute("data-sdt-prefix"),
323
- sdtContentsLocked: dom.getAttribute("data-sdt-contents-locked") === "1",
324
- sdtDeletionLocked: dom.getAttribute("data-sdt-deletion-locked") === "1"
325
- })
376
+ getAttrs: (dom) => {
377
+ const tcAttrs = rawXml(dom, "data-tcattrs", ATTRIBUTES);
378
+ const tcPr = rawXml(dom, "data-tcpr", ELEMENT("tcPr"));
379
+ const sdtPrefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
380
+ if (tcAttrs === false || tcPr === false || sdtPrefix === false) {
381
+ return false;
382
+ }
383
+ return {
384
+ colspan: parseInt10(dom.getAttribute("colspan"), 1),
385
+ rowspan: parseInt10(dom.getAttribute("rowspan"), 1),
386
+ colwidth: toColWidth(
387
+ parseNumberList(dom.getAttribute("data-colwidth"))
388
+ ),
389
+ tcAttrs,
390
+ tcPr,
391
+ tcW: toTableWidth(parseJson(dom.getAttribute("data-tcw"))),
392
+ format: toCellFormat(parseJson(dom.getAttribute("data-fmt"))),
393
+ sdtPrefix,
394
+ sdtContentsLocked: dom.getAttribute("data-sdt-contents-locked") === "1",
395
+ sdtDeletionLocked: dom.getAttribute("data-sdt-deletion-locked") === "1"
396
+ };
397
+ }
326
398
  }
327
399
  ]
328
400
  },
@@ -333,7 +405,7 @@ var docxSchema = new Schema({
333
405
  * XML around with it.
334
406
  */
335
407
  rawBlock: {
336
- group: "block",
408
+ group: "block preserved",
337
409
  atom: true,
338
410
  selectable: false,
339
411
  attrs: {
@@ -355,16 +427,17 @@ var docxSchema = new Schema({
355
427
  parseDOM: [
356
428
  {
357
429
  tag: `div.${editorClassNames.rawXmlBlock}`,
358
- getAttrs: (dom) => ({
359
- xml: dom.getAttribute("data-xml"),
360
- name: dom.getAttribute("data-name")
361
- })
430
+ getAttrs: (dom) => {
431
+ const xml = rawXml(dom, "data-xml", ANY_ELEMENT);
432
+ if (xml === false) return false;
433
+ return { xml, name: dom.getAttribute("data-name") };
434
+ }
362
435
  }
363
436
  ]
364
437
  },
365
438
  /** The node that carries a non-paragraph body block (a table we could not model, sectPr, an unknown element) exactly as it came */
366
439
  docxRaw: {
367
- group: "block",
440
+ group: "block preserved",
368
441
  atom: true,
369
442
  selectable: false,
370
443
  attrs: {
@@ -378,7 +451,7 @@ var docxSchema = new Schema({
378
451
  "div",
379
452
  {
380
453
  class: className,
381
- "data-src": node.attrs.srcId === null ? "" : `${node.attrs.srcId}`,
454
+ "data-src": text(node.attrs.srcId) ?? "",
382
455
  "data-name": text(node.attrs.name)
383
456
  },
384
457
  label
@@ -396,7 +469,7 @@ var docxSchema = new Schema({
396
469
  },
397
470
  /** A bookmark range marker that occurs directly under w:body rather than inside a paragraph */
398
471
  bookmarkBlock: {
399
- group: "block",
472
+ group: "block preserved",
400
473
  atom: true,
401
474
  isolating: true,
402
475
  selectable: false,
@@ -409,7 +482,7 @@ var docxSchema = new Schema({
409
482
  "div",
410
483
  {
411
484
  class: editorClassNames.bookmarkBlock,
412
- "data-src": node.attrs.srcId === null ? "" : `${node.attrs.srcId}`,
485
+ "data-src": text(node.attrs.srcId) ?? "",
413
486
  "data-name": text(node.attrs.name),
414
487
  hidden: "hidden"
415
488
  }
@@ -442,7 +515,10 @@ var docxSchema = new Schema({
442
515
  parseDOM: [
443
516
  {
444
517
  tag: "br",
445
- getAttrs: (dom) => ({ brAttrs: dom.getAttribute("data-battrs") })
518
+ getAttrs: (dom) => {
519
+ const brAttrs = rawXml(dom, "data-battrs", ATTRIBUTES);
520
+ return brAttrs === false ? false : { brAttrs };
521
+ }
446
522
  }
447
523
  ]
448
524
  },
@@ -475,12 +551,16 @@ var docxSchema = new Schema({
475
551
  parseDOM: [
476
552
  {
477
553
  tag: `img.${editorClassNames.image}`,
478
- getAttrs: (dom) => ({
479
- src: toImageSrc(dom.getAttribute("src")),
480
- extent: toImageExtent(parseJson(dom.getAttribute("data-extent"))),
481
- alt: dom.getAttribute("alt") || null,
482
- xml: dom.getAttribute("data-xml")
483
- })
554
+ getAttrs: (dom) => {
555
+ const xml = rawXml(dom, "data-xml", ELEMENT("drawing"));
556
+ if (xml === false) return false;
557
+ return {
558
+ src: toImageSrc(dom.getAttribute("src")),
559
+ extent: toImageExtent(parseJson(dom.getAttribute("data-extent"))),
560
+ alt: dom.getAttribute("alt") || null,
561
+ xml
562
+ };
563
+ }
484
564
  }
485
565
  ]
486
566
  },
@@ -508,10 +588,11 @@ var docxSchema = new Schema({
508
588
  parseDOM: [
509
589
  {
510
590
  tag: `span.${editorClassNames.commentMarker}[data-comment-marker="start"]`,
511
- getAttrs: (dom) => ({
512
- id: dom.getAttribute("data-comment-id"),
513
- xml: dom.getAttribute("data-xml")
514
- })
591
+ getAttrs: (dom) => {
592
+ const xml = rawXml(dom, "data-xml", ELEMENT("commentRangeStart"));
593
+ if (xml === false) return false;
594
+ return { id: dom.getAttribute("data-comment-id"), xml };
595
+ }
515
596
  }
516
597
  ]
517
598
  },
@@ -539,10 +620,11 @@ var docxSchema = new Schema({
539
620
  parseDOM: [
540
621
  {
541
622
  tag: `span.${editorClassNames.commentMarker}[data-comment-marker="end"]`,
542
- getAttrs: (dom) => ({
543
- id: dom.getAttribute("data-comment-id"),
544
- xml: dom.getAttribute("data-xml")
545
- })
623
+ getAttrs: (dom) => {
624
+ const xml = rawXml(dom, "data-xml", ELEMENT("commentRangeEnd"));
625
+ if (xml === false) return false;
626
+ return { id: dom.getAttribute("data-comment-id"), xml };
627
+ }
546
628
  }
547
629
  ]
548
630
  },
@@ -596,22 +678,43 @@ var docxSchema = new Schema({
596
678
  parseDOM: [
597
679
  {
598
680
  tag: `span.${editorClassNames.commentMarker}[data-comment-marker="reference"]`,
599
- getAttrs: (dom) => ({
600
- id: dom.getAttribute("data-comment-id"),
601
- referenceXml: dom.getAttribute("data-reference-xml"),
602
- author: dom.getAttribute("data-comment-author"),
603
- authorId: dom.getAttribute("data-comment-author-id"),
604
- initials: dom.getAttribute("data-comment-initials"),
605
- date: dom.getAttribute("data-comment-date"),
606
- text: dom.getAttribute("data-comment-text") ?? "",
607
- commentXml: dom.getAttribute("data-comment-xml"),
608
- imported: dom.getAttribute("data-comment-imported") === "1",
609
- paraId: dom.getAttribute("data-comment-para-id"),
610
- resolved: dom.getAttribute("data-comment-resolved") === "1",
611
- extensionXml: dom.getAttribute("data-comment-extension-xml"),
612
- threadImported: dom.getAttribute("data-comment-thread-imported") === "1",
613
- replies: parseJson(dom.getAttribute("data-comment-replies")) ?? []
614
- })
681
+ getAttrs: (dom) => {
682
+ const referenceXml = rawXml(
683
+ dom,
684
+ "data-reference-xml",
685
+ ELEMENT("commentReference")
686
+ );
687
+ const commentXml = rawXml(
688
+ dom,
689
+ "data-comment-xml",
690
+ ELEMENT("comment")
691
+ );
692
+ const extensionXml = rawXml(
693
+ dom,
694
+ "data-comment-extension-xml",
695
+ ANY_ELEMENT
696
+ );
697
+ const replies = parseJson(dom.getAttribute("data-comment-replies")) ?? [];
698
+ if (referenceXml === false || commentXml === false || extensionXml === false || !repliesHoldTheirXml(replies)) {
699
+ return false;
700
+ }
701
+ return {
702
+ id: dom.getAttribute("data-comment-id"),
703
+ referenceXml,
704
+ author: dom.getAttribute("data-comment-author"),
705
+ authorId: dom.getAttribute("data-comment-author-id"),
706
+ initials: dom.getAttribute("data-comment-initials"),
707
+ date: dom.getAttribute("data-comment-date"),
708
+ text: dom.getAttribute("data-comment-text") ?? "",
709
+ commentXml,
710
+ imported: dom.getAttribute("data-comment-imported") === "1",
711
+ paraId: dom.getAttribute("data-comment-para-id"),
712
+ resolved: dom.getAttribute("data-comment-resolved") === "1",
713
+ extensionXml,
714
+ threadImported: dom.getAttribute("data-comment-thread-imported") === "1",
715
+ replies
716
+ };
717
+ }
615
718
  }
616
719
  ]
617
720
  },
@@ -652,14 +755,22 @@ var docxSchema = new Schema({
652
755
  parseDOM: [
653
756
  {
654
757
  tag: `sup.${editorClassNames.noteReference}`,
655
- getAttrs: (dom) => ({
656
- kind: dom.getAttribute("data-note-kind") === "endnote" ? "endnote" : "footnote",
657
- id: dom.getAttribute("data-note-id"),
658
- label: dom.getAttribute("data-note-label") ?? "?",
659
- text: dom.getAttribute("data-note-text") ?? "",
660
- customMarkFollows: dom.getAttribute("data-custom-mark-follows") === "1",
661
- referenceXml: dom.getAttribute("data-reference-xml")
662
- })
758
+ getAttrs: (dom) => {
759
+ const referenceXml = rawXml(
760
+ dom,
761
+ "data-reference-xml",
762
+ ELEMENT("footnoteReference", "endnoteReference")
763
+ );
764
+ if (referenceXml === false) return false;
765
+ return {
766
+ kind: dom.getAttribute("data-note-kind") === "endnote" ? "endnote" : "footnote",
767
+ id: dom.getAttribute("data-note-id"),
768
+ label: dom.getAttribute("data-note-label") ?? "?",
769
+ text: dom.getAttribute("data-note-text") ?? "",
770
+ customMarkFollows: dom.getAttribute("data-custom-mark-follows") === "1",
771
+ referenceXml
772
+ };
773
+ }
663
774
  }
664
775
  ]
665
776
  },
@@ -682,7 +793,10 @@ var docxSchema = new Schema({
682
793
  parseDOM: [
683
794
  {
684
795
  tag: `span.${editorClassNames.rawInline}`,
685
- getAttrs: (dom) => ({ xml: dom.getAttribute("data-xml") })
796
+ getAttrs: (dom) => {
797
+ const xml = rawXml(dom, "data-xml", ANY_ELEMENT);
798
+ return xml === false ? false : { xml };
799
+ }
686
800
  }
687
801
  ]
688
802
  },
@@ -737,8 +851,8 @@ var docxSchema = new Schema({
737
851
  {
738
852
  tag: `span.${editorClassNames.sdt}`,
739
853
  getAttrs: (dom) => {
740
- const prefix = dom.getAttribute("data-sdt-prefix");
741
- if (prefix === null) return false;
854
+ const prefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
855
+ if (prefix === null || prefix === false) return false;
742
856
  return {
743
857
  sdtPrefix: prefix,
744
858
  sdtKey: parseInt10(dom.getAttribute("data-sdt-key"), 0),
@@ -800,8 +914,9 @@ var docxSchema = new Schema({
800
914
  {
801
915
  tag: `span.${editorClassNames.link}`,
802
916
  getAttrs: (dom) => {
803
- const prefix = dom.getAttribute("data-link-prefix");
917
+ const prefix = rawXml(dom, "data-link-prefix", LINK_PREFIX);
804
918
  const href = dom.getAttribute("data-href");
919
+ if (prefix === false) return false;
805
920
  if (prefix === null && href === null) return false;
806
921
  return {
807
922
  linkPrefix: prefix,
@@ -820,7 +935,7 @@ var docxSchema = new Schema({
820
935
  attrs: {
821
936
  rPr: { default: null },
822
937
  rAttrs: { default: null },
823
- /** The display values derived from reading rPr */
938
+ /** Derived run formatting; see `./attrRoles`. */
824
939
  format: { default: null }
825
940
  },
826
941
  toDOM(mark) {
@@ -829,11 +944,16 @@ var docxSchema = new Schema({
829
944
  parseDOM: [
830
945
  {
831
946
  tag: `span.${editorClassNames.run}`,
832
- getAttrs: (dom) => ({
833
- rAttrs: dom.getAttribute("data-rattrs"),
834
- rPr: dom.getAttribute("data-rpr"),
835
- format: toRunFormat(parseJson(dom.getAttribute("data-fmt")))
836
- })
947
+ getAttrs: (dom) => {
948
+ const rAttrs = rawXml(dom, "data-rattrs", ATTRIBUTES);
949
+ const rPr = rawXml(dom, "data-rpr", ELEMENT("rPr"));
950
+ if (rAttrs === false || rPr === false) return false;
951
+ return {
952
+ rAttrs,
953
+ rPr,
954
+ format: toRunFormat(parseJson(dom.getAttribute("data-fmt")))
955
+ };
956
+ }
837
957
  }
838
958
  ]
839
959
  },
@@ -854,7 +974,10 @@ var docxSchema = new Schema({
854
974
  parseDOM: [
855
975
  {
856
976
  tag: `span.${editorClassNames.tab}`,
857
- getAttrs: (dom) => ({ tabAttrs: dom.getAttribute("data-tattrs") })
977
+ getAttrs: (dom) => {
978
+ const tabAttrs = rawXml(dom, "data-tattrs", ATTRIBUTES);
979
+ return tabAttrs === false ? false : { tabAttrs };
980
+ }
858
981
  }
859
982
  ]
860
983
  }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * What a guard is, and the reach primitives one is written with.
3
+ *
4
+ * This module names no guard and imports none, so a module that writes a guard (`./locks`,
5
+ * `./preservedGuards`) and the list that registers them (`./guards`) can both read it without
6
+ * pointing at each other. A guard reading its shape off `./guards` instead would put a cycle
7
+ * there: `EDIT_GUARDS` is built while `./guards` is evaluated, so whenever the guard's own module
8
+ * happened to be evaluated first the list would be built out of bindings that had not been
9
+ * assigned yet, and every edit would then be judged against a list holding holes.
10
+ */
11
+ import type { Node as PMNode } from "prosemirror-model";
12
+ import type { EditorState, PluginKey, Transaction } from "prosemirror-state";
13
+ import { type Step } from "prosemirror-transform";
14
+ /**
15
+ * What a command means to do, in as much of it as a guard can answer for before anything is built.
16
+ *
17
+ * The positions are the ones the command would work over, counted in the document as it stands.
18
+ */
19
+ export type EditIntent =
20
+ /** Something goes in at this spot, and nothing there goes away */
21
+ {
22
+ kind: "insert";
23
+ at: number;
24
+ }
25
+ /** What stands in this stretch is marked where it stands */
26
+ | {
27
+ kind: "mark";
28
+ from: number;
29
+ to: number;
30
+ }
31
+ /** What stands in this stretch goes away, whatever takes its place */
32
+ | {
33
+ kind: "replace";
34
+ from: number;
35
+ to: number;
36
+ }
37
+ /** The block at this spot is rewritten around its content, an alignment or an indent */
38
+ | {
39
+ kind: "block";
40
+ at: number;
41
+ };
42
+ /**
43
+ * How a guard is named in the honesty test and in a refusal read by a developer.
44
+ *
45
+ * The names are written out because the registry is a closed list: `lockHonesty` checks each one
46
+ * against the places it puts that guard to the test, and a place naming a guard that no longer
47
+ * stands has to be a mistake the compiler catches rather than an annotation answering for nothing.
48
+ */
49
+ export type EditGuardName = "protection" | "lock" | "bookmark" | "note" | "section";
50
+ /** What every guard answers, whichever of the two judgements it is written as */
51
+ interface GuardCommon {
52
+ readonly name: EditGuardName;
53
+ /** Whether the intent is shut where it stands, which is what a command asks before it builds */
54
+ shuts(intent: EditIntent, state: EditorState): boolean;
55
+ }
56
+ /**
57
+ * A guard that judges one step at a time, over the document that step was built against.
58
+ *
59
+ * Only this form takes a pass. A pass lifts one guard's reading of a step, and a rule that has to
60
+ * see both documents of the change at once has no single step for it to let through.
61
+ */
62
+ export interface StepGuard extends GuardCommon {
63
+ step(step: Step, before: PMNode, after: PMNode, state: EditorState): boolean;
64
+ /** The passes that lift this guard's step judgement. Omitted: none does */
65
+ liftedBy?: readonly PluginKey<boolean>[];
66
+ change?: never;
67
+ }
68
+ /** A guard that needs both documents of the whole change at once */
69
+ export interface ChangeGuard extends GuardCommon {
70
+ change(tr: Transaction, state: EditorState): boolean;
71
+ step?: never;
72
+ liftedBy?: never;
73
+ }
74
+ /**
75
+ * One rule an edit is judged by: whether it would leave the document in a state the file cannot be
76
+ * written back from.
77
+ *
78
+ * A guard is one judgement or the other, and answers `shuts` either way, so there is no writing
79
+ * one that judges nothing and no hanging a pass on a judgement no pass can lift.
80
+ */
81
+ export type EditGuard = StepGuard | ChangeGuard;
82
+ /** Whether this stretch of the document holds a node the question answers for */
83
+ export declare function rangeHolds(doc: PMNode, from: number, to: number, holds: (node: PMNode) => boolean): boolean;
84
+ /**
85
+ * Whether the step reaches a node the question answers for: puts one in, takes one out, or
86
+ * rewrites the one where it stands.
87
+ *
88
+ * A rule about such nodes cannot have been broken by a change that reaches none of them, which is
89
+ * what lets a guard settle the common transaction - typing, and nothing more - over the stretches
90
+ * its own steps rewrote rather than over the whole document.
91
+ *
92
+ * A step of a kind this does not know - one a consumer brought - is answered as reaching one,
93
+ * since what it rewrote is not known either. The whole-document judgement then has the say, and
94
+ * an unknown step costs a comparison rather than a hole in the guard.
95
+ */
96
+ export declare function stepReaches(step: Step, before: PMNode, after: PMNode, holds: (node: PMNode) => boolean): boolean;
97
+ /** Whether any step of the transaction reaches a node the question answers for */
98
+ export declare function transactionReaches(tr: Transaction, holds: (node: PMNode) => boolean): boolean;
99
+ export {};