@stll/docx-core 0.37.0 → 0.38.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.
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import { writeZipPart } from "./zip/writeZipPart.js";
3
3
  import { i as serializeSequenceChildren } from "./sequenceChildren-B8ODeI3J.js";
4
4
  import { a as hasIllegalXmlCharacters, i as escapeXmlText, n as sanitizeMarkdownHref, o as sanitizeXmlCharacters, r as escapeXmlAttribute, t as sanitizeExternalUrl } from "./href-2XSCK-T8.js";
5
5
  import { Result, TaggedError, panic } from "better-result";
6
+ import { decodeText, encodeUtf8 } from "@stll/utf8-boundaries";
6
7
  import JSZip from "jszip";
7
8
  import { XMLParser, XMLValidator } from "fast-xml-parser";
8
9
  import { ENTITY_ACTION, EntityDecoder } from "@nodable/entities";
@@ -366,51 +367,51 @@ const serializeDocumentToDocx = async (document, options = {}) => {
366
367
  writeZipPart({
367
368
  zip,
368
369
  path: "[Content_Types].xml",
369
- data: contentTypesXml(hasNumbering)
370
+ data: encodeUtf8(contentTypesXml(hasNumbering))
370
371
  });
371
372
  writeZipPart({
372
373
  zip,
373
374
  path: "_rels/.rels",
374
- data: ROOT_RELS_XML
375
+ data: encodeUtf8(ROOT_RELS_XML)
375
376
  });
376
377
  writeZipPart({
377
378
  zip,
378
379
  path: "docProps/app.xml",
379
- data: APP_XML
380
+ data: encodeUtf8(APP_XML)
380
381
  });
381
382
  writeZipPart({
382
383
  zip,
383
384
  path: "docProps/core.xml",
384
- data: serializeCoreProperties(document)
385
+ data: encodeUtf8(serializeCoreProperties(document))
385
386
  });
386
387
  writeZipPart({
387
388
  zip,
388
389
  path: "word/document.xml",
389
- data: serializeDocumentXml(document)
390
+ data: encodeUtf8(serializeDocumentXml(document))
390
391
  });
391
392
  writeZipPart({
392
393
  zip,
393
394
  path: "word/styles.xml",
394
- data: serializeStyles(document.package.styles)
395
+ data: encodeUtf8(serializeStyles(document.package.styles))
395
396
  });
396
397
  if (hasNumbering) writeZipPart({
397
398
  zip,
398
399
  path: "word/numbering.xml",
399
- data: serializeNumbering(document.package.numbering)
400
+ data: encodeUtf8(serializeNumbering(document.package.numbering))
400
401
  });
401
402
  writeZipPart({
402
403
  zip,
403
404
  path: `word/${DEFAULT_FOOTER_PART_NAME}`,
404
- data: buildFooterXml(options.language)
405
+ data: encodeUtf8(buildFooterXml(options.language))
405
406
  });
406
407
  writeZipPart({
407
408
  zip,
408
409
  path: "word/_rels/document.xml.rels",
409
- data: documentRelsXml(hasNumbering)
410
+ data: encodeUtf8(documentRelsXml(hasNumbering))
410
411
  });
411
412
  for (const [path, file] of Object.entries(zip.files)) {
412
413
  if (file.dir || !path.endsWith(".xml")) continue;
413
- assertValidOoxmlNumericIds(await file.async("text"), path);
414
+ assertValidOoxmlNumericIds(await file.async("uint8array").then((bytes) => decodeText(bytes, { ignoreBOM: true })), path);
414
415
  }
415
416
  return await zip.generateAsync({
416
417
  type: "arraybuffer",
@@ -1226,7 +1227,7 @@ const validateDocxPackage = async (buffer) => {
1226
1227
  error: documentRead.error
1227
1228
  };
1228
1229
  const documentBytes = documentRead?.bytes;
1229
- const documentXml = documentBytes ? new TextDecoder().decode(documentBytes) : void 0;
1230
+ const documentXml = documentBytes ? decodeText(documentBytes) : void 0;
1230
1231
  const documentError = documentXml ? validateDocumentXml(documentXml) : "Generated DOCX has no word/document.xml root document.";
1231
1232
  if (documentError) return {
1232
1233
  valid: false,
@@ -1240,7 +1241,7 @@ const validateDocxPackage = async (buffer) => {
1240
1241
  code: DOCX_PACKAGE_ISSUE_CODES.ArchiveBoundsExceeded,
1241
1242
  error: documentRelsRead.error
1242
1243
  };
1243
- const documentRelsXml = documentRelsRead ? new TextDecoder().decode(documentRelsRead.bytes) : void 0;
1244
+ const documentRelsXml = documentRelsRead ? decodeText(documentRelsRead.bytes) : void 0;
1244
1245
  if (documentRelsXml?.includes("/relationships/numbering") && !zip.file("word/numbering.xml")) return {
1245
1246
  valid: false,
1246
1247
  code: DOCX_PACKAGE_ISSUE_CODES.MissingNumberingPart,
@@ -1262,7 +1263,7 @@ const validateDocxPackage = async (buffer) => {
1262
1263
  code: DOCX_PACKAGE_ISSUE_CODES.ArchiveBoundsExceeded,
1263
1264
  error: read.error
1264
1265
  };
1265
- xml = new TextDecoder().decode(read.bytes);
1266
+ xml = decodeText(read.bytes);
1266
1267
  }
1267
1268
  const issue = validateSchemaAttributes(xml);
1268
1269
  if (issue) return {
@@ -1,8 +1,5 @@
1
1
  import { $r as TextFormatting, Cn as Section, Gr as TableFormatting, H as BreakContent, I as BlockContent, Jn as TableRow, Mr as NumberingInstance, Rn as TabContent, Rr as ParagraphFormatting, Sr as AbstractNumbering, W as Comment, Wr as TableCellFormatting, Xr as TableRowFormatting, _n as Run, _t as HeaderFooter, a as DocumentSettings, an as ParagraphPropertyChange, b as StyleDefinitions, et as DocumentBody, g as Relationship, h as MediaFile, i as Document, jr as NumberingDefinitions, l as ExhaustiveFields, mt as Footnote, nn as ParagraphContent, ot as Endnote, rn as ParagraphMarkChange, s as DocxPackage, tn as Paragraph, vi as HeadingOutlineLevel, vn as RunContent, vt as HeaderFooterType, wn as SectionProperties, xt as INSERTION_SEAM_POLICIES, zn as Table } from "../document-wNmygRXS.js";
2
2
  import { Result } from "better-result";
3
- //#region src/ops/noteMarks.d.ts
4
- declare const noteUsesCustomMark: (document: Document, note: Footnote | Endnote) => boolean;
5
- //#endregion
6
3
  //#region src/ops/types.d.ts
7
4
  /** JSON-safe map presence, including an explicitly undefined section map. */
8
5
  type SectionMapState = {
@@ -1127,27 +1124,6 @@ declare class DocumentOpRefusal extends DocumentOpRefusal_base<{
1127
1124
  opType: DocumentOpType | undefined;
1128
1125
  }> {}
1129
1126
  //#endregion
1130
- //#region src/ops/storyLifecycle.d.ts
1131
- declare const sectionPropertiesAt: (document: Document, sectionIndex: number) => SectionProperties | undefined;
1132
- //#endregion
1133
- //#region src/ops/stories.d.ts
1134
- declare const sameStory: (left: OpStory, right: OpStory) => boolean;
1135
- declare const findStoryBody: (document: Document, story: OpStory) => DocumentBody | undefined;
1136
- declare const storyBody: (document: Document, story: OpStory) => DocumentBody;
1137
- declare const documentStories: (document: Document) => OpStory[];
1138
- //#endregion
1139
- //#region src/ops/blocks.d.ts
1140
- /**
1141
- * A body holding other blocks, its section view derived from them. A view
1142
- * that does not line up with the blocks (a different section count) is left
1143
- * for {@link sectionsInStep} to report.
1144
- */
1145
- declare const withBodyContent: (body: DocumentBody, content: BlockContent[]) => DocumentBody;
1146
- //#endregion
1147
- //#region src/ops/wire.d.ts
1148
- /** Capture own undefined fields before an operation enters its JSON journal. */
1149
- declare const captureDocumentOp: (op: DocumentOp) => DocumentOp;
1150
- //#endregion
1151
1127
  //#region src/ops/leaves.d.ts
1152
1128
  /** A place between two leaves. */
1153
1129
  type Gap = {
@@ -1173,6 +1149,41 @@ declare const inlineLeafSpans: (items: readonly InlineNode[]) => {
1173
1149
  /** Gaps in document order: by offset, then by the zero-width leaves before them. */
1174
1150
  declare const compareGaps: (left: Gap, right: Gap) => number;
1175
1151
  //#endregion
1152
+ //#region src/ops/comments.d.ts
1153
+ /**
1154
+ * Shared loaded-comment activation contract; legacy parentId revision associations stay explicit.
1155
+ * `allowedOrphans` names unanchored root comments the caller accepts ("all" for a loaded package).
1156
+ */
1157
+ declare const commentDocumentIssue: (document: Document, allowedOrphans?: ReadonlySet<number> | "all") => string | undefined;
1158
+ /** Allocate from the package's annotation domains without a hidden mutable counter. */
1159
+ declare const freshCommentId: (document: Document) => Result<number, DocumentOpRefusal>;
1160
+ /** Reconcile modeled comment and numbering parts with their document relationships. */
1161
+ declare const reconcileDocumentPartRelationships: (document: Document) => Document;
1162
+ //#endregion
1163
+ //#region src/ops/noteMarks.d.ts
1164
+ declare const noteUsesCustomMark: (document: Document, note: Footnote | Endnote) => boolean;
1165
+ //#endregion
1166
+ //#region src/ops/storyLifecycle.d.ts
1167
+ declare const sectionPropertiesAt: (document: Document, sectionIndex: number) => SectionProperties | undefined;
1168
+ //#endregion
1169
+ //#region src/ops/stories.d.ts
1170
+ declare const sameStory: (left: OpStory, right: OpStory) => boolean;
1171
+ declare const findStoryBody: (document: Document, story: OpStory) => DocumentBody | undefined;
1172
+ declare const storyBody: (document: Document, story: OpStory) => DocumentBody;
1173
+ declare const documentStories: (document: Document) => OpStory[];
1174
+ //#endregion
1175
+ //#region src/ops/blocks.d.ts
1176
+ /**
1177
+ * A body holding other blocks, its section view derived from them. A view
1178
+ * that does not line up with the blocks (a different section count) is left
1179
+ * for {@link sectionsInStep} to report.
1180
+ */
1181
+ declare const withBodyContent: (body: DocumentBody, content: BlockContent[]) => DocumentBody;
1182
+ //#endregion
1183
+ //#region src/ops/wire.d.ts
1184
+ /** Capture own undefined fields before an operation enters its JSON journal. */
1185
+ declare const captureDocumentOp: (op: DocumentOp) => DocumentOp;
1186
+ //#endregion
1176
1187
  //#region src/ops/hyperlinkIntent.d.ts
1177
1188
  type HyperlinkEditorIntent = {
1178
1189
  type: "setHyperlink";
@@ -1634,13 +1645,4 @@ declare const createClient: (document: Document) => {
1634
1645
  readonly notices: readonly ClientNotice[];
1635
1646
  };
1636
1647
  //#endregion
1637
- //#region src/ops/comments.d.ts
1638
- /**
1639
- * Shared loaded-comment activation contract; legacy parentId revision associations stay explicit.
1640
- * `allowedOrphans` names unanchored root comments the caller accepts ("all" for a loaded package).
1641
- */
1642
- declare const commentDocumentIssue: (document: Document, allowedOrphans?: ReadonlySet<number> | "all") => string | undefined;
1643
- /** Allocate from the package's annotation domains without a hidden mutable counter. */
1644
- declare const freshCommentId: (document: Document) => Result<number, DocumentOpRefusal>;
1645
- //#endregion
1646
- export { type AddNoteOp, type AppliedDocumentOp, BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, type BatchRejectionReason, type BlockInsertionPoint, type CommentAnchor, type CommentState, type CreateCommentOp, type CreateHeaderFooterOp, type CreateNumberingInstanceOp, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, type DeleteBlocksOp, type DeleteColumnOp, type DeleteCommentOp, type DeleteNumberingInstanceOp, type DeleteRangeOp, type DeleteRowOp, type DeleteTableOp, type DocumentBatch, type DocumentOp, type DocumentOpEnvelope, DocumentOpRefusal, type DocumentOpRefusalReason, type DocumentOpType, DocumentOpsContractError, EMPTY_PROPERTY_SETS, type EditorIntent, type EditorIntentMode, type EmptyPropertySet, type FormattingPatch, type HeaderFooterStory, INHERIT_RUN_PROPS, type InlineSlice, type InsertBlocksOp, type InsertColumnOp, type InsertContentOp, type InsertRowOp, type InsertTableOp, type InsertTextOp, type InsertedRunProps, type JoinBlocksOp, type JoinInlineOp, MAX_BATCH_WIRE_BYTES, type MergeCellsOp, type NewIds, type NoteStory, type NumberingPartState, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, type OpStory, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, type PackageResourceEntry, type PackageResourceEntryChange, type PackageResourceMapChange, type PackageResourceMedia, type PackageResourcePart, type PackageResources, type ParagraphPropsPatch, type ParagraphReviewFields, type PlanTrackedDeletionOptions, type PlanTrackedReplaceOptions, REVISION_DECISIONS, type RemoveHeaderFooterOp, type RemoveNoteOp, type ReplaceBlocksOp, type ReplaceInlineOp, type ResolveRevisionOp, type RestoreCommentStateOp, type RestoreStoryPartsOp, type RevisionDecision, type RevisionStamp, type RunPropsPatch, SPLIT_HALVES, type SectionEndpoint, type SectionMapState, type SectionPropertiesState, type SectionViewEntry, type SectionViewState, type SequencedBatch, type SetCellPropsOp, type SetCommentResolutionOp, type SetContainerBlocksOp, type SetPackageResourcesOp, type SetParagraphPropsOp, type SetParagraphReviewOp, type SetRowPropsOp, type SetRunPropsOp, type SetSectionEndpointOp, type SetSectionPropsOp, type SetTableGridOp, type SetTableOp, type SetTablePropsOp, type SetTableRowsOp, type SplitBlockOp, type SplitCellOp, type SplitHalf, type SplitInlineOp, type SplitParagraphFields, type TableEditOp, type TableIntentOperation, type TextPosition, type TouchedBlocks, type UpdateCommentContentOp, allocateCommentAnchorIds, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, commentDocumentIssue, compareGaps, compileEditorIntent, createClient, createEditorIntentIdAllocator, createSequencer, defaultInsertionGap, documentStories, editorParagraphGroups, editorPositionAtPhysicalPosition, findStoryBody, formattingEquals, freshCommentId, idKey, inlineLeafSpans, isTextOnlyRun, mapTocBookmarkPosition, mergeRunContent, normalizeForOps, noteUsesCustomMark, packageParagraphIds, packageResourcesOpOf, paragraphLength, paragraphLogicalText, paragraphVisibleText, parseDocumentBatch, physicalOffsetAtVisibleOffset, physicalPositionAtEditorOffset, planTrackedDeletion, planTrackedReplace, registerParagraphPropertySnapshot, revisionIdDemand, runsMergeable, sameStory, sectionPropertiesAt, storyBody, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch, withBodyContent, zeroWidthLeavesAt };
1648
+ export { type AddNoteOp, type AppliedDocumentOp, BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, type BatchRejectionReason, type BlockInsertionPoint, type CommentAnchor, type CommentState, type CreateCommentOp, type CreateHeaderFooterOp, type CreateNumberingInstanceOp, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, type DeleteBlocksOp, type DeleteColumnOp, type DeleteCommentOp, type DeleteNumberingInstanceOp, type DeleteRangeOp, type DeleteRowOp, type DeleteTableOp, type DocumentBatch, type DocumentOp, type DocumentOpEnvelope, DocumentOpRefusal, type DocumentOpRefusalReason, type DocumentOpType, DocumentOpsContractError, EMPTY_PROPERTY_SETS, type EditorIntent, type EditorIntentMode, type EmptyPropertySet, type FormattingPatch, type HeaderFooterStory, INHERIT_RUN_PROPS, type InlineSlice, type InsertBlocksOp, type InsertColumnOp, type InsertContentOp, type InsertRowOp, type InsertTableOp, type InsertTextOp, type InsertedRunProps, type JoinBlocksOp, type JoinInlineOp, MAX_BATCH_WIRE_BYTES, type MergeCellsOp, type NewIds, type NoteStory, type NumberingPartState, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, type OpStory, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, type PackageResourceEntry, type PackageResourceEntryChange, type PackageResourceMapChange, type PackageResourceMedia, type PackageResourcePart, type PackageResources, type ParagraphPropsPatch, type ParagraphReviewFields, type PlanTrackedDeletionOptions, type PlanTrackedReplaceOptions, REVISION_DECISIONS, type RemoveHeaderFooterOp, type RemoveNoteOp, type ReplaceBlocksOp, type ReplaceInlineOp, type ResolveRevisionOp, type RestoreCommentStateOp, type RestoreStoryPartsOp, type RevisionDecision, type RevisionStamp, type RunPropsPatch, SPLIT_HALVES, type SectionEndpoint, type SectionMapState, type SectionPropertiesState, type SectionViewEntry, type SectionViewState, type SequencedBatch, type SetCellPropsOp, type SetCommentResolutionOp, type SetContainerBlocksOp, type SetPackageResourcesOp, type SetParagraphPropsOp, type SetParagraphReviewOp, type SetRowPropsOp, type SetRunPropsOp, type SetSectionEndpointOp, type SetSectionPropsOp, type SetTableGridOp, type SetTableOp, type SetTablePropsOp, type SetTableRowsOp, type SplitBlockOp, type SplitCellOp, type SplitHalf, type SplitInlineOp, type SplitParagraphFields, type TableEditOp, type TableIntentOperation, type TextPosition, type TouchedBlocks, type UpdateCommentContentOp, allocateCommentAnchorIds, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, commentDocumentIssue, compareGaps, compileEditorIntent, createClient, createEditorIntentIdAllocator, createSequencer, defaultInsertionGap, documentStories, editorParagraphGroups, editorPositionAtPhysicalPosition, findStoryBody, formattingEquals, freshCommentId, idKey, inlineLeafSpans, isTextOnlyRun, mapTocBookmarkPosition, mergeRunContent, normalizeForOps, noteUsesCustomMark, packageParagraphIds, packageResourcesOpOf, paragraphLength, paragraphLogicalText, paragraphVisibleText, parseDocumentBatch, physicalOffsetAtVisibleOffset, physicalPositionAtEditorOffset, planTrackedDeletion, planTrackedReplace, reconcileDocumentPartRelationships, registerParagraphPropertySnapshot, revisionIdDemand, runsMergeable, sameStory, sectionPropertiesAt, storyBody, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch, withBodyContent, zeroWidthLeavesAt };