@office-open/docx 0.10.13 → 0.10.15

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.
@@ -1,4 +1,4 @@
1
- import { $ as parseRunProperties, C as FontWrapper, Q as stringifyRunProperties, Z as stringifyParagraphProperties, c as stringifyTableProperties, d as stringifyParagraphInline, et as stringifyElement, h as parseParagraph, i as parseTableRowPropertiesEl, it as WidthType, l as stringifyTableRowProperties, n as parseTableCellPropertiesEl, r as parseTablePropertiesEl, s as stringifyTableCellProperties, st as BorderStyle, xt as Media } from "./comments-BKbOd_pv.mjs";
1
+ import { $ as parseRunProperties, C as FontWrapper, Q as stringifyRunProperties, Z as stringifyParagraphProperties, c as stringifyTableProperties, d as stringifyParagraphInline, et as stringifyElement, h as parseParagraph, i as parseTableRowPropertiesEl, it as WidthType, l as stringifyTableRowProperties, n as parseTableCellPropertiesEl, r as parseTablePropertiesEl, s as stringifyTableCellProperties, st as BorderStyle, xt as Media } from "./comments-D_flVfxw.mjs";
2
2
  import { DOCX_PARTS, Relationships, appPropertiesDesc, buildContentTypeOverrides, convertInchesToTwip, customPropertiesDesc, decimalNumber, derivePasswordHash, uniqueNumericIdCreator } from "@office-open/core";
3
3
  import { attr, attrBool, attrMeasure, attrNum, escapeXml, findChild, js2xml, stringify, textOf, xml2js } from "@office-open/xml";
4
4
  import { ChartCollection } from "@office-open/core/chart";
