@portone/docx-editor 0.2.0 → 0.2.1

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 (101) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/CONTRIBUTING.md +4 -0
  3. package/dist/docx/commentOnlyChange.d.ts +3 -1
  4. package/dist/docx/commentOnlyChange.js +38 -21
  5. package/dist/docx/comments/constants.d.ts +3 -3
  6. package/dist/docx/comments/constants.js +4 -3
  7. package/dist/docx/comments/grammar.d.ts +80 -0
  8. package/dist/docx/comments/grammar.js +213 -0
  9. package/dist/docx/comments/people.js +12 -14
  10. package/dist/docx/comments/reading.d.ts +5 -3
  11. package/dist/docx/comments/reading.js +23 -20
  12. package/dist/docx/comments/verifying.d.ts +44 -0
  13. package/dist/docx/comments/verifying.js +206 -0
  14. package/dist/docx/comments/writing.js +73 -46
  15. package/dist/docx/exportDocx.js +2 -1
  16. package/dist/docx/formatting/direct.js +3 -6
  17. package/dist/docx/headersFooters.js +8 -10
  18. package/dist/docx/notes.js +3 -5
  19. package/dist/docx/paraProps.js +51 -46
  20. package/dist/docx/propsXml.d.ts +20 -2
  21. package/dist/docx/propsXml.js +72 -32
  22. package/dist/docx/relationships.js +8 -3
  23. package/dist/docx/runProps.js +34 -34
  24. package/dist/docx/sdt.js +16 -14
  25. package/dist/docx/serializeBlock.js +1 -2
  26. package/dist/docx/serializeParagraph.d.ts +2 -1
  27. package/dist/docx/serializeParagraph.js +25 -14
  28. package/dist/docx/serializeTable.js +38 -24
  29. package/dist/docx/storyProjection.d.ts +36 -0
  30. package/dist/docx/storyProjection.js +23 -0
  31. package/dist/docx/tableFormatting/editing.js +49 -73
  32. package/dist/docx/tableTemplate.js +24 -3
  33. package/dist/docx/theme.d.ts +0 -6
  34. package/dist/docx/theme.js +0 -8
  35. package/dist/editor/clipboard/images.js +17 -3
  36. package/dist/editor/clipboard/inlineFormatting.d.ts +7 -0
  37. package/dist/editor/clipboard/inlineFormatting.js +1 -0
  38. package/dist/editor/commands/breakCommands.js +4 -3
  39. package/dist/editor/commands/canRunCommand.d.ts +2 -2
  40. package/dist/editor/commands/canRunCommand.js +1 -1
  41. package/dist/editor/commands/comments/editing.js +3 -6
  42. package/dist/editor/commands/historyCommands.js +2 -1
  43. package/dist/editor/commands/lockCommands.js +10 -1
  44. package/dist/editor/commands/tabCommands.js +4 -3
  45. package/dist/editor/createEditor.js +3 -9
  46. package/dist/editor/externalClipboard.js +166 -4
  47. package/dist/editor/insertImage.js +4 -3
  48. package/dist/editor/plugins/documentProtection.d.ts +3 -3
  49. package/dist/editor/plugins/lockedContent.d.ts +4 -4
  50. package/dist/editor/plugins/lockedContent.js +1 -1
  51. package/dist/numbering/listTemplate.js +32 -10
  52. package/dist/ooxml/element.d.ts +52 -0
  53. package/dist/ooxml/element.js +49 -0
  54. package/dist/ooxml/fragment.d.ts +53 -0
  55. package/dist/ooxml/fragment.js +76 -0
  56. package/dist/ooxml/image.js +4 -3
  57. package/dist/ooxml/names.d.ts +27 -0
  58. package/dist/ooxml/names.js +29 -0
  59. package/dist/ooxml/precedence.d.ts +31 -0
  60. package/dist/ooxml/precedence.js +51 -0
  61. package/dist/ooxml/xml.d.ts +31 -0
  62. package/dist/ooxml/xml.js +27 -0
  63. package/dist/page/blockKinds.d.ts +42 -0
  64. package/dist/page/blockKinds.js +0 -0
  65. package/dist/page/measureBlocks.d.ts +14 -1
  66. package/dist/page/measureBlocks.js +31 -11
  67. package/dist/page/pageDecorations.d.ts +16 -0
  68. package/dist/page/pageDecorations.js +1 -0
  69. package/dist/page/pageLayout.d.ts +8 -43
  70. package/dist/page/pageLayout.js +28 -51
  71. package/dist/page/tableMeasurements.d.ts +10 -2
  72. package/dist/page/tableMeasurements.js +20 -16
  73. package/dist/page/usePageLayout.d.ts +15 -0
  74. package/dist/page/usePageLayout.js +30 -2
  75. package/dist/schema/attrRoles.d.ts +35 -0
  76. package/dist/schema/attrRoles.js +106 -0
  77. package/dist/schema/docxSchema.d.ts +6 -0
  78. package/dist/schema/docxSchema.js +212 -101
  79. package/dist/schema/editGuard.d.ts +99 -0
  80. package/dist/schema/editGuard.js +43 -0
  81. package/dist/schema/guards.d.ts +68 -0
  82. package/dist/schema/guards.js +82 -0
  83. package/dist/schema/locks.d.ts +8 -35
  84. package/dist/schema/locks.js +29 -64
  85. package/dist/schema/preservedGuards.d.ts +30 -0
  86. package/dist/schema/preservedGuards.js +50 -0
  87. package/dist/schema/protection.d.ts +10 -3
  88. package/dist/schema/protection.js +2 -1
  89. package/dist/schema/sourceEquality.d.ts +27 -0
  90. package/dist/schema/sourceEquality.js +65 -0
  91. package/dist/table/cellFormatting.js +2 -1
  92. package/dist/table/commands.d.ts +2 -2
  93. package/dist/table/commands.js +1 -1
  94. package/dist/table/index.d.ts +2 -2
  95. package/dist/table/merge.d.ts +2 -2
  96. package/dist/table/merge.js +1 -1
  97. package/package.json +4 -1
  98. package/dist/editor/plugins/bookmarkProtection.d.ts +0 -3
  99. package/dist/editor/plugins/bookmarkProtection.js +0 -28
  100. package/dist/editor/plugins/noteProtection.d.ts +0 -3
  101. package/dist/editor/plugins/noteProtection.js +0 -25
