@stll/folio-core 0.44.0 → 0.45.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 (90) hide show
  1. package/dist/ai-edits/__fixtures__/paragraphs.js +2 -2
  2. package/dist/ai-edits/headless.js +1 -0
  3. package/dist/compare/content-alignment.js +78 -53
  4. package/dist/compare/inline-atoms.js +34 -20
  5. package/dist/content-controls/mutateContentControls.js +4 -2
  6. package/dist/display-list/dom/renderDisplayListToDom.js +8 -8
  7. package/dist/document-operations.js +14 -3
  8. package/dist/docx/borderParser.js +5 -5
  9. package/dist/docx/commentParser.js +47 -36
  10. package/dist/docx/commentThreadKey.d.ts +18 -0
  11. package/dist/docx/commentThreadKey.js +22 -0
  12. package/dist/docx/diagramPreview.js +87 -27
  13. package/dist/docx/groupDrawingParser.js +3 -3
  14. package/dist/docx/hyperlinkParser.js +2 -2
  15. package/dist/docx/imageParser.d.ts +9 -1
  16. package/dist/docx/imageParser.js +58 -12
  17. package/dist/docx/imageRawXml.d.ts +14 -1
  18. package/dist/docx/imageRawXml.js +30 -6
  19. package/dist/docx/mathToMathml.js +12 -14
  20. package/dist/docx/nonVisualDrawingProps.d.ts +34 -0
  21. package/dist/docx/nonVisualDrawingProps.js +46 -0
  22. package/dist/docx/paragraphTextBoxEnrichment.js +3 -0
  23. package/dist/docx/parser.js +2 -2
  24. package/dist/docx/previewBudget.d.ts +64 -0
  25. package/dist/docx/previewBudget.js +88 -0
  26. package/dist/docx/revisionIdNormalization.js +17 -5
  27. package/dist/docx/rezip.js +17 -18
  28. package/dist/docx/sdtPropertiesPatch.js +24 -18
  29. package/dist/docx/sectionReferenceHistory.js +2 -2
  30. package/dist/docx/selectiveSave.js +6 -6
  31. package/dist/docx/serializer/blockSdtSerializer.js +38 -26
  32. package/dist/docx/serializer/borderSerializer.d.ts +1 -1
  33. package/dist/docx/serializer/borderSerializer.js +13 -12
  34. package/dist/docx/serializer/commentSerializer.d.ts +41 -16
  35. package/dist/docx/serializer/commentSerializer.js +82 -85
  36. package/dist/docx/serializer/fontTableSerializer.js +6 -6
  37. package/dist/docx/serializer/headerFooterSerializer.js +5 -5
  38. package/dist/docx/serializer/markupRangeAttributes.js +2 -2
  39. package/dist/docx/serializer/numberingSerializer.js +7 -6
  40. package/dist/docx/serializer/paragraphSerializer.js +19 -18
  41. package/dist/docx/serializer/partNamespaces.js +2 -2
  42. package/dist/docx/serializer/runSerializer.js +48 -28
  43. package/dist/docx/serializer/sectionPropertiesSerializer.js +11 -10
  44. package/dist/docx/serializer/settingsSerializer.js +4 -3
  45. package/dist/docx/serializer/stylesSerializer.js +6 -6
  46. package/dist/docx/serializer/tableSerializer.js +10 -9
  47. package/dist/docx/serializer/textFormattingSerializer.js +29 -28
  48. package/dist/docx/serializer/themeSerializer.js +6 -6
  49. package/dist/docx/serializer/trackedChangeAttributes.js +2 -2
  50. package/dist/docx/serializer/xmlUtils.d.ts +1 -2
  51. package/dist/docx/serializer/xmlUtils.js +1 -13
  52. package/dist/docx/server/boundedArchive.d.ts +12 -0
  53. package/dist/docx/server/boundedArchive.js +20 -1
  54. package/dist/docx/server/validateDocxConformance.js +22 -1
  55. package/dist/docx/shapeParser.js +7 -5
  56. package/dist/docx/textBoxParser.js +7 -2
  57. package/dist/docx/unzip.d.ts +23 -0
  58. package/dist/docx/unzip.js +32 -22
  59. package/dist/docx/verbatimCapture.js +1 -1
  60. package/dist/docx/vmlImageParser.js +3 -2
  61. package/dist/docx/vmlPreview.d.ts +1 -3
  62. package/dist/docx/vmlPreview.js +2 -30
  63. package/dist/docx/xmlParser.d.ts +16 -1
  64. package/dist/docx/xmlParser.js +56 -26
  65. package/dist/docx/xmlResourceLimits.d.ts +89 -9
  66. package/dist/docx/xmlResourceLimits.js +105 -24
  67. package/dist/internal/paragraphFormattingSerialization.js +3 -2
  68. package/dist/layout-painter/renderImage.js +4 -3
  69. package/dist/layout-painter/renderParagraph.js +4 -3
  70. package/dist/managers/autoSaveCodec.js +2 -8
  71. package/dist/markdown/images.js +1 -4
  72. package/dist/prosemirror/attrs/index.js +69 -0
  73. package/dist/prosemirror/commands/image.js +1 -0
  74. package/dist/prosemirror/conversion/fromProseDoc.js +69 -29
  75. package/dist/prosemirror/conversion/toProseDoc.js +83 -19
  76. package/dist/prosemirror/extensions/marks/HyperlinkExtension.js +2 -3
  77. package/dist/prosemirror/extensions/nodes/ImageExtension.js +4 -0
  78. package/dist/prosemirror/extensions/nodes/ShapeExtension.js +7 -2
  79. package/dist/prosemirror/extensions/nodes/TextBoxExtension.js +8 -4
  80. package/dist/prosemirror/paragraphFormattingProvenance.d.ts +6 -3
  81. package/dist/prosemirror/paragraphFormattingProvenance.js +12 -3
  82. package/dist/prosemirror/schema/nodes.d.ts +50 -1
  83. package/dist/utils/base64.d.ts +36 -0
  84. package/dist/utils/base64.js +40 -0
  85. package/dist/utils/clipboard.js +2 -1
  86. package/dist/utils/units.d.ts +10 -1
  87. package/dist/utils/units.js +12 -1
  88. package/dist/utils/urlSecurity.d.ts +8 -2
  89. package/dist/utils/urlSecurity.js +21 -3
  90. package/package.json +2 -2