@@ -448,8 +448,9 @@ function stringifyLevel(opts) {
448
448
  children.push(`<w:start w:val="${decimalNumber(opts.start ?? 1)}"/>`);
449
449
  if (opts.format) children.push(`<w:numFmt w:val="${opts.format}"/>`);
450
450
  if (opts.lvlRestart !== void 0) children.push(`<w:lvlRestart w:val="${decimalNumber(opts.lvlRestart)}"/>`);
451
- if (opts.suffix) children.push(`<w:suff w:val="${opts.suffix}"/>`);
451
+ if (opts.paragraphStyle !== void 0) children.push(`<w:pStyle w:val="${opts.paragraphStyle}"/>`);
452
452
  if (opts.isLegalNumberingStyle) children.push("<w:isLgl/>");
453
+ if (opts.suffix) children.push(`<w:suff w:val="${opts.suffix}"/>`);
453
454
  if (opts.text !== void 0 || opts.textNull) {
454
455
  const lvlTextAttrs = [];
455
456
  if (opts.text !== void 0) lvlTextAttrs.push(`w:val="${opts.text}"`);
@@ -555,6 +556,11 @@ function parseLevelEl(el, parseParagraphProperties, ctx) {
555
556
  const val = attr(suff, "w:val");
556
557
  if (val) opts.suffix = val;
557
558
  }
559
+ const pStyle = findChild(el, "w:pStyle");
560
+ if (pStyle) {
561
+ const val = attr(pStyle, "w:val");
562
+ if (val) opts.paragraphStyle = val;
563
+ }
558
564
  if (findChild(el, "w:isLgl")) opts.isLegalNumberingStyle = true;
559
565
  const lvlText = findChild(el, "w:lvlText");
560
566
  if (lvlText) {
@@ -725,15 +731,38 @@ function headingOverride(options, level) {
725
731
  default: return;
726
732
  }
727
733
  }
728
- /** Build `<w:docDefaults>` XML matching Word's default settings. */
729
- function stringifyDocDefaults(opts) {
734
+ /** Word's default `<w:rPrDefault>` theme fonts, kern, 11pt, ligatures. Injected
735
+ * for fresh documents (no structured docDefaults provided) to mirror Word's
736
+ * Normal.dotx. On round-trip the structured form is used instead. */
737
+ const WORD_DEFAULT_RPR_DEFAULT = "<w:rPrDefault><w:rPr><w:rFonts w:asciiTheme=\"minorHAnsi\" w:eastAsiaTheme=\"minorEastAsia\" w:hAnsiTheme=\"minorHAnsi\" w:cstheme=\"minorBidi\"/><w:kern w:val=\"2\"/><w:sz w:val=\"22\"/><w:szCs w:val=\"24\"/><w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\" w:bidi=\"ar-SA\"/><w14:ligatures w14:val=\"standardContextual\"/></w:rPr></w:rPrDefault>";
738
+ /** Word's default `<w:pPrDefault>` — widow/orphan control + 8pt after / 1.16 line.
739
+ * Injected for fresh documents only. */
740
+ const WORD_DEFAULT_PPR_DEFAULT = "<w:pPrDefault><w:pPr><w:widowControl/><w:spacing w:after=\"160\" w:line=\"278\" w:lineRule=\"auto\"/></w:pPr></w:pPrDefault>";
741
+ /**
742
+ * Build `<w:docDefaults>` XML from a structured {@link DocumentDefaultsOptions}.
743
+ *
744
+ * Three states per run/paragraph:
745
+ * - object → structured child element
746
+ * - null → explicit empty tag (`<w:pPrDefault/>`) — the source declared "no
747
+ * defaults here", so Word does NOT inject its own spacing. Distinct
748
+ * from absence (ECMA-376: empty pPrDefault suppresses default
749
+ * spacing; a missing pPrDefault is application-defined).
750
+ * - undefined → field is missing. On fresh generation (`injectDefaults`) we
751
+ * mirror Word's Normal.dotx; on round-trip we omit the element
752
+ * to preserve the source's "this default category is absent".
753
+ */
754
+ function stringifyDocDefaults(opts, injectDefaults = true) {
730
755
  const children = [];
731
- const rPr = stringifyRunProperties(opts.run);
732
- if (rPr) children.push(`<w:rPrDefault>${rPr}</w:rPrDefault>`);
733
- else children.push("<w:rPrDefault><w:rPr><w:rFonts w:asciiTheme=\"minorHAnsi\" w:eastAsiaTheme=\"minorEastAsia\" w:hAnsiTheme=\"minorHAnsi\" w:cstheme=\"minorBidi\"/><w:kern w:val=\"2\"/><w:sz w:val=\"22\"/><w:szCs w:val=\"24\"/><w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\" w:bidi=\"ar-SA\"/><w14:ligatures w14:val=\"standardContextual\"/></w:rPr></w:rPrDefault>");
734
- const pPr = stringifyParagraphProperties(opts.paragraph).xml;
735
- if (pPr) children.push(`<w:pPrDefault>${pPr}</w:pPrDefault>`);
736
- else children.push("<w:pPrDefault><w:pPr><w:widowControl/><w:spacing w:after=\"160\" w:line=\"278\" w:lineRule=\"auto\"/></w:pPr></w:pPrDefault>");
756
+ if (opts.run) {
757
+ const rPr = stringifyRunProperties(opts.run);
758
+ children.push(rPr ? `<w:rPrDefault>${rPr}</w:rPrDefault>` : `<w:rPrDefault/>`);
759
+ } else if (opts.run === null) children.push(`<w:rPrDefault/>`);
760
+ else if (injectDefaults) children.push(WORD_DEFAULT_RPR_DEFAULT);
761
+ if (opts.paragraph) {
762
+ const pPr = stringifyParagraphProperties(opts.paragraph).xml;
763
+ children.push(pPr ? `<w:pPrDefault>${pPr}</w:pPrDefault>` : `<w:pPrDefault/>`);
764
+ } else if (opts.paragraph === null) children.push(`<w:pPrDefault/>`);
765
+ else if (injectDefaults) children.push(WORD_DEFAULT_PPR_DEFAULT);
737
766
  return `<w:docDefaults>${children.join("")}</w:docDefaults>`;
738
767
  }
739
768
  let cachedDefaultStyles = null;
@@ -1480,16 +1509,16 @@ function parseDocDefaults(el, parseParagraphProperties, ctx) {
1480
1509
  const rPr = findChild(rPrDefault, "w:rPr");
1481
1510
  if (rPr) {
1482
1511
  const runDefaults = parseRunProperties(rPr);
1483
- if (Object.keys(runDefaults).length > 0) document.run = runDefaults;
1484
- }
1512
+ document.run = Object.keys(runDefaults).length > 0 ? runDefaults : null;
1513
+ } else document.run = null;
1485
1514
  }
1486
1515
  const pPrDefault = findChild(el, "w:pPrDefault");
1487
1516
  if (pPrDefault) {
1488
1517
  const pPr = findChild(pPrDefault, "w:pPr");
1489
1518
  if (pPr) {
1490
1519
  const paraDefaults = parseParagraphProperties(pPr, ctx);
1491
- if (Object.keys(paraDefaults).length > 0) document.paragraph = paraDefaults;
1492
- }
1520
+ document.paragraph = Object.keys(paraDefaults).length > 0 ? paraDefaults : null;
1521
+ } else document.paragraph = null;
1493
1522
  }
1494
1523
  return Object.keys(document).length > 0 ? { document } : void 0;
1495
1524
  }
