odf.js 1.1.0 → 1.3.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.cjs +974 -9
- package/dist/index.d.cts +139 -1
- package/dist/index.d.ts +139 -1
- package/dist/index.js +932 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { XMLBuilder, XMLParser } from "fast-xml-parser";
|
|
3
3
|
import { unzipSync, zipSync } from "fflate";
|
|
4
|
+
import { AlignmentSchema, AlignmentSchema as AlignmentSchema$1, ColorSchema, colorToRgbHex, rgbHexToColor } from "document-content-model";
|
|
4
5
|
//#region src/model/node.ts
|
|
5
6
|
const AttributeSchema = z.object({
|
|
6
7
|
name: z.string(),
|
|
@@ -447,6 +448,17 @@ function txt(value) {
|
|
|
447
448
|
function encodeXmlText(value) {
|
|
448
449
|
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
449
450
|
}
|
|
451
|
+
const XML_ENTITY_PATTERN = /&(?:amp|lt|gt|quot|apos);/g;
|
|
452
|
+
const XML_ENTITY_DECODE = {
|
|
453
|
+
"&": "&",
|
|
454
|
+
"<": "<",
|
|
455
|
+
">": ">",
|
|
456
|
+
""": "\"",
|
|
457
|
+
"'": "'"
|
|
458
|
+
};
|
|
459
|
+
function decodeXmlText(value) {
|
|
460
|
+
return value.replace(XML_ENTITY_PATTERN, (entity) => XML_ENTITY_DECODE[entity]);
|
|
461
|
+
}
|
|
450
462
|
//#endregion
|
|
451
463
|
//#region src/manifest.ts
|
|
452
464
|
const ManifestEntrySchema = z.object({
|
|
@@ -470,26 +482,26 @@ const STANDARD_XML_PART_NAMES = /* @__PURE__ */ new Set([
|
|
|
470
482
|
"meta.xml",
|
|
471
483
|
"settings.xml"
|
|
472
484
|
]);
|
|
473
|
-
function findChildElement(nodes, tag) {
|
|
485
|
+
function findChildElement$1(nodes, tag) {
|
|
474
486
|
for (const node of nodes) if (node.type === "element" && node.tag === tag) return node;
|
|
475
487
|
}
|
|
476
|
-
function attrValue(element, name) {
|
|
488
|
+
function attrValue$2(element, name) {
|
|
477
489
|
return element.attributes.find((attribute) => attribute.name === name)?.value;
|
|
478
490
|
}
|
|
479
491
|
function readManifest(pkg) {
|
|
480
492
|
const part = pkg.parts[MANIFEST_PART];
|
|
481
493
|
if (part?.kind !== "xml") throw new Error(`package has no ${MANIFEST_PART} XML part to read`);
|
|
482
|
-
const root = findChildElement(part.nodes, "manifest:manifest");
|
|
494
|
+
const root = findChildElement$1(part.nodes, "manifest:manifest");
|
|
483
495
|
if (root === void 0) throw new Error(`${MANIFEST_PART} has no manifest:manifest root element`);
|
|
484
|
-
const version = attrValue(root, "manifest:version");
|
|
496
|
+
const version = attrValue$2(root, "manifest:version");
|
|
485
497
|
if (version === void 0) throw new Error(`${MANIFEST_PART}'s manifest:manifest root is missing the required manifest:version attribute`);
|
|
486
498
|
const entries = [];
|
|
487
499
|
for (const child of root.children) {
|
|
488
500
|
if (child.type !== "element" || child.tag !== "manifest:file-entry") continue;
|
|
489
|
-
const fullPath = attrValue(child, "manifest:full-path");
|
|
490
|
-
const mediaType = attrValue(child, "manifest:media-type");
|
|
501
|
+
const fullPath = attrValue$2(child, "manifest:full-path");
|
|
502
|
+
const mediaType = attrValue$2(child, "manifest:media-type");
|
|
491
503
|
if (fullPath === void 0 || mediaType === void 0) throw new Error(`${MANIFEST_PART} has a manifest:file-entry missing manifest:full-path or manifest:media-type`);
|
|
492
|
-
const entryVersion = attrValue(child, "manifest:version");
|
|
504
|
+
const entryVersion = attrValue$2(child, "manifest:version");
|
|
493
505
|
entries.push(entryVersion === void 0 ? {
|
|
494
506
|
fullPath,
|
|
495
507
|
mediaType
|
|
@@ -599,7 +611,7 @@ function validateManifest(pkg) {
|
|
|
599
611
|
});
|
|
600
612
|
return problems;
|
|
601
613
|
}
|
|
602
|
-
const root = findChildElement(manifestPart.nodes, "manifest:manifest");
|
|
614
|
+
const root = findChildElement$1(manifestPart.nodes, "manifest:manifest");
|
|
603
615
|
if (root === void 0) {
|
|
604
616
|
problems.push({
|
|
605
617
|
severity: "error",
|
|
@@ -648,7 +660,7 @@ function validateManifest(pkg) {
|
|
|
648
660
|
for (const child of root.children) {
|
|
649
661
|
if (child.type !== "element" || child.tag !== "manifest:file-entry") continue;
|
|
650
662
|
if (!child.children.some((grandchild) => grandchild.type === "element" && grandchild.tag === "manifest:encryption-data")) continue;
|
|
651
|
-
const fullPath = attrValue(child, "manifest:full-path");
|
|
663
|
+
const fullPath = attrValue$2(child, "manifest:full-path");
|
|
652
664
|
if (fullPath === void 0) continue;
|
|
653
665
|
problems.push({
|
|
654
666
|
severity: "warning",
|
|
@@ -680,4 +692,914 @@ function setDocumentMediaType(pkg, mediaType, version = DEFAULT_MANIFEST_VERSION
|
|
|
680
692
|
});
|
|
681
693
|
}
|
|
682
694
|
//#endregion
|
|
683
|
-
|
|
695
|
+
//#region src/typed/shared/units.ts
|
|
696
|
+
const LENGTH_PATTERN = /^(-?(?:\d+(?:\.\d+)?|\.\d+))(cm|mm|in|pt|pc|px)$/;
|
|
697
|
+
const POINTS_PER_INCH = 72;
|
|
698
|
+
const POINTS_PER_PICA = 12;
|
|
699
|
+
const CM_PER_INCH = 2.54;
|
|
700
|
+
const MM_PER_INCH = 25.4;
|
|
701
|
+
const CSS_REFERENCE_PIXELS_PER_INCH = 96;
|
|
702
|
+
function unitToPtFactor(unit) {
|
|
703
|
+
switch (unit) {
|
|
704
|
+
case "pt": return 1;
|
|
705
|
+
case "in": return POINTS_PER_INCH;
|
|
706
|
+
case "cm": return POINTS_PER_INCH / CM_PER_INCH;
|
|
707
|
+
case "mm": return POINTS_PER_INCH / MM_PER_INCH;
|
|
708
|
+
case "pc": return POINTS_PER_PICA;
|
|
709
|
+
case "px": return POINTS_PER_INCH / CSS_REFERENCE_PIXELS_PER_INCH;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
function isLengthUnit(value) {
|
|
713
|
+
return value === "cm" || value === "mm" || value === "in" || value === "pt" || value === "pc" || value === "px";
|
|
714
|
+
}
|
|
715
|
+
function parseOdfLength(value) {
|
|
716
|
+
const match = LENGTH_PATTERN.exec(value);
|
|
717
|
+
if (match === null) return;
|
|
718
|
+
const numeric = match[1];
|
|
719
|
+
const unit = match[2];
|
|
720
|
+
if (numeric === void 0 || unit === void 0 || !isLengthUnit(unit)) return;
|
|
721
|
+
return Number(numeric) * unitToPtFactor(unit);
|
|
722
|
+
}
|
|
723
|
+
function formatOdfLength(pt, unit = "pt") {
|
|
724
|
+
return `${pt / unitToPtFactor(unit)}${unit}`;
|
|
725
|
+
}
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region src/typed/shared/color.ts
|
|
728
|
+
const ODF_COLOR_PATTERN = /^#[0-9a-fA-F]{6}$/;
|
|
729
|
+
function parseOdfColor(value) {
|
|
730
|
+
if (!ODF_COLOR_PATTERN.test(value)) return;
|
|
731
|
+
return rgbHexToColor(value);
|
|
732
|
+
}
|
|
733
|
+
function formatOdfColor(color) {
|
|
734
|
+
return `#${colorToRgbHex(color)}`;
|
|
735
|
+
}
|
|
736
|
+
//#endregion
|
|
737
|
+
//#region src/styles/properties.ts
|
|
738
|
+
const StylePropertiesSchema = z.object({
|
|
739
|
+
bold: z.boolean().optional(),
|
|
740
|
+
italic: z.boolean().optional(),
|
|
741
|
+
underline: z.boolean().optional(),
|
|
742
|
+
strike: z.boolean().optional(),
|
|
743
|
+
fontFamily: z.string().optional(),
|
|
744
|
+
sizePt: z.number().optional(),
|
|
745
|
+
color: ColorSchema.optional(),
|
|
746
|
+
alignment: AlignmentSchema$1.optional(),
|
|
747
|
+
spacingBeforePt: z.number().optional(),
|
|
748
|
+
spacingAfterPt: z.number().optional(),
|
|
749
|
+
lineSpacing: z.number().optional(),
|
|
750
|
+
indentLeftPt: z.number().optional(),
|
|
751
|
+
indentFirstLinePt: z.number().optional()
|
|
752
|
+
});
|
|
753
|
+
const ATTR = {
|
|
754
|
+
fontWeight: "fo:font-weight",
|
|
755
|
+
fontStyle: "fo:font-style",
|
|
756
|
+
underlineStyle: "style:text-underline-style",
|
|
757
|
+
underlineWidth: "style:text-underline-width",
|
|
758
|
+
underlineColor: "style:text-underline-color",
|
|
759
|
+
lineThroughStyle: "style:text-line-through-style",
|
|
760
|
+
lineThroughType: "style:text-line-through-type",
|
|
761
|
+
fontFamily: "fo:font-family",
|
|
762
|
+
fontSize: "fo:font-size",
|
|
763
|
+
color: "fo:color",
|
|
764
|
+
textAlign: "fo:text-align",
|
|
765
|
+
marginTop: "fo:margin-top",
|
|
766
|
+
marginBottom: "fo:margin-bottom",
|
|
767
|
+
lineHeight: "fo:line-height",
|
|
768
|
+
marginLeft: "fo:margin-left",
|
|
769
|
+
textIndent: "fo:text-indent"
|
|
770
|
+
};
|
|
771
|
+
const TEXT_ATTR_NAMES = /* @__PURE__ */ new Set([
|
|
772
|
+
ATTR.fontWeight,
|
|
773
|
+
ATTR.fontStyle,
|
|
774
|
+
ATTR.underlineStyle,
|
|
775
|
+
ATTR.underlineWidth,
|
|
776
|
+
ATTR.underlineColor,
|
|
777
|
+
ATTR.lineThroughStyle,
|
|
778
|
+
ATTR.lineThroughType,
|
|
779
|
+
ATTR.fontFamily,
|
|
780
|
+
ATTR.fontSize,
|
|
781
|
+
ATTR.color
|
|
782
|
+
]);
|
|
783
|
+
const PARAGRAPH_ATTR_NAMES = /* @__PURE__ */ new Set([
|
|
784
|
+
ATTR.textAlign,
|
|
785
|
+
ATTR.marginTop,
|
|
786
|
+
ATTR.marginBottom,
|
|
787
|
+
ATTR.lineHeight,
|
|
788
|
+
ATTR.marginLeft,
|
|
789
|
+
ATTR.textIndent
|
|
790
|
+
]);
|
|
791
|
+
function attributeMap(element) {
|
|
792
|
+
const map = /* @__PURE__ */ new Map();
|
|
793
|
+
for (const attribute of element.attributes) map.set(attribute.name, attribute.value);
|
|
794
|
+
return map;
|
|
795
|
+
}
|
|
796
|
+
const parseLength = parseOdfLength;
|
|
797
|
+
function formatPt(valuePt) {
|
|
798
|
+
return formatOdfLength(valuePt, "pt");
|
|
799
|
+
}
|
|
800
|
+
const PERCENTAGE_PATTERN = /^(-?(?:\d+(?:\.\d+)?|\.\d+))%$/;
|
|
801
|
+
function parsePercentageMultiplier(value) {
|
|
802
|
+
const match = PERCENTAGE_PATTERN.exec(value);
|
|
803
|
+
if (match === null) return;
|
|
804
|
+
const numeric = match[1];
|
|
805
|
+
if (numeric === void 0) return;
|
|
806
|
+
return Number(numeric) / 100;
|
|
807
|
+
}
|
|
808
|
+
function formatPercentageMultiplier(multiplier) {
|
|
809
|
+
return `${multiplier * 100}%`;
|
|
810
|
+
}
|
|
811
|
+
const parseColor = parseOdfColor;
|
|
812
|
+
const formatColor = formatOdfColor;
|
|
813
|
+
function parseLineDecoration(style, companionA, companionAOnValue, companionB, companionBOnValue) {
|
|
814
|
+
if (style === void 0 && companionA === void 0 && companionB === void 0) return;
|
|
815
|
+
if (style === "solid" && (companionA === void 0 || companionA === companionAOnValue) && (companionB === void 0 || companionB === companionBOnValue)) return true;
|
|
816
|
+
if (style === "none" && companionA === void 0 && companionB === void 0) return false;
|
|
817
|
+
return "unknown";
|
|
818
|
+
}
|
|
819
|
+
const RISKY_STYLE_ELEMENT_ATTRS = /* @__PURE__ */ new Set(["style:master-page-name", "style:next-style-name"]);
|
|
820
|
+
function parseTextProperties(element) {
|
|
821
|
+
const attrs = attributeMap(element);
|
|
822
|
+
const properties = {};
|
|
823
|
+
let hasUnknown = false;
|
|
824
|
+
for (const name of attrs.keys()) if (!TEXT_ATTR_NAMES.has(name)) hasUnknown = true;
|
|
825
|
+
const fontWeight = attrs.get(ATTR.fontWeight);
|
|
826
|
+
if (fontWeight === "bold") properties.bold = true;
|
|
827
|
+
else if (fontWeight === "normal") properties.bold = false;
|
|
828
|
+
else if (fontWeight !== void 0) hasUnknown = true;
|
|
829
|
+
const fontStyle = attrs.get(ATTR.fontStyle);
|
|
830
|
+
if (fontStyle === "italic") properties.italic = true;
|
|
831
|
+
else if (fontStyle === "normal") properties.italic = false;
|
|
832
|
+
else if (fontStyle !== void 0) hasUnknown = true;
|
|
833
|
+
const underline = parseLineDecoration(attrs.get(ATTR.underlineStyle), attrs.get(ATTR.underlineWidth), "auto", attrs.get(ATTR.underlineColor), "font-color");
|
|
834
|
+
if (underline === "unknown") hasUnknown = true;
|
|
835
|
+
else if (underline !== void 0) properties.underline = underline;
|
|
836
|
+
const strike = parseLineDecoration(attrs.get(ATTR.lineThroughStyle), attrs.get(ATTR.lineThroughType), "single", void 0, "");
|
|
837
|
+
if (strike === "unknown") hasUnknown = true;
|
|
838
|
+
else if (strike !== void 0) properties.strike = strike;
|
|
839
|
+
const fontFamily = attrs.get(ATTR.fontFamily);
|
|
840
|
+
if (fontFamily !== void 0) properties.fontFamily = fontFamily;
|
|
841
|
+
const fontSize = attrs.get(ATTR.fontSize);
|
|
842
|
+
if (fontSize !== void 0) {
|
|
843
|
+
const pt = parseLength(fontSize);
|
|
844
|
+
if (pt === void 0) hasUnknown = true;
|
|
845
|
+
else properties.sizePt = pt;
|
|
846
|
+
}
|
|
847
|
+
const color = attrs.get(ATTR.color);
|
|
848
|
+
if (color !== void 0) {
|
|
849
|
+
const parsed = parseColor(color);
|
|
850
|
+
if (parsed === void 0) hasUnknown = true;
|
|
851
|
+
else properties.color = parsed;
|
|
852
|
+
}
|
|
853
|
+
return {
|
|
854
|
+
properties,
|
|
855
|
+
hasUnknown
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
function parseParagraphProperties(element) {
|
|
859
|
+
const attrs = attributeMap(element);
|
|
860
|
+
const properties = {};
|
|
861
|
+
let hasUnknown = false;
|
|
862
|
+
for (const name of attrs.keys()) if (!PARAGRAPH_ATTR_NAMES.has(name)) hasUnknown = true;
|
|
863
|
+
const textAlign = attrs.get(ATTR.textAlign);
|
|
864
|
+
if (textAlign === "left" || textAlign === "center" || textAlign === "right" || textAlign === "justify") properties.alignment = textAlign;
|
|
865
|
+
else if (textAlign !== void 0) hasUnknown = true;
|
|
866
|
+
const marginTop = attrs.get(ATTR.marginTop);
|
|
867
|
+
if (marginTop !== void 0) {
|
|
868
|
+
const pt = parseLength(marginTop);
|
|
869
|
+
if (pt === void 0) hasUnknown = true;
|
|
870
|
+
else properties.spacingBeforePt = pt;
|
|
871
|
+
}
|
|
872
|
+
const marginBottom = attrs.get(ATTR.marginBottom);
|
|
873
|
+
if (marginBottom !== void 0) {
|
|
874
|
+
const pt = parseLength(marginBottom);
|
|
875
|
+
if (pt === void 0) hasUnknown = true;
|
|
876
|
+
else properties.spacingAfterPt = pt;
|
|
877
|
+
}
|
|
878
|
+
const marginLeft = attrs.get(ATTR.marginLeft);
|
|
879
|
+
if (marginLeft !== void 0) {
|
|
880
|
+
const pt = parseLength(marginLeft);
|
|
881
|
+
if (pt === void 0) hasUnknown = true;
|
|
882
|
+
else properties.indentLeftPt = pt;
|
|
883
|
+
}
|
|
884
|
+
const textIndent = attrs.get(ATTR.textIndent);
|
|
885
|
+
if (textIndent !== void 0) {
|
|
886
|
+
const pt = parseLength(textIndent);
|
|
887
|
+
if (pt === void 0) hasUnknown = true;
|
|
888
|
+
else properties.indentFirstLinePt = pt;
|
|
889
|
+
}
|
|
890
|
+
const lineHeight = attrs.get(ATTR.lineHeight);
|
|
891
|
+
if (lineHeight !== void 0) {
|
|
892
|
+
const multiplier = parsePercentageMultiplier(lineHeight);
|
|
893
|
+
if (multiplier === void 0) hasUnknown = true;
|
|
894
|
+
else properties.lineSpacing = multiplier;
|
|
895
|
+
}
|
|
896
|
+
return {
|
|
897
|
+
properties,
|
|
898
|
+
hasUnknown
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
function parseStyleElementProperties(styleElement) {
|
|
902
|
+
let properties = {};
|
|
903
|
+
let hasUnknown = false;
|
|
904
|
+
for (const attribute of styleElement.attributes) if (RISKY_STYLE_ELEMENT_ATTRS.has(attribute.name)) hasUnknown = true;
|
|
905
|
+
for (const child of styleElement.children) {
|
|
906
|
+
if (child.type !== "element") continue;
|
|
907
|
+
if (child.tag === "style:text-properties") {
|
|
908
|
+
const result = parseTextProperties(child);
|
|
909
|
+
properties = {
|
|
910
|
+
...properties,
|
|
911
|
+
...result.properties
|
|
912
|
+
};
|
|
913
|
+
if (result.hasUnknown) hasUnknown = true;
|
|
914
|
+
} else if (child.tag === "style:paragraph-properties") {
|
|
915
|
+
const result = parseParagraphProperties(child);
|
|
916
|
+
properties = {
|
|
917
|
+
...properties,
|
|
918
|
+
...result.properties
|
|
919
|
+
};
|
|
920
|
+
if (result.hasUnknown) hasUnknown = true;
|
|
921
|
+
} else hasUnknown = true;
|
|
922
|
+
}
|
|
923
|
+
return {
|
|
924
|
+
properties,
|
|
925
|
+
hasUnknown
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
function textPropertiesToAttributes(properties) {
|
|
929
|
+
const attributes = [];
|
|
930
|
+
if (properties.bold !== void 0) attributes.push({
|
|
931
|
+
name: ATTR.fontWeight,
|
|
932
|
+
value: properties.bold ? "bold" : "normal"
|
|
933
|
+
});
|
|
934
|
+
if (properties.italic !== void 0) attributes.push({
|
|
935
|
+
name: ATTR.fontStyle,
|
|
936
|
+
value: properties.italic ? "italic" : "normal"
|
|
937
|
+
});
|
|
938
|
+
if (properties.underline !== void 0) if (properties.underline) {
|
|
939
|
+
attributes.push({
|
|
940
|
+
name: ATTR.underlineStyle,
|
|
941
|
+
value: "solid"
|
|
942
|
+
});
|
|
943
|
+
attributes.push({
|
|
944
|
+
name: ATTR.underlineWidth,
|
|
945
|
+
value: "auto"
|
|
946
|
+
});
|
|
947
|
+
attributes.push({
|
|
948
|
+
name: ATTR.underlineColor,
|
|
949
|
+
value: "font-color"
|
|
950
|
+
});
|
|
951
|
+
} else attributes.push({
|
|
952
|
+
name: ATTR.underlineStyle,
|
|
953
|
+
value: "none"
|
|
954
|
+
});
|
|
955
|
+
if (properties.strike !== void 0) if (properties.strike) {
|
|
956
|
+
attributes.push({
|
|
957
|
+
name: ATTR.lineThroughStyle,
|
|
958
|
+
value: "solid"
|
|
959
|
+
});
|
|
960
|
+
attributes.push({
|
|
961
|
+
name: ATTR.lineThroughType,
|
|
962
|
+
value: "single"
|
|
963
|
+
});
|
|
964
|
+
} else attributes.push({
|
|
965
|
+
name: ATTR.lineThroughStyle,
|
|
966
|
+
value: "none"
|
|
967
|
+
});
|
|
968
|
+
if (properties.fontFamily !== void 0) attributes.push({
|
|
969
|
+
name: ATTR.fontFamily,
|
|
970
|
+
value: encodeXmlText(properties.fontFamily)
|
|
971
|
+
});
|
|
972
|
+
if (properties.sizePt !== void 0) attributes.push({
|
|
973
|
+
name: ATTR.fontSize,
|
|
974
|
+
value: formatPt(properties.sizePt)
|
|
975
|
+
});
|
|
976
|
+
if (properties.color !== void 0) attributes.push({
|
|
977
|
+
name: ATTR.color,
|
|
978
|
+
value: formatColor(properties.color)
|
|
979
|
+
});
|
|
980
|
+
return attributes;
|
|
981
|
+
}
|
|
982
|
+
function paragraphPropertiesToAttributes(properties) {
|
|
983
|
+
const attributes = [];
|
|
984
|
+
if (properties.alignment !== void 0) attributes.push({
|
|
985
|
+
name: ATTR.textAlign,
|
|
986
|
+
value: properties.alignment
|
|
987
|
+
});
|
|
988
|
+
if (properties.spacingBeforePt !== void 0) attributes.push({
|
|
989
|
+
name: ATTR.marginTop,
|
|
990
|
+
value: formatPt(properties.spacingBeforePt)
|
|
991
|
+
});
|
|
992
|
+
if (properties.spacingAfterPt !== void 0) attributes.push({
|
|
993
|
+
name: ATTR.marginBottom,
|
|
994
|
+
value: formatPt(properties.spacingAfterPt)
|
|
995
|
+
});
|
|
996
|
+
if (properties.lineSpacing !== void 0) attributes.push({
|
|
997
|
+
name: ATTR.lineHeight,
|
|
998
|
+
value: formatPercentageMultiplier(properties.lineSpacing)
|
|
999
|
+
});
|
|
1000
|
+
if (properties.indentLeftPt !== void 0) attributes.push({
|
|
1001
|
+
name: ATTR.marginLeft,
|
|
1002
|
+
value: formatPt(properties.indentLeftPt)
|
|
1003
|
+
});
|
|
1004
|
+
if (properties.indentFirstLinePt !== void 0) attributes.push({
|
|
1005
|
+
name: ATTR.textIndent,
|
|
1006
|
+
value: formatPt(properties.indentFirstLinePt)
|
|
1007
|
+
});
|
|
1008
|
+
return attributes;
|
|
1009
|
+
}
|
|
1010
|
+
//#endregion
|
|
1011
|
+
//#region src/styles/serialize.ts
|
|
1012
|
+
function attributesToRecord(attributes) {
|
|
1013
|
+
const record = {};
|
|
1014
|
+
for (const attribute of attributes) record[attribute.name] = attribute.value;
|
|
1015
|
+
return record;
|
|
1016
|
+
}
|
|
1017
|
+
function buildStylePropertyElements(properties) {
|
|
1018
|
+
const elements = [];
|
|
1019
|
+
const paragraphAttributes = paragraphPropertiesToAttributes(properties);
|
|
1020
|
+
if (paragraphAttributes.length > 0) elements.push(el("style:paragraph-properties", attributesToRecord(paragraphAttributes)));
|
|
1021
|
+
const textAttributes = textPropertiesToAttributes(properties);
|
|
1022
|
+
if (textAttributes.length > 0) elements.push(el("style:text-properties", attributesToRecord(textAttributes)));
|
|
1023
|
+
return elements;
|
|
1024
|
+
}
|
|
1025
|
+
function canonicalPropertiesString(properties) {
|
|
1026
|
+
return [...paragraphPropertiesToAttributes(properties), ...textPropertiesToAttributes(properties)].map((attribute) => `${attribute.name}=${attribute.value}`).join("|");
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region src/styles/registry.ts
|
|
1030
|
+
const STYLE_FAMILIES = [
|
|
1031
|
+
"paragraph",
|
|
1032
|
+
"text",
|
|
1033
|
+
"table",
|
|
1034
|
+
"table-column",
|
|
1035
|
+
"table-row",
|
|
1036
|
+
"table-cell",
|
|
1037
|
+
"graphic"
|
|
1038
|
+
];
|
|
1039
|
+
function isStyleFamily(value) {
|
|
1040
|
+
return value === "paragraph" || value === "text" || value === "table" || value === "table-column" || value === "table-row" || value === "table-cell" || value === "graphic";
|
|
1041
|
+
}
|
|
1042
|
+
const CONTENT_PREFIXES = {
|
|
1043
|
+
paragraph: "P",
|
|
1044
|
+
text: "T",
|
|
1045
|
+
table: "ta",
|
|
1046
|
+
"table-column": "co",
|
|
1047
|
+
"table-row": "ro",
|
|
1048
|
+
"table-cell": "ce",
|
|
1049
|
+
graphic: "fr"
|
|
1050
|
+
};
|
|
1051
|
+
const STYLES_PREFIXES = {
|
|
1052
|
+
paragraph: "PS",
|
|
1053
|
+
text: "TS",
|
|
1054
|
+
table: "taS",
|
|
1055
|
+
"table-column": "coS",
|
|
1056
|
+
"table-row": "roS",
|
|
1057
|
+
"table-cell": "ceS",
|
|
1058
|
+
graphic: "frS"
|
|
1059
|
+
};
|
|
1060
|
+
function prefixesForPart(partPath) {
|
|
1061
|
+
const baseName = partPath.slice(partPath.lastIndexOf("/") + 1);
|
|
1062
|
+
if (baseName === "content.xml") return CONTENT_PREFIXES;
|
|
1063
|
+
if (baseName === "styles.xml") return STYLES_PREFIXES;
|
|
1064
|
+
throw new Error(`StyleRegistry.forPart: expected a part named "content.xml" or "styles.xml" (by base name), got "${partPath}"`);
|
|
1065
|
+
}
|
|
1066
|
+
function emptyFamilySets() {
|
|
1067
|
+
return {
|
|
1068
|
+
paragraph: /* @__PURE__ */ new Set(),
|
|
1069
|
+
text: /* @__PURE__ */ new Set(),
|
|
1070
|
+
table: /* @__PURE__ */ new Set(),
|
|
1071
|
+
"table-column": /* @__PURE__ */ new Set(),
|
|
1072
|
+
"table-row": /* @__PURE__ */ new Set(),
|
|
1073
|
+
"table-cell": /* @__PURE__ */ new Set(),
|
|
1074
|
+
graphic: /* @__PURE__ */ new Set()
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
function attrValue$1(element, name) {
|
|
1078
|
+
return element.attributes.find((attribute) => attribute.name === name)?.value;
|
|
1079
|
+
}
|
|
1080
|
+
function findDirectChild(nodes, tag) {
|
|
1081
|
+
for (const node of nodes) if (node.type === "element" && node.tag === tag) return node;
|
|
1082
|
+
}
|
|
1083
|
+
function findRootElement(nodes) {
|
|
1084
|
+
const root = nodes.find((node) => node.type === "element");
|
|
1085
|
+
if (root === void 0) throw new Error("StyleRegistry: part has no root XML element -- construct the part's minimal root (office:document-content/office:document-styles) before building a StyleRegistry for it");
|
|
1086
|
+
return root;
|
|
1087
|
+
}
|
|
1088
|
+
function ensureAutomaticStyles(root) {
|
|
1089
|
+
const existing = findDirectChild(root.children, "office:automatic-styles");
|
|
1090
|
+
if (existing !== void 0) return existing;
|
|
1091
|
+
const created = el("office:automatic-styles");
|
|
1092
|
+
const insertBeforeTags = /* @__PURE__ */ new Set([
|
|
1093
|
+
"office:body",
|
|
1094
|
+
"office:master-styles",
|
|
1095
|
+
"office:settings"
|
|
1096
|
+
]);
|
|
1097
|
+
const insertIndex = root.children.findIndex((node) => node.type === "element" && insertBeforeTags.has(node.tag));
|
|
1098
|
+
if (insertIndex === -1) root.children.push(created);
|
|
1099
|
+
else root.children.splice(insertIndex, 0, created);
|
|
1100
|
+
return created;
|
|
1101
|
+
}
|
|
1102
|
+
function reserveStyleNames(container, reserved) {
|
|
1103
|
+
for (const child of container.children) {
|
|
1104
|
+
if (child.type !== "element" || child.tag !== "style:style") continue;
|
|
1105
|
+
const name = attrValue$1(child, "style:name");
|
|
1106
|
+
const family = attrValue$1(child, "style:family");
|
|
1107
|
+
if (name === void 0 || family === void 0 || !isStyleFamily(family)) continue;
|
|
1108
|
+
reserved[family].add(name);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
const FINGERPRINT_SEPARATOR = "\0";
|
|
1112
|
+
const NO_PARENT_SENTINEL = "";
|
|
1113
|
+
function computeFingerprint(family, properties, parentStyleName) {
|
|
1114
|
+
const parentComponent = parentStyleName ?? NO_PARENT_SENTINEL;
|
|
1115
|
+
return [
|
|
1116
|
+
family,
|
|
1117
|
+
canonicalPropertiesString(properties),
|
|
1118
|
+
parentComponent
|
|
1119
|
+
].join(FINGERPRINT_SEPARATOR);
|
|
1120
|
+
}
|
|
1121
|
+
var StyleRegistry = class StyleRegistry {
|
|
1122
|
+
automaticStyles;
|
|
1123
|
+
prefixes;
|
|
1124
|
+
reservedByFamily;
|
|
1125
|
+
knownStyles = /* @__PURE__ */ new Map();
|
|
1126
|
+
fingerprintToName = /* @__PURE__ */ new Map();
|
|
1127
|
+
nameToFingerprint = /* @__PURE__ */ new Map();
|
|
1128
|
+
familyCounters = {
|
|
1129
|
+
paragraph: 1,
|
|
1130
|
+
text: 1,
|
|
1131
|
+
table: 1,
|
|
1132
|
+
"table-column": 1,
|
|
1133
|
+
"table-row": 1,
|
|
1134
|
+
"table-cell": 1,
|
|
1135
|
+
graphic: 1
|
|
1136
|
+
};
|
|
1137
|
+
constructor(automaticStyles, prefixes, reservedByFamily) {
|
|
1138
|
+
this.automaticStyles = automaticStyles;
|
|
1139
|
+
this.prefixes = prefixes;
|
|
1140
|
+
this.reservedByFamily = reservedByFamily;
|
|
1141
|
+
}
|
|
1142
|
+
static forPart(pkg, partPath, options = {}) {
|
|
1143
|
+
const part = pkg.parts[partPath];
|
|
1144
|
+
if (part?.kind !== "xml") throw new Error(`StyleRegistry.forPart: "${partPath}" is not an XML part of the given package`);
|
|
1145
|
+
const prefixes = prefixesForPart(partPath);
|
|
1146
|
+
const root = findRootElement(part.nodes);
|
|
1147
|
+
const automaticStyles = ensureAutomaticStyles(root);
|
|
1148
|
+
const reservedByFamily = emptyFamilySets();
|
|
1149
|
+
const registry = new StyleRegistry(automaticStyles, prefixes, reservedByFamily);
|
|
1150
|
+
for (const child of automaticStyles.children) {
|
|
1151
|
+
if (child.type !== "element" || child.tag !== "style:style") continue;
|
|
1152
|
+
const name = attrValue$1(child, "style:name");
|
|
1153
|
+
const family = attrValue$1(child, "style:family");
|
|
1154
|
+
if (name === void 0 || family === void 0 || !isStyleFamily(family)) continue;
|
|
1155
|
+
registry.knownStyles.set(name, child);
|
|
1156
|
+
reservedByFamily[family].add(name);
|
|
1157
|
+
const parsed = parseStyleElementProperties(child);
|
|
1158
|
+
if (!parsed.hasUnknown) {
|
|
1159
|
+
const parentStyleName = attrValue$1(child, "style:parent-style-name");
|
|
1160
|
+
const fingerprint = computeFingerprint(family, parsed.properties, parentStyleName);
|
|
1161
|
+
if (!registry.fingerprintToName.has(fingerprint)) {
|
|
1162
|
+
registry.fingerprintToName.set(fingerprint, name);
|
|
1163
|
+
registry.nameToFingerprint.set(name, fingerprint);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
const ownStyles = findDirectChild(root.children, "office:styles");
|
|
1168
|
+
if (ownStyles !== void 0) reserveStyleNames(ownStyles, reservedByFamily);
|
|
1169
|
+
if (options.otherPart !== void 0) {
|
|
1170
|
+
const otherPart = options.otherPart.pkg.parts[options.otherPart.partPath];
|
|
1171
|
+
if (otherPart?.kind === "xml") {
|
|
1172
|
+
const otherRoot = findRootElement(otherPart.nodes);
|
|
1173
|
+
const otherAutomatic = findDirectChild(otherRoot.children, "office:automatic-styles");
|
|
1174
|
+
if (otherAutomatic !== void 0) reserveStyleNames(otherAutomatic, reservedByFamily);
|
|
1175
|
+
const otherStyles = findDirectChild(otherRoot.children, "office:styles");
|
|
1176
|
+
if (otherStyles !== void 0) reserveStyleNames(otherStyles, reservedByFamily);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
if (options.additionalReservedNames !== void 0) for (const family of STYLE_FAMILIES) for (const name of options.additionalReservedNames) reservedByFamily[family].add(name);
|
|
1180
|
+
return registry;
|
|
1181
|
+
}
|
|
1182
|
+
fingerprint(request) {
|
|
1183
|
+
return computeFingerprint(request.family, request.properties, request.parentStyleName);
|
|
1184
|
+
}
|
|
1185
|
+
intern(request) {
|
|
1186
|
+
const fingerprint = this.fingerprint(request);
|
|
1187
|
+
const existingName = this.fingerprintToName.get(fingerprint);
|
|
1188
|
+
if (existingName !== void 0) return existingName;
|
|
1189
|
+
const name = this.mintName(request.family);
|
|
1190
|
+
const attributes = {
|
|
1191
|
+
"style:name": name,
|
|
1192
|
+
"style:family": request.family
|
|
1193
|
+
};
|
|
1194
|
+
if (request.parentStyleName !== void 0) attributes["style:parent-style-name"] = encodeXmlText(request.parentStyleName);
|
|
1195
|
+
const styleElement = el("style:style", attributes, buildStylePropertyElements(request.properties));
|
|
1196
|
+
this.automaticStyles.children.push(styleElement);
|
|
1197
|
+
this.knownStyles.set(name, styleElement);
|
|
1198
|
+
this.reservedByFamily[request.family].add(name);
|
|
1199
|
+
this.fingerprintToName.set(fingerprint, name);
|
|
1200
|
+
this.nameToFingerprint.set(name, fingerprint);
|
|
1201
|
+
return name;
|
|
1202
|
+
}
|
|
1203
|
+
mintName(family) {
|
|
1204
|
+
const prefix = this.prefixes[family];
|
|
1205
|
+
const reserved = this.reservedByFamily[family];
|
|
1206
|
+
let counter = this.familyCounters[family];
|
|
1207
|
+
while (reserved.has(`${prefix}${counter}`)) counter += 1;
|
|
1208
|
+
const name = `${prefix}${counter}`;
|
|
1209
|
+
this.familyCounters[family] = counter + 1;
|
|
1210
|
+
return name;
|
|
1211
|
+
}
|
|
1212
|
+
names() {
|
|
1213
|
+
return [...this.knownStyles.keys()];
|
|
1214
|
+
}
|
|
1215
|
+
gc(referenced) {
|
|
1216
|
+
let removed = 0;
|
|
1217
|
+
for (const [name, element] of [...this.knownStyles]) {
|
|
1218
|
+
if (referenced.has(name)) continue;
|
|
1219
|
+
const index = this.automaticStyles.children.indexOf(element);
|
|
1220
|
+
if (index !== -1) this.automaticStyles.children.splice(index, 1);
|
|
1221
|
+
this.knownStyles.delete(name);
|
|
1222
|
+
const fingerprint = this.nameToFingerprint.get(name);
|
|
1223
|
+
if (fingerprint !== void 0) {
|
|
1224
|
+
this.fingerprintToName.delete(fingerprint);
|
|
1225
|
+
this.nameToFingerprint.delete(name);
|
|
1226
|
+
}
|
|
1227
|
+
removed += 1;
|
|
1228
|
+
}
|
|
1229
|
+
return removed;
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
//#endregion
|
|
1233
|
+
//#region src/xml/query.ts
|
|
1234
|
+
function rootElement(nodes) {
|
|
1235
|
+
for (const node of nodes) if (node.type === "element") return node;
|
|
1236
|
+
}
|
|
1237
|
+
function findChildElement(nodes, tag) {
|
|
1238
|
+
for (const node of nodes) if (node.type === "element" && node.tag === tag) return node;
|
|
1239
|
+
}
|
|
1240
|
+
function childrenWithTag(element, tag) {
|
|
1241
|
+
const out = [];
|
|
1242
|
+
for (const child of element.children) if (child.type === "element" && child.tag === tag) out.push(child);
|
|
1243
|
+
return out;
|
|
1244
|
+
}
|
|
1245
|
+
function attrValue(element, name) {
|
|
1246
|
+
return element.attributes.find((attribute) => attribute.name === name)?.value;
|
|
1247
|
+
}
|
|
1248
|
+
//#endregion
|
|
1249
|
+
//#region src/typed/shared/text.ts
|
|
1250
|
+
function getOdfSpaceCount(element) {
|
|
1251
|
+
const raw = attrValue(element, "text:c");
|
|
1252
|
+
if (raw === void 0) return 1;
|
|
1253
|
+
const parsed = Number.parseInt(raw, 10);
|
|
1254
|
+
if (!Number.isInteger(parsed) || parsed < 0 || String(parsed) !== raw) throw new Error(`getOdfSpaceCount: text:s has a malformed text:c attribute: "${raw}"`);
|
|
1255
|
+
return parsed;
|
|
1256
|
+
}
|
|
1257
|
+
function measureOdfNodeLength(node) {
|
|
1258
|
+
if (node.type === "text") return node.value.length;
|
|
1259
|
+
if (node.type !== "element") return 0;
|
|
1260
|
+
if (node.tag === "text:s") return getOdfSpaceCount(node);
|
|
1261
|
+
if (node.tag === "text:tab" || node.tag === "text:line-break") return 1;
|
|
1262
|
+
if (node.tag === "text:span") return sumOdfNodeLength(node.children);
|
|
1263
|
+
return 0;
|
|
1264
|
+
}
|
|
1265
|
+
function sumOdfNodeLength(nodes) {
|
|
1266
|
+
let total = 0;
|
|
1267
|
+
for (const node of nodes) total += measureOdfNodeLength(node);
|
|
1268
|
+
return total;
|
|
1269
|
+
}
|
|
1270
|
+
function decodeOdfNode(node) {
|
|
1271
|
+
if (node.type === "text") return decodeXmlText(node.value);
|
|
1272
|
+
if (node.type !== "element") return "";
|
|
1273
|
+
if (node.tag === "text:s") return " ".repeat(getOdfSpaceCount(node));
|
|
1274
|
+
if (node.tag === "text:tab") return " ";
|
|
1275
|
+
if (node.tag === "text:line-break") return "\n";
|
|
1276
|
+
if (node.tag === "text:span") return decodeOdfText(node);
|
|
1277
|
+
return "";
|
|
1278
|
+
}
|
|
1279
|
+
function decodeOdfText(container) {
|
|
1280
|
+
let text = "";
|
|
1281
|
+
for (const child of container.children) text += decodeOdfNode(child);
|
|
1282
|
+
return text;
|
|
1283
|
+
}
|
|
1284
|
+
//#endregion
|
|
1285
|
+
//#region src/styles/span.ts
|
|
1286
|
+
function ensureSpan(paragraph, start, end, styleName) {
|
|
1287
|
+
if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end < start) throw new Error(`ensureSpan: invalid range [${start}, ${end})`);
|
|
1288
|
+
const total = sumOdfNodeLength(paragraph.children);
|
|
1289
|
+
if (end > total) throw new Error(`ensureSpan: range end ${end} exceeds the container's total character length ${total}`);
|
|
1290
|
+
const { before, after: rest } = splitChildrenAt(paragraph.children, start);
|
|
1291
|
+
const { before: middle, after } = splitChildrenAt(rest, end - start);
|
|
1292
|
+
let span;
|
|
1293
|
+
const soleChild = middle.length === 1 ? middle[0] : void 0;
|
|
1294
|
+
if (soleChild?.type === "element" && soleChild.tag === "text:span") {
|
|
1295
|
+
span = soleChild;
|
|
1296
|
+
setStyleName(span, styleName);
|
|
1297
|
+
} else span = el("text:span", { "text:style-name": encodeXmlText(styleName) }, middle);
|
|
1298
|
+
paragraph.children = [
|
|
1299
|
+
...before,
|
|
1300
|
+
span,
|
|
1301
|
+
...after
|
|
1302
|
+
];
|
|
1303
|
+
return span;
|
|
1304
|
+
}
|
|
1305
|
+
function cloneAttributes(attributes) {
|
|
1306
|
+
return attributes.map((attribute) => ({ ...attribute }));
|
|
1307
|
+
}
|
|
1308
|
+
function setStyleName(span, styleName) {
|
|
1309
|
+
const encoded = encodeXmlText(styleName);
|
|
1310
|
+
const existing = span.attributes.find((attribute) => attribute.name === "text:style-name");
|
|
1311
|
+
if (existing !== void 0) {
|
|
1312
|
+
existing.value = encoded;
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
span.attributes.push({
|
|
1316
|
+
name: "text:style-name",
|
|
1317
|
+
value: encoded
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
function buildSpaceRun(count) {
|
|
1321
|
+
return count === 1 ? el("text:s") : el("text:s", { "text:c": String(count) });
|
|
1322
|
+
}
|
|
1323
|
+
function splitNode(node, offset) {
|
|
1324
|
+
if (node.type === "text") return {
|
|
1325
|
+
left: {
|
|
1326
|
+
type: "text",
|
|
1327
|
+
value: node.value.slice(0, offset)
|
|
1328
|
+
},
|
|
1329
|
+
right: {
|
|
1330
|
+
type: "text",
|
|
1331
|
+
value: node.value.slice(offset)
|
|
1332
|
+
}
|
|
1333
|
+
};
|
|
1334
|
+
if (node.type === "element" && node.tag === "text:s") {
|
|
1335
|
+
const count = getOdfSpaceCount(node);
|
|
1336
|
+
return {
|
|
1337
|
+
left: buildSpaceRun(offset),
|
|
1338
|
+
right: buildSpaceRun(count - offset)
|
|
1339
|
+
};
|
|
1340
|
+
}
|
|
1341
|
+
if (node.type === "element" && node.tag === "text:span") {
|
|
1342
|
+
const inner = splitChildrenAt(node.children, offset);
|
|
1343
|
+
return {
|
|
1344
|
+
left: inner.before.length === 0 ? void 0 : {
|
|
1345
|
+
...node,
|
|
1346
|
+
attributes: cloneAttributes(node.attributes),
|
|
1347
|
+
children: inner.before
|
|
1348
|
+
},
|
|
1349
|
+
right: inner.after.length === 0 ? void 0 : {
|
|
1350
|
+
...node,
|
|
1351
|
+
attributes: cloneAttributes(node.attributes),
|
|
1352
|
+
children: inner.after
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
1356
|
+
const label = node.type === "element" ? node.tag : node.type;
|
|
1357
|
+
throw new Error(`ensureSpan: cannot split "${label}" at a fractional offset -- this indicates a character-length computation bug, since every node type with length 1 or 0 should never reach this branch`);
|
|
1358
|
+
}
|
|
1359
|
+
function splitChildrenAt(children, offset) {
|
|
1360
|
+
if (offset <= 0) return {
|
|
1361
|
+
before: [],
|
|
1362
|
+
after: [...children]
|
|
1363
|
+
};
|
|
1364
|
+
const before = [];
|
|
1365
|
+
let remaining = offset;
|
|
1366
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
1367
|
+
if (remaining === 0) return {
|
|
1368
|
+
before,
|
|
1369
|
+
after: children.slice(index)
|
|
1370
|
+
};
|
|
1371
|
+
const node = children[index];
|
|
1372
|
+
const length = measureOdfNodeLength(node);
|
|
1373
|
+
if (remaining >= length) {
|
|
1374
|
+
before.push(node);
|
|
1375
|
+
remaining -= length;
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
const { left, right } = splitNode(node, remaining);
|
|
1379
|
+
const after = [];
|
|
1380
|
+
if (left !== void 0) before.push(left);
|
|
1381
|
+
if (right !== void 0) after.push(right);
|
|
1382
|
+
after.push(...children.slice(index + 1));
|
|
1383
|
+
return {
|
|
1384
|
+
before,
|
|
1385
|
+
after
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
return {
|
|
1389
|
+
before,
|
|
1390
|
+
after: []
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
//#endregion
|
|
1394
|
+
//#region src/typed/shared/a1.ts
|
|
1395
|
+
const ALPHABET_SIZE = 26;
|
|
1396
|
+
const ALPHABET_START_CODE = "A".charCodeAt(0);
|
|
1397
|
+
function columnIndexToLetters(index) {
|
|
1398
|
+
if (!Number.isInteger(index) || index < 0) throw new Error(`columnIndexToLetters: index must be a non-negative integer, got ${index}`);
|
|
1399
|
+
let remaining = index + 1;
|
|
1400
|
+
let letters = "";
|
|
1401
|
+
while (remaining > 0) {
|
|
1402
|
+
const digit = (remaining - 1) % ALPHABET_SIZE;
|
|
1403
|
+
letters = String.fromCharCode(ALPHABET_START_CODE + digit) + letters;
|
|
1404
|
+
remaining = Math.floor((remaining - 1) / ALPHABET_SIZE);
|
|
1405
|
+
}
|
|
1406
|
+
return letters;
|
|
1407
|
+
}
|
|
1408
|
+
function cellReference(columnIndex, rowIndex) {
|
|
1409
|
+
if (!Number.isInteger(rowIndex) || rowIndex < 0) throw new Error(`cellReference: rowIndex must be a non-negative integer, got ${rowIndex}`);
|
|
1410
|
+
return `${columnIndexToLetters(columnIndex)}${rowIndex + 1}`;
|
|
1411
|
+
}
|
|
1412
|
+
function validateRepeatCount(repeatCount, caller) {
|
|
1413
|
+
if (!Number.isInteger(repeatCount) || repeatCount < 1) throw new Error(`${caller}: repeatCount must be a positive integer, got ${repeatCount}`);
|
|
1414
|
+
}
|
|
1415
|
+
var TableCursor = class {
|
|
1416
|
+
columnCursor = 0;
|
|
1417
|
+
rowCursor = 0;
|
|
1418
|
+
get columnIndex() {
|
|
1419
|
+
return this.columnCursor;
|
|
1420
|
+
}
|
|
1421
|
+
get rowIndex() {
|
|
1422
|
+
return this.rowCursor;
|
|
1423
|
+
}
|
|
1424
|
+
nextCell(repeatCount = 1) {
|
|
1425
|
+
validateRepeatCount(repeatCount, "TableCursor.nextCell");
|
|
1426
|
+
const reference = cellReference(this.columnCursor, this.rowCursor);
|
|
1427
|
+
this.columnCursor += repeatCount;
|
|
1428
|
+
return reference;
|
|
1429
|
+
}
|
|
1430
|
+
nextRow(repeatCount = 1) {
|
|
1431
|
+
validateRepeatCount(repeatCount, "TableCursor.nextRow");
|
|
1432
|
+
this.rowCursor += repeatCount;
|
|
1433
|
+
this.columnCursor = 0;
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
//#endregion
|
|
1437
|
+
//#region src/typed/shared/geometry.ts
|
|
1438
|
+
function parsePageSize(pageLayoutProperties) {
|
|
1439
|
+
const widthValue = attrValue(pageLayoutProperties, "fo:page-width");
|
|
1440
|
+
const heightValue = attrValue(pageLayoutProperties, "fo:page-height");
|
|
1441
|
+
if (widthValue === void 0 || heightValue === void 0) return;
|
|
1442
|
+
const widthPt = parseOdfLength(widthValue);
|
|
1443
|
+
const heightPt = parseOdfLength(heightValue);
|
|
1444
|
+
if (widthPt === void 0 || heightPt === void 0) return;
|
|
1445
|
+
return {
|
|
1446
|
+
widthPt,
|
|
1447
|
+
heightPt
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
function parseMargins(pageLayoutProperties) {
|
|
1451
|
+
const topValue = attrValue(pageLayoutProperties, "fo:margin-top");
|
|
1452
|
+
const rightValue = attrValue(pageLayoutProperties, "fo:margin-right");
|
|
1453
|
+
const bottomValue = attrValue(pageLayoutProperties, "fo:margin-bottom");
|
|
1454
|
+
const leftValue = attrValue(pageLayoutProperties, "fo:margin-left");
|
|
1455
|
+
if (topValue === void 0 || rightValue === void 0 || bottomValue === void 0 || leftValue === void 0) return;
|
|
1456
|
+
const topPt = parseOdfLength(topValue);
|
|
1457
|
+
const rightPt = parseOdfLength(rightValue);
|
|
1458
|
+
const bottomPt = parseOdfLength(bottomValue);
|
|
1459
|
+
const leftPt = parseOdfLength(leftValue);
|
|
1460
|
+
if (topPt === void 0 || rightPt === void 0 || bottomPt === void 0 || leftPt === void 0) return;
|
|
1461
|
+
return {
|
|
1462
|
+
topPt,
|
|
1463
|
+
rightPt,
|
|
1464
|
+
bottomPt,
|
|
1465
|
+
leftPt
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
function parseBox(element) {
|
|
1469
|
+
const xValue = attrValue(element, "svg:x");
|
|
1470
|
+
const yValue = attrValue(element, "svg:y");
|
|
1471
|
+
const widthValue = attrValue(element, "svg:width");
|
|
1472
|
+
const heightValue = attrValue(element, "svg:height");
|
|
1473
|
+
if (xValue === void 0 || yValue === void 0 || widthValue === void 0 || heightValue === void 0) return;
|
|
1474
|
+
const xPt = parseOdfLength(xValue);
|
|
1475
|
+
const yPt = parseOdfLength(yValue);
|
|
1476
|
+
const widthPt = parseOdfLength(widthValue);
|
|
1477
|
+
const heightPt = parseOdfLength(heightValue);
|
|
1478
|
+
if (xPt === void 0 || yPt === void 0 || widthPt === void 0 || heightPt === void 0) return;
|
|
1479
|
+
return {
|
|
1480
|
+
xPt,
|
|
1481
|
+
yPt,
|
|
1482
|
+
widthPt,
|
|
1483
|
+
heightPt
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
//#endregion
|
|
1487
|
+
//#region src/typed/shared/cascade.ts
|
|
1488
|
+
const STYLE_PARTS = ["content.xml", "styles.xml"];
|
|
1489
|
+
const NAME_KEY_SEPARATOR = "\0";
|
|
1490
|
+
function nameKey(family, name) {
|
|
1491
|
+
return `${family}${NAME_KEY_SEPARATOR}${name}`;
|
|
1492
|
+
}
|
|
1493
|
+
function collectStyles(pkg) {
|
|
1494
|
+
const byName = /* @__PURE__ */ new Map();
|
|
1495
|
+
const defaultByFamily = /* @__PURE__ */ new Map();
|
|
1496
|
+
for (const partPath of STYLE_PARTS) {
|
|
1497
|
+
const part = pkg.parts[partPath];
|
|
1498
|
+
if (part?.kind !== "xml") continue;
|
|
1499
|
+
const root = rootElement(part.nodes);
|
|
1500
|
+
if (root === void 0) continue;
|
|
1501
|
+
for (const containerTag of ["office:automatic-styles", "office:styles"]) {
|
|
1502
|
+
const container = findChildElement(root.children, containerTag);
|
|
1503
|
+
if (container === void 0) continue;
|
|
1504
|
+
for (const child of container.children) {
|
|
1505
|
+
if (child.type !== "element") continue;
|
|
1506
|
+
if (child.tag === "style:style") {
|
|
1507
|
+
const name = attrValue(child, "style:name");
|
|
1508
|
+
const family = attrValue(child, "style:family");
|
|
1509
|
+
if (name === void 0 || family === void 0 || !isStyleFamily(family)) continue;
|
|
1510
|
+
byName.set(nameKey(family, name), child);
|
|
1511
|
+
} else if (child.tag === "style:default-style") {
|
|
1512
|
+
const family = attrValue(child, "style:family");
|
|
1513
|
+
if (family === void 0 || !isStyleFamily(family)) continue;
|
|
1514
|
+
if (!defaultByFamily.has(family)) defaultByFamily.set(family, child);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
return {
|
|
1520
|
+
byName,
|
|
1521
|
+
defaultByFamily
|
|
1522
|
+
};
|
|
1523
|
+
}
|
|
1524
|
+
function resolveStyle(styleName, family, pkg) {
|
|
1525
|
+
const { byName, defaultByFamily } = collectStyles(pkg);
|
|
1526
|
+
const diagnostics = [];
|
|
1527
|
+
const defaultElement = defaultByFamily.get(family);
|
|
1528
|
+
let properties = defaultElement === void 0 ? {} : parseStyleElementProperties(defaultElement).properties;
|
|
1529
|
+
if (styleName === void 0) return {
|
|
1530
|
+
properties,
|
|
1531
|
+
diagnostics
|
|
1532
|
+
};
|
|
1533
|
+
const chain = [];
|
|
1534
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1535
|
+
let currentName = styleName;
|
|
1536
|
+
while (currentName !== void 0) {
|
|
1537
|
+
const key = nameKey(family, currentName);
|
|
1538
|
+
if (visited.has(key)) {
|
|
1539
|
+
diagnostics.push({
|
|
1540
|
+
severity: "warning",
|
|
1541
|
+
message: `cyclic style:parent-style-name chain detected at style "${currentName}" (family "${family}") -- breaking the cycle and stopping cascade resolution at this point`
|
|
1542
|
+
});
|
|
1543
|
+
break;
|
|
1544
|
+
}
|
|
1545
|
+
visited.add(key);
|
|
1546
|
+
const element = byName.get(key);
|
|
1547
|
+
if (element === void 0) {
|
|
1548
|
+
diagnostics.push({
|
|
1549
|
+
severity: "warning",
|
|
1550
|
+
message: `style "${currentName}" (family "${family}") was not found in content.xml or styles.xml -- stopping cascade resolution at this point`
|
|
1551
|
+
});
|
|
1552
|
+
break;
|
|
1553
|
+
}
|
|
1554
|
+
chain.push(element);
|
|
1555
|
+
currentName = attrValue(element, "style:parent-style-name");
|
|
1556
|
+
}
|
|
1557
|
+
chain.reverse();
|
|
1558
|
+
for (const element of chain) properties = {
|
|
1559
|
+
...properties,
|
|
1560
|
+
...parseStyleElementProperties(element).properties
|
|
1561
|
+
};
|
|
1562
|
+
return {
|
|
1563
|
+
properties,
|
|
1564
|
+
diagnostics
|
|
1565
|
+
};
|
|
1566
|
+
}
|
|
1567
|
+
//#endregion
|
|
1568
|
+
//#region src/typed/shared/metadata.ts
|
|
1569
|
+
const META_PART = "meta.xml";
|
|
1570
|
+
function elementText(element) {
|
|
1571
|
+
let text = "";
|
|
1572
|
+
for (const child of element.children) if (child.type === "text") text += decodeXmlText(child.value);
|
|
1573
|
+
return text;
|
|
1574
|
+
}
|
|
1575
|
+
function firstElementText(container, tag) {
|
|
1576
|
+
const element = childrenWithTag(container, tag)[0];
|
|
1577
|
+
if (element === void 0) return;
|
|
1578
|
+
const text = elementText(element);
|
|
1579
|
+
return text.length > 0 ? text : void 0;
|
|
1580
|
+
}
|
|
1581
|
+
function readOdfMetadata(pkg) {
|
|
1582
|
+
const part = pkg.parts[META_PART];
|
|
1583
|
+
if (part?.kind !== "xml") return {};
|
|
1584
|
+
const root = rootElement(part.nodes);
|
|
1585
|
+
const meta = root === void 0 ? void 0 : findChildElement(root.children, "office:meta");
|
|
1586
|
+
if (meta === void 0) return {};
|
|
1587
|
+
const metadata = {};
|
|
1588
|
+
const title = firstElementText(meta, "dc:title");
|
|
1589
|
+
if (title !== void 0) metadata.title = title;
|
|
1590
|
+
const author = firstElementText(meta, "meta:initial-creator");
|
|
1591
|
+
if (author !== void 0) metadata.author = author;
|
|
1592
|
+
const subject = firstElementText(meta, "dc:subject");
|
|
1593
|
+
if (subject !== void 0) metadata.subject = subject;
|
|
1594
|
+
const keywords = childrenWithTag(meta, "meta:keyword").map(elementText).filter((keyword) => keyword.length > 0);
|
|
1595
|
+
if (keywords.length > 0) metadata.keywords = keywords;
|
|
1596
|
+
const creator = firstElementText(meta, "meta:generator");
|
|
1597
|
+
if (creator !== void 0) metadata.creator = creator;
|
|
1598
|
+
const createdIso = firstElementText(meta, "meta:creation-date");
|
|
1599
|
+
if (createdIso !== void 0) metadata.createdIso = createdIso;
|
|
1600
|
+
const modifiedIso = firstElementText(meta, "dc:date");
|
|
1601
|
+
if (modifiedIso !== void 0) metadata.modifiedIso = modifiedIso;
|
|
1602
|
+
return metadata;
|
|
1603
|
+
}
|
|
1604
|
+
//#endregion
|
|
1605
|
+
export { AlignmentSchema, AttributeSchema, BinaryPartSchema, MANIFEST_PART, META_PART, MIMETYPE_PART, ManifestEntrySchema, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, PackageSchema, PartSchema, STYLE_FAMILIES, StylePropertiesSchema, StyleRegistry, TableCursor, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, decodeOdfText, decodePackage, decodeXmlText, el, encodePackage, encodeXmlText, ensureSpan, findChildElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readManifest, readMimetype, readOdfMetadata, resolveStyle, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
|