@@ -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
  }
@@ -64,6 +73,29 @@ function srcIdOf(dom) {
64
73
  const parsed = Number.parseInt(raw, 10);
65
74
  return Number.isNaN(parsed) ? null : parsed;
66
75
  }
76
+ function repliesHoldTheirXml(value) {
77
+ if (!Array.isArray(value)) return true;
78
+ const replies = value;
79
+ return replies.every((reply) => {
80
+ if (!isRecord(reply)) return true;
81
+ return acceptRawXml(ELEMENT("comment"), text(reply.commentXml) ?? null) !== false && acceptRawXml(ANY_ELEMENT, text(reply.extensionXml) ?? null) !== false;
82
+ });
83
+ }
84
+ var SDT_PREFIX = {
85
+ kind: "openTag",
86
+ name: "sdt",
87
+ closedBy: "<w:sdtContent/></w:sdt>",
88
+ head: ["sdtPr", "sdtEndPr"]
89
+ };
90
+ var LINK_PREFIX = {
91
+ kind: "openTag",
92
+ name: "hyperlink",
93
+ closedBy: "</w:hyperlink>",
94
+ head: []
95
+ };
96
+ function rawXml(dom, attribute, shape) {
97
+ return acceptRawXml(shape, dom.getAttribute(attribute));
98
+ }
67
99
  var TABLE_PLACEHOLDER = "Table (unsupported layout, original is preserved)";
68
100
  var UNKNOWN_PLACEHOLDER = "Unsupported content (not editable, original is preserved)";
69
101
  var TABLE_NAMES = ["w:tbl", "tbl"];