@@ -2286,6 +2315,13 @@ function stringifyColorSchemeMapping(opts) {
2286
2315
  if (opts.followedHyperlink !== void 0) attrs["w:followedHyperlink"] = opts.followedHyperlink;
2287
2316
  return attrEl("w:clrSchemeMapping", attrs);
2288
2317
  }
2318
+ /** MS Office default compatSettings for a fresh Word 2013+ document. */
2319
+ const FRESH_COMPATIBILITY = {
2320
+ version: 15,
2321
+ overrideTableStyleFontSizeAndJustification: true,
2322
+ enableOpenTypeFeatures: true,
2323
+ doNotFlipMirrorIndents: true
2324
+ };
2289
2325
  function stringifyCompatibility(opts) {
2290
2326
  const p = [];
2291
2327
  if (opts.useSingleBorderforContiguousCells) p.push(onOff("w:useSingleBorderforContiguousCells", true));
@@ -2512,11 +2548,13 @@ const settingsDesc = {
2512
2548
  if (opts.hdrShapeDefaults !== void 0) p.push(`<w:hdrShapeDefaults>${opts.hdrShapeDefaults}</w:hdrShapeDefaults>`);
2513
2549
  if (opts.footnotePr !== void 0) p.push(stringifyFootnotePr(opts.footnotePr));
2514
2550
  if (opts.endnotePr !== void 0) p.push(stringifyEndnotePr(opts.endnotePr));
2515
- const compatXml = stringifyCompatibility({
2516
- ...opts.compatibility,
2517
- version: opts.compatibility?.version ?? opts.compatibilityModeVersion
2518
- });
2519
- if (compatXml) p.push(compatXml);
2551
+ if (opts.compatibility !== false) {
2552
+ const compatXml = stringifyCompatibility(opts.compatibility === void 0 ? FRESH_COMPATIBILITY : {
2553
+ ...opts.compatibility,
2554
+ version: opts.compatibility.version ?? opts.compatibilityModeVersion ?? 15
2555
+ });
2556
+ if (compatXml) p.push(compatXml);
2557
+ }
2520
2558
  if (opts.docVars?.length) {
2521
2559
  const vars = opts.docVars.map((v) => attrEl("w:docVar", {
2522
2560
  "w:name": v.name,
@@ -2552,6 +2590,9 @@ const settingsDesc = {
2552
2590
  p.push(onOff("w:doNotEmbedSmartTags", opts.doNotEmbedSmartTags));
2553
2591
  p.push(strVal("w:decimalSymbol", opts.decimalSymbol));
2554
2592
  p.push(strVal("w:listSeparator", opts.listSeparator));
2593
+ if (opts.w14DocId !== void 0) p.push(strVal("w14:docId", opts.w14DocId));
2594
+ if (opts.w15ChartTrackingRefBased) p.push(`<w15:chartTrackingRefBased/>`);
2595
+ if (opts.w15DocId !== void 0) p.push(strVal("w15:docId", opts.w15DocId));
2555
2596
  return `<w:settings ${SETTINGS_NS}>${p.join("")}</w:settings>`;
2556
2597
  },
2557
2598
  parse(el, _ctx) {
@@ -2795,10 +2836,7 @@ const settingsDesc = {
2795
2836
  if (en) opts.endnotePr = en;
2796
2837
  }
2797
2838
  const compatEl = findChild(el, "w:compat");
2798
- if (compatEl) {
2799
- const compat = parseCompatibility(compatEl);
2800
- if (compat) opts.compatibility = compat;
2801
- }
2839
+ opts.compatibility = (compatEl ? parseCompatibility(compatEl) : void 0) ?? false;
2802
2840
  const docVarsEl = findChild(el, "w:docVars");
2803
2841
  if (docVarsEl) {
2804
2842
  const vars = [];
@@ -2904,6 +2942,11 @@ const settingsDesc = {
2904
2942
  if (decimalSymbol) opts.decimalSymbol = decimalSymbol;
2905
2943
  const listSeparator = readStr(findChild(el, "w:listSeparator"), "w:val");
2906
2944
  if (listSeparator) opts.listSeparator = listSeparator;
2945
+ const w14DocId = readStr(findChild(el, "w14:docId"), "w14:val");
2946
+ if (w14DocId) opts.w14DocId = w14DocId;
2947
+ if (findChild(el, "w15:chartTrackingRefBased")) opts.w15ChartTrackingRefBased = true;
2948
+ const w15DocId = readStr(findChild(el, "w15:docId"), "w15:val");
2949
+ if (w15DocId) opts.w15DocId = w15DocId;
2907
2950
  return opts;
2908
2951
  }
2909
2952
  };
@@ -2933,9 +2976,15 @@ const footnotesDesc = {
2933
2976
  for (const [i, para] of paragraphs.entries()) {
2934
2977
  const pXml = stringifyParagraphInline(para, ctx);
2935
2978
  if (i === 0) {
2936
- const openIdx = pXml.indexOf(">");
2937
- if (openIdx !== -1) parts.push(pXml.slice(0, openIdx + 1) + FOOTNOTE_REF_RUN + pXml.slice(openIdx + 1));
2938
- else parts.push(pXml);
2979
+ const pPrEnd = pXml.indexOf("</w:pPr>");
2980
+ if (pPrEnd !== -1) {
2981
+ const at = pPrEnd + 8;
2982
+ parts.push(pXml.slice(0, at) + FOOTNOTE_REF_RUN + pXml.slice(at));
2983
+ } else {
2984
+ const openIdx = pXml.indexOf(">");
2985
+ if (openIdx !== -1) parts.push(pXml.slice(0, openIdx + 1) + FOOTNOTE_REF_RUN + pXml.slice(openIdx + 1));
2986
+ else parts.push(pXml);
2987
+ }
2939
2988
  } else parts.push(pXml);
2940
2989
  }
2941
2990
  parts.push("</w:footnote>");
@@ -3592,6 +3641,26 @@ function withAltChunkOverrides(input, altChunks) {
3592
3641
  overrides
3593
3642
  };
3594
3643
  }
3644
+ const CUSTOM_PROPERTIES_PART_NAME = "/docProps/custom.xml";
3645
+ const CUSTOM_PROPERTIES_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.custom-properties+xml";
3646
+ /**
3647
+ * Ensure the docProps/custom.xml Override exists on a round-tripped package.
3648
+ * The custom-properties part is always written (even when empty), but a source
3649
+ * [Content_Types] may omit its Override — the part then resolves only through
3650
+ * the generic `application/xml` Default, which Word rejects as unreadable
3651
+ * content (OPC O5). Mirror the unconditional part emission by forcing the
3652
+ * Override whenever it is missing.
3653
+ */
3654
+ function ensureCustomPropertiesOverride(input) {
3655
+ if (input.overrides.some((o) => o.partName.toLowerCase() === CUSTOM_PROPERTIES_PART_NAME)) return input;
3656
+ return {
3657
+ ...input,
3658
+ overrides: [...input.overrides, {
3659
+ partName: CUSTOM_PROPERTIES_PART_NAME,
3660
+ contentType: CUSTOM_PROPERTIES_CONTENT_TYPE
3661
+ }]
3662
+ };
3663
+ }
3595
3664
  /**
3596
3665
  * Build [Content_Types].xml for a fresh DOCX compile, driven by the part
3597
3666
  * registry. Static parts (document/styles/…/comments/headers/…) come from
@@ -4232,7 +4301,7 @@ var DocxWriteContext = class {
4232
4301
  compatibility: options.compatibility,
4233
4302
  compatibilityModeVersion: options.compatabilityModeVersion,
4234
4303
  defaultTabStop: options.defaultTabStop,
4235
- evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
4304
+ evenAndOddHeaders: options.evenAndOddHeaderAndFooters || void 0,
4236
4305
  characterSpacingControl: options.characterSpacingControl,
4237
4306
  hyphenation: {
4238
4307
  autoHyphenation: options.hyphenation?.autoHyphenation,
@@ -4282,7 +4351,7 @@ var DocxWriteContext = class {
4282
4351
  const s = options.styles;
4283
4352
  if (s.roundTripped) {
4284
4353
  const f = new DefaultStylesFactory().newInstance({});
4285
- const docDefaults = s.docDefaultsXml ?? f.importedStyles?.[0]?._raw ?? "";
4354
+ const docDefaults = s.default?.document !== void 0 ? stringifyDocDefaults(s.default.document, false) : s.docDefaultsXml ?? f.importedStyles?.[0]?._raw ?? "";
4286
4355
  const latentStyles = s.latentStylesXml ?? f.importedStyles?.[1]?._raw ?? "";
4287
4356
  this.styles = new Styles({
4288
4357
  importedStyles: [{ _raw: docDefaults }, { _raw: latentStyles }],
@@ -4462,6 +4531,6 @@ var DocxReadContext = class {
4462
4531
  }
4463
4532
  };
4464
4533
  //#endregion
4465
- export { DefaultStylesFactory as A, AlignmentType as B, footnotesDesc as C, buildStyleCache as D, buildNumberingCache as E, stringifyTableStyle as F, Numbering as I, parseNumberingDefinitions as L, stringifyConditionalTableStyle as M, stringifyNumberingStyle as N, extractStyleId as O, stringifyParagraphStyle as P, LevelFormat as R, endnotesDesc as S, Styles as T, glossaryDesc as _, framesetXml as a, SubDocCollection as b, customPropertiesDesc as c, contentTypesDesc as d, withAltChunkOverrides as f, DocPartType as g, DocPartGallery as h, frameXml as i, stringifyCharacterStyle as j, parseStyleDefinitions as k, corePropertiesDesc as l, DocPartBehavior as m, DocxWriteContext as n, webSettingsDesc as o, withMediaDefaults as p, TargetScreenSize as r, appPropertiesDesc as s, DocxReadContext as t, buildContentTypesFromRegistry as u, bibliographyDesc as v, settingsDesc as w, AltChunkCollection as x, fontTableDesc as y, LevelSuffix as z };
4534
+ export { parseStyleDefinitions as A, LevelFormat as B, endnotesDesc as C, buildNumberingCache as D, Styles as E, stringifyNumberingStyle as F, AlignmentType as H, stringifyParagraphStyle as I, stringifyTableStyle as L, stringifyCharacterStyle as M, stringifyConditionalTableStyle as N, buildStyleCache as O, stringifyDocDefaults as P, Numbering as R, AltChunkCollection as S, settingsDesc as T, LevelSuffix as V, DocPartType as _, framesetXml as a, fontTableDesc as b, customPropertiesDesc as c, contentTypesDesc as d, ensureCustomPropertiesOverride as f, DocPartGallery as g, DocPartBehavior as h, frameXml as i, DefaultStylesFactory as j, extractStyleId as k, corePropertiesDesc as l, withMediaDefaults as m, DocxWriteContext as n, webSettingsDesc as o, withAltChunkOverrides as p, TargetScreenSize as r, appPropertiesDesc as s, DocxReadContext as t, buildContentTypesFromRegistry as u, glossaryDesc as v, footnotesDesc as w, SubDocCollection as x, bibliographyDesc as y, parseNumberingDefinitions as z };
4466
4535
 
4467
- //# sourceMappingURL=context-B4Ffx12w.mjs.map
4536
+ //# sourceMappingURL=context-DLmSgNzb.mjs.map