@@ -1,3 +1,4 @@
1
+ import { escapeXmlText } from "@stll/docx-core";
1
2
  import JSZip from "jszip";
2
3
  //#region src/ai-edits/__fixtures__/paragraphs.ts
3
4
  /**
@@ -13,8 +14,7 @@ const ZIP_ENTRY_OPTIONS = {
13
14
  date: new Date(Date.UTC(2e3, 0, 1)),
14
15
  createFolders: false
15
16
  };
16
- const escapeXml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
17
- const paragraphXml = (text) => `<w:p><w:r><w:t xml:space="preserve">${escapeXml(text)}</w:t></w:r></w:p>`;
17
+ const paragraphXml = (text) => `<w:p><w:r><w:t xml:space="preserve">${escapeXmlText(text)}</w:t></w:r></w:p>`;
18
18
  const buildParagraphsDocx = async (paragraphs, { inTableCell } = {}) => {
19
19
  const paragraphsXml = paragraphs.map(paragraphXml).join("");
20
20
  const body = inTableCell ? `<w:tbl><w:tblPr><w:tblW w:w="0" w:type="auto"/></w:tblPr><w:tblGrid><w:gridCol w:w="9360"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcW w:w="9360" w:type="dxa"/></w:tcPr>${paragraphsXml}</w:tc></w:tr></w:tbl>` + paragraphXml("After the table.") : paragraphsXml;
@@ -212,6 +212,7 @@ const sameReferencedNumberingLevels = ({ current, target, numId, levelsByNumId }
212
212
  for (const level of levelsByNumId.get(numId) ?? []) {
213
213
  const currentLevel = currentNumbering.getLevel(numId, level);
214
214
  const targetLevel = targetNumbering.getLevel(numId, level);
215
+ if (currentLevel === null && targetLevel === null) continue;
215
216
  if (!currentLevel || !targetLevel || canonicalJson(currentLevel) !== canonicalJson(targetLevel)) return false;
216
217
  }
217
218
  return true;
@@ -455,6 +455,27 @@ const MAX_CONTENT_STRUCTURE_PROFILE_TEXT_CODE_UNITS = 16384;
455
455
  const MAX_CONTENT_STRUCTURE_PROFILE_TOKENS = 64;
456
456
  const MAX_CONTENT_STRUCTURE_PROFILE_KIND_CODE_UNITS = 256;
457
457
  const CONTENT_STRUCTURE_ANCHOR_MINIMUM_WORD_COUNT = 3;
458
+ /** Four independent 32-bit lanes, so a digest collision needs four at once. */
459
+ const CONTENT_DIGEST_SEEDS = [
460
+ 2166136261,
461
+ 2654435769,
462
+ 2246822507,
463
+ 3266489909
464
+ ];
465
+ const CONTENT_DIGEST_PRIMES = [
466
+ 16777619,
467
+ 1540483477,
468
+ 668265263,
469
+ 374761393
470
+ ];
471
+ const foldContentDigest = (lanes, text) => {
472
+ for (let index = 0; index < text.length; index++) {
473
+ const unit = text.charCodeAt(index);
474
+ for (let lane = 0; lane < lanes.length; lane++) lanes[lane] = Math.imul((lanes[lane] ?? 0) ^ unit, CONTENT_DIGEST_PRIMES[lane] ?? 1);
475
+ }
476
+ for (let lane = 0; lane < lanes.length; lane++) lanes[lane] = Math.imul((lanes[lane] ?? 0) ^ text.length, CONTENT_DIGEST_PRIMES[lane] ?? 1);
477
+ };
478
+ const renderContentDigest = (lanes, blockCount) => `${String(blockCount)}:${[...lanes].map((lane) => (lane >>> 0).toString(36)).join(":")}`;
458
479
  const createContentStructureProfile = ({ blocks, blockStructure, idStability, physicalCellCount, retainContainerIdentity = false }) => {
459
480
  let containerIdentity = null;
460
481
  if (retainContainerIdentity) if (blocks.length > MAX_CONTENT_STRUCTURE_PROFILE_BLOCKS) containerIdentity = { status: "unavailable" };
@@ -481,39 +502,34 @@ const createContentStructureProfile = ({ blocks, blockStructure, idStability, ph
481
502
  if (idStability(block) === "stable") stableIds.push(block.id);
482
503
  }
483
504
  }
484
- if (blocks.length > MAX_CONTENT_STRUCTURE_PROFILE_BLOCKS) return {
485
- anchorTexts: [],
505
+ const lanes = Int32Array.from(CONTENT_DIGEST_SEEDS);
506
+ for (const block of blocks) foldContentDigest(lanes, JSON.stringify([
507
+ ...blockStructure(block),
508
+ block.kind,
509
+ block.text
510
+ ]));
511
+ const identity = {
486
512
  blockIds,
487
513
  containerIdentity,
488
- exactSignature: null,
514
+ contentDigest: renderContentDigest(lanes, blocks.length),
489
515
  physicalCellCount: physicalCellCount ?? null,
490
- stableIds,
491
- tokenCounts: null,
492
- tokenCount: 0
516
+ stableIds
517
+ };
518
+ const overCap = {
519
+ ...identity,
520
+ anchors: { status: "skipped-over-cap" },
521
+ tokens: { status: "skipped-over-cap" }
493
522
  };
523
+ if (blocks.length > MAX_CONTENT_STRUCTURE_PROFILE_BLOCKS) return overCap;
494
524
  let textCodeUnits = 0;
495
525
  for (const block of blocks) {
496
- if (block.kind.length > MAX_CONTENT_STRUCTURE_PROFILE_KIND_CODE_UNITS || block.text.length > MAX_CONTENT_STRUCTURE_PROFILE_TEXT_CODE_UNITS - textCodeUnits) return {
497
- anchorTexts: [],
498
- blockIds,
499
- containerIdentity,
500
- exactSignature: null,
501
- physicalCellCount: physicalCellCount ?? null,
502
- stableIds,
503
- tokenCounts: null,
504
- tokenCount: 0
505
- };
526
+ if (block.kind.length > MAX_CONTENT_STRUCTURE_PROFILE_KIND_CODE_UNITS || block.text.length > MAX_CONTENT_STRUCTURE_PROFILE_TEXT_CODE_UNITS - textCodeUnits) return overCap;
506
527
  textCodeUnits += block.text.length;
507
528
  }
508
529
  const tokenCounts = /* @__PURE__ */ new Map();
509
530
  const anchorTexts = /* @__PURE__ */ new Set();
510
531
  let tokenCount = 0;
511
532
  let tokensComplete = true;
512
- const signature = blocks.map((block) => [
513
- ...blockStructure(block),
514
- block.kind,
515
- block.text
516
- ]);
517
533
  for (const block of blocks) {
518
534
  let blockWordCount = 0;
519
535
  for (const match of block.text.matchAll(/\S+/gu)) {
@@ -530,24 +546,31 @@ const createContentStructureProfile = ({ blocks, blockStructure, idStability, ph
530
546
  if (!tokensComplete) break;
531
547
  }
532
548
  return {
533
- anchorTexts: [...anchorTexts],
534
- blockIds,
535
- containerIdentity,
536
- exactSignature: JSON.stringify(signature),
537
- physicalCellCount: physicalCellCount ?? null,
538
- stableIds,
539
- tokenCounts: tokensComplete ? tokenCounts : null,
540
- tokenCount: tokensComplete ? tokenCount : 0
549
+ ...identity,
550
+ anchors: {
551
+ status: "computed",
552
+ texts: [...anchorTexts]
553
+ },
554
+ tokens: tokensComplete ? {
555
+ status: "computed",
556
+ counts: tokenCounts,
557
+ total: tokenCount
558
+ } : { status: "skipped-over-cap" }
541
559
  };
542
560
  };
561
+ const UNKNOWN_CONTENT_STRUCTURE_SIMILARITY = { status: "unknown" };
543
562
  const contentStructureProfileSimilarity = (base, revised, workSession) => {
544
- if (!base.tokenCounts || !revised.tokenCounts || base.tokenCount === 0 || revised.tokenCount === 0) return 0;
545
- const [tokens, counterparts] = base.tokenCounts.size <= revised.tokenCounts.size ? [base.tokenCounts, revised.tokenCounts] : [revised.tokenCounts, base.tokenCounts];
546
- if (tokens.size > workSession.remainingStructuralTokenLookups) return 0;
563
+ if (base.tokens.status === "skipped-over-cap" || revised.tokens.status === "skipped-over-cap") return UNKNOWN_CONTENT_STRUCTURE_SIMILARITY;
564
+ if (base.tokens.total === 0 || revised.tokens.total === 0) return UNKNOWN_CONTENT_STRUCTURE_SIMILARITY;
565
+ const [tokens, counterparts] = base.tokens.counts.size <= revised.tokens.counts.size ? [base.tokens.counts, revised.tokens.counts] : [revised.tokens.counts, base.tokens.counts];
566
+ if (tokens.size > workSession.remainingStructuralTokenLookups) return UNKNOWN_CONTENT_STRUCTURE_SIMILARITY;
547
567
  workSession.remainingStructuralTokenLookups -= tokens.size;
548
568
  let shared = 0;
549
569
  for (const [token, count] of tokens) shared += Math.min(count, counterparts.get(token) ?? 0);
550
- return 2 * shared / (base.tokenCount + revised.tokenCount);
570
+ return {
571
+ status: "measured",
572
+ value: 2 * shared / (base.tokens.total + revised.tokens.total)
573
+ };
551
574
  };
552
575
  const tableStructureProfile = (blocks, idStability) => {
553
576
  const tableOrdinalByIndex = /* @__PURE__ */ new Map();
@@ -634,7 +657,6 @@ const uniqueExactContentSequencePairs = ({ baseKeys, revisedKeys }) => {
634
657
  const uniqueIndexes = (keys) => {
635
658
  const indexes = /* @__PURE__ */ new Map();
636
659
  keys.forEach((key, index) => {
637
- if (key === -1) return;
638
660
  indexes.set(key, indexes.has(key) ? null : index);
639
661
  });
640
662
  return indexes;
@@ -731,16 +753,15 @@ const alignProfiledContentSequence = ({ base, revised, workSession, canPair = ()
731
753
  }
732
754
  const exactSignatureKeys = /* @__PURE__ */ new Map();
733
755
  let nextExactSignatureKey = 0;
734
- const internExactSignature = (signature) => {
735
- if (signature === null) return -1;
736
- const existing = exactSignatureKeys.get(signature);
756
+ const internContentDigest = (digest) => {
757
+ const existing = exactSignatureKeys.get(digest);
737
758
  if (existing !== void 0) return existing;
738
759
  const key = nextExactSignatureKey++;
739
- exactSignatureKeys.set(signature, key);
760
+ exactSignatureKeys.set(digest, key);
740
761
  return key;
741
762
  };
742
- const baseExactSignatureKeys = base.map(({ profile }) => internExactSignature(profile.exactSignature));
743
- const revisedExactSignatureKeys = revised.map(({ profile }) => internExactSignature(profile.exactSignature));
763
+ const baseExactSignatureKeys = base.map(({ profile }) => internContentDigest(profile.contentDigest));
764
+ const revisedExactSignatureKeys = revised.map(({ profile }) => internContentDigest(profile.contentDigest));
744
765
  let persistedPairs = /* @__PURE__ */ new Set();
745
766
  if (base.some(({ profile }) => profile.containerIdentity !== null) || revised.some(({ profile }) => profile.containerIdentity !== null)) {
746
767
  const exactPairs = uniqueExactContentSequencePairs({
@@ -781,16 +802,20 @@ const alignProfiledContentSequence = ({ base, revised, workSession, canPair = ()
781
802
  if (!revisedItem) continue;
782
803
  if (!canPair(baseItem, revisedItem)) continue;
783
804
  const pairIndex = baseIndex * revised.length + revisedIndex;
784
- const exact = baseExactSignatureKeys[baseIndex] !== -1 && baseExactSignatureKeys[baseIndex] === revisedExactSignatureKeys[revisedIndex];
805
+ const exact = baseExactSignatureKeys[baseIndex] === revisedExactSignatureKeys[revisedIndex];
785
806
  const stable = stablePairs.has(pairIndex);
786
- const profileSimilarity = exact ? 1 : contentStructureProfileSimilarity(baseItem.profile, revisedItem.profile, workSession);
787
- const similar = Math.max(0, Math.min(1, profileSimilarity * similarityFactor(baseItem, revisedItem)));
807
+ const profileSimilarity = exact ? {
808
+ status: "measured",
809
+ value: 1
810
+ } : contentStructureProfileSimilarity(baseItem.profile, revisedItem.profile, workSession);
811
+ const similar = profileSimilarity.status === "measured" ? Math.max(0, Math.min(1, profileSimilarity.value * similarityFactor(baseItem, revisedItem))) : 0;
812
+ const similarEnough = profileSimilarity.status === "measured" && similar >= CONTENT_STRUCTURE_PAIR_SIMILARITY;
788
813
  const stableAtSamePosition = stable && baseIndex === revisedIndex;
789
814
  const persistedAtSamePosition = baseIndex === revisedIndex && provenanceTransitionAtSamePosition[baseIndex] === 1;
790
815
  const shiftedPersisted = baseIndex !== revisedIndex && persistedPairs.has(pairIndex);
791
816
  const persisted = persistedAtSamePosition || shiftedPersisted;
792
817
  const soleStructuralSlot = pairSoleStructuralSlot && base.length === 1 && revised.length === 1;
793
- if (!exact && !stableAtSamePosition && !persisted && !soleStructuralSlot && similar < CONTENT_STRUCTURE_PAIR_SIMILARITY) continue;
818
+ if (!exact && !stableAtSamePosition && !persisted && !soleStructuralSlot && !similarEnough) continue;
794
819
  pairScores[pairIndex] = (stable ? stableWeight : 0) + (exact ? exactWeight : 0) + (shiftedPersisted && !stable && !exact ? secondaryWeight : 0) + (persistedAtSamePosition ? continuityBonus : 0) + Math.round(similar * CONTENT_STRUCTURE_SIMILARITY_SCALE) + (soleStructuralSlot ? 1 : 0);
795
820
  }
796
821
  }
@@ -1124,11 +1149,10 @@ const contentStructureEvidenceIndexes = (base, revised) => {
1124
1149
  let nextExactKey = 0;
1125
1150
  const internExactSignatures = (items) => {
1126
1151
  for (const { profile } of items) {
1127
- if (profile.exactSignature === null) continue;
1128
- let key = exactKeys.get(profile.exactSignature);
1152
+ let key = exactKeys.get(profile.contentDigest);
1129
1153
  if (key === void 0) {
1130
1154
  key = nextExactKey++;
1131
- exactKeys.set(profile.exactSignature, key);
1155
+ exactKeys.set(profile.contentDigest, key);
1132
1156
  }
1133
1157
  exactKeyByProfile.set(profile, key);
1134
1158
  }
@@ -1152,7 +1176,7 @@ const contentStructureEvidenceIndexes = (base, revised) => {
1152
1176
  if (indexes.at(-1) !== itemIndex) indexes.push(itemIndex);
1153
1177
  } else stableIndexes.set(id, [itemIndex]);
1154
1178
  }
1155
- for (const text of profile.anchorTexts) {
1179
+ for (const text of profile.anchors.status === "computed" ? profile.anchors.texts : []) {
1156
1180
  const indexes = anchorTextIndexes.get(text);
1157
1181
  if (indexes) {
1158
1182
  if (indexes.at(-1) !== itemIndex) indexes.push(itemIndex);
@@ -1190,12 +1214,13 @@ const sortedIndexesLeaveRange = (indexes, start, end) => {
1190
1214
  const profileHasEvidenceOutsideRange = (profile, opposite, start, end) => {
1191
1215
  const exactKey = opposite.exactKeyByProfile.get(profile);
1192
1216
  if (exactKey !== void 0 && sortedIndexesLeaveRange(opposite.exactIndexes.get(exactKey), start, end)) return true;
1193
- if (profile.anchorTexts.some((text) => sortedIndexesLeaveRange(opposite.anchorTextIndexes.get(text), start, end))) return true;
1217
+ if (profile.anchors.status === "computed" && profile.anchors.texts.some((text) => sortedIndexesLeaveRange(opposite.anchorTextIndexes.get(text), start, end))) return true;
1194
1218
  return profile.stableIds.some((id) => sortedIndexesLeaveRange(opposite.stableIndexes.get(id), start, end));
1195
1219
  };
1196
1220
  const profilesShareContentAnchor = (base, revised) => {
1197
- const baseTexts = new Set(base.anchorTexts);
1198
- return revised.anchorTexts.some((text) => baseTexts.has(text));
1221
+ if (base.anchors.status === "skipped-over-cap" || revised.anchors.status === "skipped-over-cap") return "unknown";
1222
+ const baseTexts = new Set(base.anchors.texts);
1223
+ return revised.anchors.texts.some((text) => baseTexts.has(text)) ? "shares" : "disjoint";
1199
1224
  };
1200
1225
  const isBodyDocumentSegment = (segment) => segment.kind === "body";
1201
1226
  const profileBodySegments = (segments, idStability) => segments.flatMap((segment) => isBodyDocumentSegment(segment) ? [{
@@ -1288,13 +1313,13 @@ const pairTableSegmentsInGaps = ({ base, revised, baseGaps, revisedGaps, trusted
1288
1313
  const revisedEnd = revisedStart + revisedItems.length;
1289
1314
  const baseHasExternalEvidence = new Set(baseItems.filter(({ profile }) => profileHasEvidenceOutsideRange(profile, evidence.revised, revisedStart, revisedEnd)).map(({ item }) => item));
1290
1315
  const revisedHasExternalEvidence = new Set(revisedItems.filter(({ profile }) => profileHasEvidenceOutsideRange(profile, evidence.base, baseStart, baseEnd)).map(({ item }) => item));
1291
- const solePairHasContentAnchor = baseItems.length === 1 && revisedItems.length === 1 && profilesShareContentAnchor(baseItems.at(0)?.profile ?? panic("A sole base table has no profile"), revisedItems.at(0)?.profile ?? panic("A sole revised table has no profile"));
1316
+ const solePairAnchorOverlap = baseItems.length === 1 && revisedItems.length === 1 ? profilesShareContentAnchor(baseItems.at(0)?.profile ?? panic("A sole base table has no profile"), revisedItems.at(0)?.profile ?? panic("A sole revised table has no profile")) : "disjoint";
1292
1317
  const alignments = alignProfiledContentSequence({
1293
1318
  base: baseItems,
1294
1319
  revised: revisedItems,
1295
1320
  workSession,
1296
1321
  canPair: (baseItem, revisedItem) => !baseHasExternalEvidence.has(baseItem.item) && !revisedHasExternalEvidence.has(revisedItem.item),
1297
- pairSoleStructuralSlot: trustedBodyPairCount > 0 || solePairHasContentAnchor
1322
+ pairSoleStructuralSlot: trustedBodyPairCount > 0 || solePairAnchorOverlap === "shares"
1298
1323
  });
1299
1324
  for (const alignment of alignments) {
1300
1325
  if (alignment.type !== "pair") continue;
@@ -29,6 +29,15 @@ const textBlocksOf = (doc) => {
29
29
  });
30
30
  return blocks;
31
31
  };
32
+ /**
33
+ * Clean-text offset nearest a document position.
34
+ *
35
+ * `buildCleanBlockText` returns one offset per character plus one for
36
+ * end-of-block, so there is always an offset to land on. The result was typed
37
+ * `number | null` and never was: the caller's refusal branch could not run,
38
+ * while an empty `offsets` would have produced `-1` rather than the `null` the
39
+ * type promised.
40
+ */
32
41
  const offsetAt = ({ offsets, position }) => {
33
42
  let low = 0;
34
43
  let high = offsets.length;
@@ -40,16 +49,33 @@ const offsetAt = ({ offsets, position }) => {
40
49
  }
41
50
  return Math.min(low, offsets.length - 1);
42
51
  };
52
+ /**
53
+ * Attributes a conversion mints for itself rather than reads from the package.
54
+ *
55
+ * `toProseDoc` salts text-box anchor ids with a per-load nonce so a pasted span
56
+ * cannot hijack a real anchor. A comparison reads two independent conversions,
57
+ * so a minted handle differs on every run, and a key holding one makes a
58
+ * document differ from itself. Only document facts belong in a key.
59
+ *
60
+ * The guard against a node type gaining a minted attribute without being added
61
+ * here is not this list but `compareIdentity.property.test.ts`, which compares
62
+ * generated packages carrying each atom with themselves.
63
+ */
64
+ const CONVERSION_LOCAL_ATTRS = { textBoxAnchor: ["anchorId"] };
65
+ const documentFactAttrs = (node) => {
66
+ const local = CONVERSION_LOCAL_ATTRS[node.type.name];
67
+ if (!local) return node.attrs;
68
+ return Object.fromEntries(Object.entries(node.attrs).filter(([name]) => !local.includes(name)));
69
+ };
43
70
  const atomKey = (node) => canonicalJson({
44
71
  type: node.type.name,
45
- attrs: node.attrs,
72
+ attrs: documentFactAttrs(node),
46
73
  content: node.content.toJSON()
47
74
  });
48
75
  const atomBlockOf = ({ node, from }) => {
49
76
  const clean = buildCleanBlockText(node, from, { fieldResults: "omitted" });
50
77
  const supported = [];
51
78
  const unsupportedTopology = [];
52
- let unalignable = false;
53
79
  node.descendants((child, relativePosition) => {
54
80
  if (child.isText) return false;
55
81
  if (!child.isInline || !child.isAtom) return true;
@@ -58,18 +84,14 @@ const atomBlockOf = ({ node, from }) => {
58
84
  offsets: clean.offsets,
59
85
  position
60
86
  });
61
- if (offset === null) {
62
- unalignable = true;
63
- return false;
64
- }
65
87
  if (!isSupportedAtom(child)) {
66
88
  if (child.type.name === "renderedPageBreak") return false;
67
- unsupportedTopology.push(`${offset}:${child.type.name}:${canonicalJson(child.attrs)}`);
89
+ unsupportedTopology.push(`${offset}:${child.type.name}:${canonicalJson(documentFactAttrs(child))}`);
68
90
  return false;
69
91
  }
70
92
  const prepared = prepareTargetInlineAtom(child);
71
93
  if (!prepared) {
72
- unsupportedTopology.push(`${offset}:${child.type.name}:${canonicalJson(child.attrs)}`);
94
+ unsupportedTopology.push(`${offset}:${child.type.name}:${canonicalJson(documentFactAttrs(child))}`);
73
95
  return false;
74
96
  }
75
97
  supported.push({
@@ -80,7 +102,7 @@ const atomBlockOf = ({ node, from }) => {
80
102
  });
81
103
  return false;
82
104
  });
83
- return unalignable ? null : {
105
+ return {
84
106
  node,
85
107
  from,
86
108
  cleanText: clean.text,
@@ -108,15 +130,7 @@ const targetBlockIdLookup = (snapshot) => {
108
130
  return owner?.id;
109
131
  };
110
132
  };
111
- const atomBlocksOf = (doc) => {
112
- const blocks = [];
113
- for (const block of textBlocksOf(doc)) {
114
- const atoms = atomBlockOf(block);
115
- if (!atoms) return null;
116
- blocks.push(atoms);
117
- }
118
- return blocks;
119
- };
133
+ const atomBlocksOf = (doc) => textBlocksOf(doc).map(atomBlockOf);
120
134
  const sameBlockTopology = (left, right) => left.node.type === right.node.type && left.cleanText === right.cleanText && canonicalJson(left.unsupportedTopology) === canonicalJson(right.unsupportedTopology);
121
135
  const matchingAtomGroup = ({ live, target, liveStart, targetStart }) => {
122
136
  if (live.length === target.length && live.every((atom, index) => atom.key === target[index]?.key)) return live.map((_, index) => [liveStart + index, targetStart + index]);
@@ -260,7 +274,7 @@ const matchInlineAtoms = ({ state, targetSnapshot, revisionStamp, originalRevisi
260
274
  let sourceBlocks;
261
275
  const liveBlocks = atomBlocksOf(reviewed.state.doc);
262
276
  const targetBlocks = atomBlocksOf(targetDocument);
263
- if (!liveBlocks || !targetBlocks || liveBlocks.length !== targetBlocks.length) return { status: "unalignable" };
277
+ if (liveBlocks.length !== targetBlocks.length) return { status: "unalignable" };
264
278
  const actions = [];
265
279
  for (const [index, live] of liveBlocks.entries()) {
266
280
  const target = targetBlocks[index];
@@ -366,7 +380,7 @@ const sameInlineAtoms = (leftDocument, rightDocument) => {
366
380
  if (!hasSupportedAtom(leftDocument) && !hasSupportedAtom(rightDocument)) return true;
367
381
  const leftBlocks = atomBlocksOf(leftDocument);
368
382
  const rightBlocks = atomBlocksOf(rightDocument);
369
- if (!leftBlocks || !rightBlocks || leftBlocks.length !== rightBlocks.length) return false;
383
+ if (leftBlocks.length !== rightBlocks.length) return false;
370
384
  return leftBlocks.every((left, index) => {
371
385
  const right = rightBlocks[index];
372
386
  if (left.supported.length === 0 && right?.supported.length === 0) return true;
@@ -1,5 +1,6 @@
1
1
  import { formatDate } from "../docx/fieldParser.js";
2
2
  import { ContentControlBoundError, ContentControlLockedError, ContentControlTypeError } from "./errors.js";
3
+ import { sanitizeXmlCharacters } from "@stll/docx-core";
3
4
  //#region src/content-controls/mutateContentControls.ts
4
5
  /**
5
6
  * Pure (immutable) write helpers for block-level content controls.
@@ -220,7 +221,8 @@ function controlMatchesFilter(control, filter) {
220
221
  if (filter.sdtType !== void 0 && control.properties.sdtType !== filter.sdtType) return false;
221
222
  return true;
222
223
  }
223
- function makeParagraphFromText(text) {
224
+ function makeParagraphFromText(rawText) {
225
+ const text = sanitizeXmlCharacters(rawText);
224
226
  return {
225
227
  type: "paragraph",
226
228
  content: text.length === 0 ? [] : [{
@@ -300,7 +302,7 @@ function setContentControlValue(doc, filter, input, options = {}) {
300
302
  ...control,
301
303
  properties: {
302
304
  ...control.properties,
303
- dropdownLastValue: input.value,
305
+ dropdownLastValue: sanitizeXmlCharacters(input.value),
304
306
  showingPlaceholder: false,
305
307
  ...strippedRawPropertiesXml !== void 0 ? { rawPropertiesXml: strippedRawPropertiesXml } : {}
306
308
  },
@@ -1,3 +1,5 @@
1
+ import { bytesToDataUrl } from "../../utils/base64.js";
2
+ import { anchorTargetAttrs } from "../../utils/urlSecurity.js";
1
3
  import { STROKE_DASH_FACTORS, walkRegions } from "../primitives.js";
2
4
  import { panic } from "better-result";
3
5
  //#region src/display-list/dom/renderDisplayListToDom.ts
@@ -42,7 +44,6 @@ const PAGE_CLASS_NAME = "layout-page";
42
44
  */
43
45
  const PAGE_ELEMENT_ID_PREFIX = "page-";
44
46
  const DEGREES_PER_RADIAN = 180 / Math.PI;
45
- const BASE64_CHUNK_SIZE = 32768;
46
47
  const IMAGE_MIME_TYPES = {
47
48
  png: "image/png",
48
49
  jpeg: "image/jpeg"
@@ -98,17 +99,11 @@ const resolveImage = (ref, images) => {
98
99
  if (source === void 0) panic(`renderDisplayListToDom: image ref ${ref} is out of range (${images.length} images)`);
99
100
  return source;
100
101
  };
101
- const toBase64 = (bytes) => {
102
- let binary = "";
103
- for (let offset = 0; offset < bytes.length; offset += BASE64_CHUNK_SIZE) binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_CHUNK_SIZE));
104
- return btoa(binary);
105
- };
106
102
  /** A `Blob` part cannot be backed by shared memory, so prove it is not. */
107
103
  const isBlobPart = (bytes) => bytes.buffer instanceof ArrayBuffer;
108
104
  const binarySrc = (bytes, mimeType) => {
109
105
  if (isBlobPart(bytes) && typeof Blob === "function" && typeof URL !== "undefined" && typeof URL.createObjectURL === "function") return URL.createObjectURL(new Blob([bytes], { type: mimeType }));
110
- if (typeof btoa === "function") return `data:${mimeType};base64,${toBase64(bytes)}`;
111
- panic("renderDisplayListToDom: no Blob or btoa to build a binary source from");
106
+ return bytesToDataUrl(bytes, mimeType);
112
107
  };
113
108
  const fontFaceRule = (face, embedded, src) => `@font-face { font-family: ${quoteFontFamily(embedded.id)}; src: url("${src}"); font-weight: ${face.weight}; font-style: ${face.italic ? "italic" : "normal"}; }`;
114
109
  /**
@@ -489,6 +484,11 @@ const paintLink = ({ rect, target, tooltip }, context) => {
489
484
  element.style.width = px(rect.widthPx);
490
485
  element.style.height = px(rect.heightPx);
491
486
  element.href = linkHref(target);
487
+ if (target.kind !== "page") {
488
+ const anchor = anchorTargetAttrs(void 0);
489
+ element.target = anchor.target;
490
+ element.rel = anchor.rel;
491
+ }
492
492
  if (tooltip !== void 0) element.title = tooltip;
493
493
  context.parent.append(element);
494
494
  };
@@ -1,6 +1,7 @@
1
1
  import { applyFolioAIEditOperations, previewFolioAIEditOperations } from "./ai-edits/apply.js";
2
2
  import { LINE_SPACING_RULE_VALUES, PARAGRAPH_ALIGNMENT_VALUES } from "./types/documentEnumValues.js";
3
3
  import { TaggedError } from "better-result";
4
+ import { sanitizeXmlCharacters } from "@stll/docx-core";
4
5
  //#region src/document-operations.ts
5
6
  const FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION = 1;
6
7
  /** Direct paragraph-alignment values accepted by the operation contract. */
@@ -138,15 +139,25 @@ const assertAllowedKeys = (value, path, allowedKeys) => {
138
139
  const unexpected = Object.keys(value).find((key) => !allowedKeys.includes(key));
139
140
  if (unexpected !== void 0) invalidBatch(`${path}.${unexpected}`, "unexpected property");
140
141
  };
142
+ /**
143
+ * Every string in a batch passes through here, so this is where a value an
144
+ * agent sent stops being able to corrupt the package it lands in: XML 1.0
145
+ * admits none of the C0 controls but tab, LF and CR, and no escape can carry
146
+ * one into a document. The batch is otherwise held to exactly what it says, so
147
+ * the rule is the narrowest one that keeps the request usable — drop what
148
+ * cannot be written, map an unpaired surrogate to U+FFFD — rather than
149
+ * rejecting a whole edit over a stray control character. The parsed batch is
150
+ * both what folio applies and what the receipt reports, so the two agree.
151
+ */
141
152
  const readString = (value, key, path) => {
142
153
  const candidate = value[key];
143
- if (typeof candidate === "string") return candidate;
154
+ if (typeof candidate === "string") return sanitizeXmlCharacters(candidate);
144
155
  return invalidBatch(`${path}.${key}`, "expected a string");
145
156
  };
146
157
  const readOptionalString = (value, key, path) => {
147
158
  const candidate = value[key];
148
159
  if (candidate === void 0) return;
149
- if (typeof candidate === "string") return candidate;
160
+ if (typeof candidate === "string") return sanitizeXmlCharacters(candidate);
150
161
  return invalidBatch(`${path}.${key}`, "expected a string when provided");
151
162
  };
152
163
  const readOptionalBoolean = (value, key, path) => {
@@ -184,7 +195,7 @@ const readOptionalStringArray = (value, key, path) => {
184
195
  if (candidate === void 0) return;
185
196
  if (!Array.isArray(candidate)) return invalidBatch(`${path}.${key}`, "expected an array when provided");
186
197
  return candidate.map((item, index) => {
187
- if (typeof item === "string") return item;
198
+ if (typeof item === "string") return sanitizeXmlCharacters(item);
188
199
  return invalidBatch(`${path}.${key}[${index}]`, "expected a string");
189
200
  });
190
201
  };
@@ -55,15 +55,15 @@ function parseBorderSpec(border, context) {
55
55
  if (shadow !== void 0) spec.shadow = shadow;
56
56
  const frame = parseOnOffAttribute(border, "w", "frame");
57
57
  if (frame !== void 0) spec.frame = frame;
58
- const artRelationshipId = getAttribute(border, "w", "id")?.trim();
58
+ const artRelationshipId = getAttribute(border, "r", "id")?.trim();
59
59
  if (artRelationshipId) spec.artRelationshipId = artRelationshipId;
60
- const topLeftArtRelationshipId = getAttribute(border, "w", "topLeft")?.trim();
60
+ const topLeftArtRelationshipId = getAttribute(border, "r", "topLeft")?.trim();
61
61
  if (topLeftArtRelationshipId) spec.topLeftArtRelationshipId = topLeftArtRelationshipId;
62
- const topRightArtRelationshipId = getAttribute(border, "w", "topRight")?.trim();
62
+ const topRightArtRelationshipId = getAttribute(border, "r", "topRight")?.trim();
63
63
  if (topRightArtRelationshipId) spec.topRightArtRelationshipId = topRightArtRelationshipId;
64
- const bottomLeftArtRelationshipId = getAttribute(border, "w", "bottomLeft")?.trim();
64
+ const bottomLeftArtRelationshipId = getAttribute(border, "r", "bottomLeft")?.trim();
65
65
  if (bottomLeftArtRelationshipId) spec.bottomLeftArtRelationshipId = bottomLeftArtRelationshipId;
66
- const bottomRightArtRelationshipId = getAttribute(border, "w", "bottomRight")?.trim();
66
+ const bottomRightArtRelationshipId = getAttribute(border, "r", "bottomRight")?.trim();
67
67
  if (bottomRightArtRelationshipId) spec.bottomRightArtRelationshipId = bottomRightArtRelationshipId;
68
68
  return spec;
69
69
  }