@@ -84,7 +116,7 @@ var docxSchema = new Schema({
84
116
  nodes: {
85
117
  doc: { content: "block+" },
86
118
  paragraph: {
87
- group: "block",
119
+ group: "block modelled",
88
120
  content: "inline*",
89
121
  // Leading and trailing whitespace carries meaning in docx text, so it is not stripped when the DOM is read back
90
122
  whitespace: "pre",
@@ -125,15 +157,22 @@ var docxSchema = new Schema({
125
157
  parseDOM: [
126
158
  {
127
159
  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
- })
160
+ getAttrs: (dom) => {
161
+ const pAttrs = rawXml(dom, "data-pattrs", ATTRIBUTES);
162
+ const pPr = rawXml(dom, "data-ppr", ELEMENT("pPr"));
163
+ if (pAttrs === false || pPr === false) return false;
164
+ return {
165
+ srcId: srcIdOf(dom),
166
+ pAttrs,
167
+ pPr,
168
+ format: toParagraphFormat(
169
+ parseJson(dom.getAttribute("data-fmt"))
170
+ ),
171
+ styleRun: toRunFormat(
172
+ parseJson(dom.getAttribute("data-style-run"))
173
+ )
174
+ };
175
+ }
137
176
  }
138
177
  ]
139
178
  },
@@ -142,7 +181,7 @@ var docxSchema = new Schema({
142
181
  * edited table is written back the new XML is built from those two.
143
182
  */
144
183
  table: {
145
- group: "block",
184
+ group: "block modelled",
146
185
  content: "tableRow+",
147
186
  tableRole: "table",
148
187
  isolating: true,
@@ -195,20 +234,25 @@ var docxSchema = new Schema({
195
234
  { tag: "colgroup", ignore: true },
196
235
  {
197
236
  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
- })
237
+ getAttrs: (dom) => {
238
+ const tblAttrs = rawXml(dom, "data-tblattrs", ATTRIBUTES);
239
+ const tblPr = rawXml(dom, "data-tblpr", ELEMENT("tblPr"));
240
+ if (tblAttrs === false || tblPr === false) return false;
241
+ return {
242
+ srcId: srcIdOf(dom),
243
+ tblAttrs,
244
+ tblPr,
245
+ tblW: toTableWidth(parseJson(dom.getAttribute("data-tblw"))),
246
+ gridCols: parseNumberList(dom.getAttribute("data-cols")),
247
+ format: toTableFormat(parseJson(dom.getAttribute("data-fmt"))),
248
+ styleInside: toInsideBorders(
249
+ parseJson(dom.getAttribute("data-style-inside"))
250
+ ),
251
+ styleCellMargins: toCellMargins(
252
+ parseJson(dom.getAttribute("data-style-margins"))
253
+ )
254
+ };
255
+ }
212
256
  }
213
257
  ]
214
258
  },
@@ -242,12 +286,20 @@ var docxSchema = new Schema({
242
286
  parseDOM: [
243
287
  {
244
288
  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
- })
289
+ getAttrs: (dom) => {
290
+ const trAttrs = rawXml(dom, "data-trattrs", ATTRIBUTES);
291
+ const tblPrEx = rawXml(dom, "data-tblprex", ELEMENT("tblPrEx"));
292
+ const trPr = rawXml(dom, "data-trpr", ELEMENT("trPr"));
293
+ if (trAttrs === false || tblPrEx === false || trPr === false) {
294
+ return false;
295
+ }
296
+ return {
297
+ trAttrs,
298
+ tblPrEx,
299
+ trPr,
300
+ format: toRowFormat(parseJson(dom.getAttribute("data-fmt")))
301
+ };
302
+ }
251
303
  }
252
304
  ]
253
305
  },
@@ -309,20 +361,28 @@ var docxSchema = new Schema({
309
361
  parseDOM: [
310
362
  {
311
363
  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
- })
364
+ getAttrs: (dom) => {
365
+ const tcAttrs = rawXml(dom, "data-tcattrs", ATTRIBUTES);
366
+ const tcPr = rawXml(dom, "data-tcpr", ELEMENT("tcPr"));
367
+ const sdtPrefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
368
+ if (tcAttrs === false || tcPr === false || sdtPrefix === false) {
369
+ return false;
370
+ }
371
+ return {
372
+ colspan: parseInt10(dom.getAttribute("colspan"), 1),
373
+ rowspan: parseInt10(dom.getAttribute("rowspan"), 1),
374
+ colwidth: toColWidth(
375
+ parseNumberList(dom.getAttribute("data-colwidth"))
376
+ ),
377
+ tcAttrs,
378
+ tcPr,
379
+ tcW: toTableWidth(parseJson(dom.getAttribute("data-tcw"))),
380
+ format: toCellFormat(parseJson(dom.getAttribute("data-fmt"))),
381
+ sdtPrefix,
382
+ sdtContentsLocked: dom.getAttribute("data-sdt-contents-locked") === "1",
383
+ sdtDeletionLocked: dom.getAttribute("data-sdt-deletion-locked") === "1"
384
+ };
385
+ }
326
386
  }
327
387
  ]
328
388
  },
@@ -333,7 +393,7 @@ var docxSchema = new Schema({
333
393
  * XML around with it.
334
394
  */
335
395
  rawBlock: {
336
- group: "block",
396
+ group: "block preserved",
337
397
  atom: true,
338
398
  selectable: false,
339
399
  attrs: {
@@ -355,16 +415,17 @@ var docxSchema = new Schema({
355
415
  parseDOM: [
356
416
  {
357
417
  tag: `div.${editorClassNames.rawXmlBlock}`,
358
- getAttrs: (dom) => ({
359
- xml: dom.getAttribute("data-xml"),
360
- name: dom.getAttribute("data-name")
361
- })
418
+ getAttrs: (dom) => {
419
+ const xml = rawXml(dom, "data-xml", ANY_ELEMENT);
420
+ if (xml === false) return false;
421
+ return { xml, name: dom.getAttribute("data-name") };
422
+ }
362
423
  }
363
424
  ]
364
425
  },
365
426
  /** The node that carries a non-paragraph body block (a table we could not model, sectPr, an unknown element) exactly as it came */
366
427
  docxRaw: {
367
- group: "block",
428
+ group: "block preserved",
368
429
  atom: true,
369
430
  selectable: false,
370
431
  attrs: {
@@ -396,7 +457,7 @@ var docxSchema = new Schema({
396
457
  },
397
458
  /** A bookmark range marker that occurs directly under w:body rather than inside a paragraph */
398
459
  bookmarkBlock: {
399
- group: "block",
460
+ group: "block preserved",
400
461
  atom: true,
401
462
  isolating: true,
402
463
  selectable: false,
@@ -442,7 +503,10 @@ var docxSchema = new Schema({
442
503
  parseDOM: [
443
504
  {
444
505
  tag: "br",
445
- getAttrs: (dom) => ({ brAttrs: dom.getAttribute("data-battrs") })
506
+ getAttrs: (dom) => {
507
+ const brAttrs = rawXml(dom, "data-battrs", ATTRIBUTES);
508
+ return brAttrs === false ? false : { brAttrs };
509
+ }
446
510
  }
447
511
  ]
448
512
  },
@@ -475,12 +539,16 @@ var docxSchema = new Schema({
475
539
  parseDOM: [
476
540
  {
477
541
  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
- })
542
+ getAttrs: (dom) => {
543
+ const xml = rawXml(dom, "data-xml", ELEMENT("drawing"));
544
+ if (xml === false) return false;
545
+ return {
546
+ src: toImageSrc(dom.getAttribute("src")),
547
+ extent: toImageExtent(parseJson(dom.getAttribute("data-extent"))),
548
+ alt: dom.getAttribute("alt") || null,
549
+ xml
550
+ };
551
+ }
484
552
  }
485
553
  ]
486
554
  },
@@ -508,10 +576,11 @@ var docxSchema = new Schema({
508
576
  parseDOM: [
509
577
  {
510
578
  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
- })
579
+ getAttrs: (dom) => {
580
+ const xml = rawXml(dom, "data-xml", ELEMENT("commentRangeStart"));
581
+ if (xml === false) return false;
582
+ return { id: dom.getAttribute("data-comment-id"), xml };
583
+ }
515
584
  }
516
585
  ]
517
586
  },
@@ -539,10 +608,11 @@ var docxSchema = new Schema({
539
608
  parseDOM: [
540
609
  {
541
610
  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
- })
611
+ getAttrs: (dom) => {
612
+ const xml = rawXml(dom, "data-xml", ELEMENT("commentRangeEnd"));
613
+ if (xml === false) return false;
614
+ return { id: dom.getAttribute("data-comment-id"), xml };
615
+ }
546
616
  }
547
617
  ]
548
618
  },
@@ -596,22 +666,43 @@ var docxSchema = new Schema({
596
666
  parseDOM: [
597
667
  {
598
668
  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
- })
669
+ getAttrs: (dom) => {
670
+ const referenceXml = rawXml(
671
+ dom,
672
+ "data-reference-xml",
673
+ ELEMENT("commentReference")
674
+ );
675
+ const commentXml = rawXml(
676
+ dom,
677
+ "data-comment-xml",
678
+ ELEMENT("comment")
679
+ );
680
+ const extensionXml = rawXml(
681
+ dom,
682
+ "data-comment-extension-xml",
683
+ ANY_ELEMENT
684
+ );
685
+ const replies = parseJson(dom.getAttribute("data-comment-replies")) ?? [];
686
+ if (referenceXml === false || commentXml === false || extensionXml === false || !repliesHoldTheirXml(replies)) {
687
+ return false;
688
+ }
689
+ return {
690
+ id: dom.getAttribute("data-comment-id"),
691
+ referenceXml,
692
+ author: dom.getAttribute("data-comment-author"),
693
+ authorId: dom.getAttribute("data-comment-author-id"),
694
+ initials: dom.getAttribute("data-comment-initials"),
695
+ date: dom.getAttribute("data-comment-date"),
696
+ text: dom.getAttribute("data-comment-text") ?? "",
697
+ commentXml,
698
+ imported: dom.getAttribute("data-comment-imported") === "1",
699
+ paraId: dom.getAttribute("data-comment-para-id"),
700
+ resolved: dom.getAttribute("data-comment-resolved") === "1",
701
+ extensionXml,
702
+ threadImported: dom.getAttribute("data-comment-thread-imported") === "1",
703
+ replies
704
+ };
705
+ }
615
706
  }
616
707
  ]
617
708
  },
@@ -652,14 +743,22 @@ var docxSchema = new Schema({
652
743
  parseDOM: [
653
744
  {
654
745
  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
- })
746
+ getAttrs: (dom) => {
747
+ const referenceXml = rawXml(
748
+ dom,
749
+ "data-reference-xml",
750
+ ELEMENT("footnoteReference", "endnoteReference")
751
+ );
752
+ if (referenceXml === false) return false;
753
+ return {
754
+ kind: dom.getAttribute("data-note-kind") === "endnote" ? "endnote" : "footnote",
755
+ id: dom.getAttribute("data-note-id"),
756
+ label: dom.getAttribute("data-note-label") ?? "?",
757
+ text: dom.getAttribute("data-note-text") ?? "",
758
+ customMarkFollows: dom.getAttribute("data-custom-mark-follows") === "1",
759
+ referenceXml
760
+ };
761
+ }
663
762
  }
664
763
  ]
665
764
  },
@@ -682,7 +781,10 @@ var docxSchema = new Schema({
682
781
  parseDOM: [
683
782
  {
684
783
  tag: `span.${editorClassNames.rawInline}`,
685
- getAttrs: (dom) => ({ xml: dom.getAttribute("data-xml") })
784
+ getAttrs: (dom) => {
785
+ const xml = rawXml(dom, "data-xml", ANY_ELEMENT);
786
+ return xml === false ? false : { xml };
787
+ }
686
788
  }
687
789
  ]
688
790
  },
@@ -737,8 +839,8 @@ var docxSchema = new Schema({
737
839
  {
738
840
  tag: `span.${editorClassNames.sdt}`,
739
841
  getAttrs: (dom) => {
740
- const prefix = dom.getAttribute("data-sdt-prefix");
741
- if (prefix === null) return false;
842
+ const prefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
843
+ if (prefix === null || prefix === false) return false;
742
844
  return {
743
845
  sdtPrefix: prefix,
744
846
  sdtKey: parseInt10(dom.getAttribute("data-sdt-key"), 0),
@@ -800,8 +902,9 @@ var docxSchema = new Schema({
800
902
  {
801
903
  tag: `span.${editorClassNames.link}`,
802
904
  getAttrs: (dom) => {
803
- const prefix = dom.getAttribute("data-link-prefix");
905
+ const prefix = rawXml(dom, "data-link-prefix", LINK_PREFIX);
804
906
  const href = dom.getAttribute("data-href");
907
+ if (prefix === false) return false;
805
908
  if (prefix === null && href === null) return false;
806
909
  return {
807
910
  linkPrefix: prefix,
@@ -829,11 +932,16 @@ var docxSchema = new Schema({
829
932
  parseDOM: [
830
933
  {
831
934
  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
- })
935
+ getAttrs: (dom) => {
936
+ const rAttrs = rawXml(dom, "data-rattrs", ATTRIBUTES);
937
+ const rPr = rawXml(dom, "data-rpr", ELEMENT("rPr"));
938
+ if (rAttrs === false || rPr === false) return false;
939
+ return {
940
+ rAttrs,
941
+ rPr,
942
+ format: toRunFormat(parseJson(dom.getAttribute("data-fmt")))
943
+ };
944
+ }
837
945
  }
838
946
  ]
839
947
  },
@@ -854,7 +962,10 @@ var docxSchema = new Schema({
854
962
  parseDOM: [
855
963
  {
856
964
  tag: `span.${editorClassNames.tab}`,
857
- getAttrs: (dom) => ({ tabAttrs: dom.getAttribute("data-tattrs") })
965
+ getAttrs: (dom) => {
966
+ const tabAttrs = rawXml(dom, "data-tattrs", ATTRIBUTES);
967
+ return tabAttrs === false ? false : { tabAttrs };
968
+ }
858
969
  }
859
970
  ]
860
971
  }
@@ -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";
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 {};
@@ -0,0 +1,43 @@
1
+ // src/schema/editGuard.ts
2
+ import {
3
+ AddMarkStep,
4
+ AddNodeMarkStep,
5
+ AttrStep,
6
+ RemoveMarkStep,
7
+ RemoveNodeMarkStep,
8
+ ReplaceAroundStep,
9
+ ReplaceStep
10
+ } from "prosemirror-transform";
11
+ function rangeHolds(doc, from, to, holds) {
12
+ let found = false;
13
+ doc.nodesBetween(from, to, (node) => {
14
+ if (found) return false;
15
+ if (holds(node)) found = true;
16
+ return !found;
17
+ });
18
+ return found;
19
+ }
20
+ function stepReaches(step, before, after, holds) {
21
+ if (step instanceof AttrStep || step instanceof AddNodeMarkStep || step instanceof RemoveNodeMarkStep) {
22
+ const node = before.nodeAt(step.pos);
23
+ return node !== null && holds(node);
24
+ }
25
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep || step instanceof AddMarkStep || step instanceof RemoveMarkStep)) {
26
+ return true;
27
+ }
28
+ let reached = false;
29
+ step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => {
30
+ reached ||= rangeHolds(before, oldStart, oldEnd, holds) || rangeHolds(after, newStart, newEnd, holds);
31
+ });
32
+ return reached;
33
+ }
34
+ function transactionReaches(tr, holds) {
35
+ return tr.steps.some(
36
+ (step, index) => stepReaches(step, tr.docs[index], tr.docs[index + 1] ?? tr.doc, holds)
37
+ );
38
+ }
39
+ export {
40
+ rangeHolds,
41
+ stepReaches,
42
+ transactionReaches
43
+ };