@office-open/docx 0.10.13 → 0.10.14

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-Cn9Q0iT6.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
  }
@@ -2514,7 +2543,7 @@ const settingsDesc = {
2514
2543
  if (opts.endnotePr !== void 0) p.push(stringifyEndnotePr(opts.endnotePr));
2515
2544
  const compatXml = stringifyCompatibility({
2516
2545
  ...opts.compatibility,
2517
- version: opts.compatibility?.version ?? opts.compatibilityModeVersion
2546
+ version: opts.compatibility?.version ?? opts.compatibilityModeVersion ?? 15
2518
2547
  });
2519
2548
  if (compatXml) p.push(compatXml);
2520
2549
  if (opts.docVars?.length) {
@@ -2552,6 +2581,9 @@ const settingsDesc = {
2552
2581
  p.push(onOff("w:doNotEmbedSmartTags", opts.doNotEmbedSmartTags));
2553
2582
  p.push(strVal("w:decimalSymbol", opts.decimalSymbol));
2554
2583
  p.push(strVal("w:listSeparator", opts.listSeparator));
2584
+ if (opts.w14DocId !== void 0) p.push(strVal("w14:docId", opts.w14DocId));
2585
+ if (opts.w15ChartTrackingRefBased) p.push(`<w15:chartTrackingRefBased/>`);
2586
+ if (opts.w15DocId !== void 0) p.push(strVal("w15:docId", opts.w15DocId));
2555
2587
  return `<w:settings ${SETTINGS_NS}>${p.join("")}</w:settings>`;
2556
2588
  },
2557
2589
  parse(el, _ctx) {
@@ -2904,6 +2936,11 @@ const settingsDesc = {
2904
2936
  if (decimalSymbol) opts.decimalSymbol = decimalSymbol;
2905
2937
  const listSeparator = readStr(findChild(el, "w:listSeparator"), "w:val");
2906
2938
  if (listSeparator) opts.listSeparator = listSeparator;
2939
+ const w14DocId = readStr(findChild(el, "w14:docId"), "w14:val");
2940
+ if (w14DocId) opts.w14DocId = w14DocId;
2941
+ if (findChild(el, "w15:chartTrackingRefBased")) opts.w15ChartTrackingRefBased = true;
2942
+ const w15DocId = readStr(findChild(el, "w15:docId"), "w15:val");
2943
+ if (w15DocId) opts.w15DocId = w15DocId;
2907
2944
  return opts;
2908
2945
  }
2909
2946
  };
@@ -2933,9 +2970,15 @@ const footnotesDesc = {
2933
2970
  for (const [i, para] of paragraphs.entries()) {
2934
2971
  const pXml = stringifyParagraphInline(para, ctx);
2935
2972
  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);
2973
+ const pPrEnd = pXml.indexOf("</w:pPr>");
2974
+ if (pPrEnd !== -1) {
2975
+ const at = pPrEnd + 8;
2976
+ parts.push(pXml.slice(0, at) + FOOTNOTE_REF_RUN + pXml.slice(at));
2977
+ } else {
2978
+ const openIdx = pXml.indexOf(">");
2979
+ if (openIdx !== -1) parts.push(pXml.slice(0, openIdx + 1) + FOOTNOTE_REF_RUN + pXml.slice(openIdx + 1));
2980
+ else parts.push(pXml);
2981
+ }
2939
2982
  } else parts.push(pXml);
2940
2983
  }
2941
2984
  parts.push("</w:footnote>");
@@ -3592,6 +3635,26 @@ function withAltChunkOverrides(input, altChunks) {
3592
3635
  overrides
3593
3636
  };
3594
3637
  }
3638
+ const CUSTOM_PROPERTIES_PART_NAME = "/docProps/custom.xml";
3639
+ const CUSTOM_PROPERTIES_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.custom-properties+xml";
3640
+ /**
3641
+ * Ensure the docProps/custom.xml Override exists on a round-tripped package.
3642
+ * The custom-properties part is always written (even when empty), but a source
3643
+ * [Content_Types] may omit its Override — the part then resolves only through
3644
+ * the generic `application/xml` Default, which Word rejects as unreadable
3645
+ * content (OPC O5). Mirror the unconditional part emission by forcing the
3646
+ * Override whenever it is missing.
3647
+ */
3648
+ function ensureCustomPropertiesOverride(input) {
3649
+ if (input.overrides.some((o) => o.partName.toLowerCase() === CUSTOM_PROPERTIES_PART_NAME)) return input;
3650
+ return {
3651
+ ...input,
3652
+ overrides: [...input.overrides, {
3653
+ partName: CUSTOM_PROPERTIES_PART_NAME,
3654
+ contentType: CUSTOM_PROPERTIES_CONTENT_TYPE
3655
+ }]
3656
+ };
3657
+ }
3595
3658
  /**
3596
3659
  * Build [Content_Types].xml for a fresh DOCX compile, driven by the part
3597
3660
  * registry. Static parts (document/styles/…/comments/headers/…) come from
@@ -4232,7 +4295,7 @@ var DocxWriteContext = class {
4232
4295
  compatibility: options.compatibility,
4233
4296
  compatibilityModeVersion: options.compatabilityModeVersion,
4234
4297
  defaultTabStop: options.defaultTabStop,
4235
- evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
4298
+ evenAndOddHeaders: options.evenAndOddHeaderAndFooters || void 0,
4236
4299
  characterSpacingControl: options.characterSpacingControl,
4237
4300
  hyphenation: {
4238
4301
  autoHyphenation: options.hyphenation?.autoHyphenation,
@@ -4282,7 +4345,7 @@ var DocxWriteContext = class {
4282
4345
  const s = options.styles;
4283
4346
  if (s.roundTripped) {
4284
4347
  const f = new DefaultStylesFactory().newInstance({});
4285
- const docDefaults = s.docDefaultsXml ?? f.importedStyles?.[0]?._raw ?? "";
4348
+ const docDefaults = s.default?.document !== void 0 ? stringifyDocDefaults(s.default.document, false) : s.docDefaultsXml ?? f.importedStyles?.[0]?._raw ?? "";
4286
4349
  const latentStyles = s.latentStylesXml ?? f.importedStyles?.[1]?._raw ?? "";
4287
4350
  this.styles = new Styles({
4288
4351
  importedStyles: [{ _raw: docDefaults }, { _raw: latentStyles }],
@@ -4462,6 +4525,6 @@ var DocxReadContext = class {
4462
4525
  }
4463
4526
  };
4464
4527
  //#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 };
4528
+ 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
4529
 
4467
- //# sourceMappingURL=context-B4Ffx12w.mjs.map
4530
+ //# sourceMappingURL=context-B5k1UabT.mjs.map