odf.js 6.1.2 → 6.2.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.
@@ -0,0 +1,402 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_util_base64 = require("../util/base64.cjs");
3
+ const require_ns = require("../ns.cjs");
4
+ const require_xml_query = require("../xml/query.cjs");
5
+ const require_ooo1_ns = require("./ns.cjs");
6
+ const require_ooo1_properties = require("./properties.cjs");
7
+ //#region src/ooo1/transform.ts
8
+ const CANONICAL_PREFIX_BY_URI = new Map([...Object.entries(require_ooo1_ns.OOO1_NAMESPACES).map(([prefix, uri]) => [uri, prefix]), ...Object.entries(require_ns.ODF_NAMESPACES).map(([prefix, uri]) => [uri, prefix])]);
9
+ function isOdfNamespacePrefix(prefix) {
10
+ return Object.hasOwn(require_ns.ODF_NAMESPACES, prefix);
11
+ }
12
+ const GENRE_ELEMENT_BY_CLASS = /* @__PURE__ */ new Map([
13
+ ["text", "office:text"],
14
+ ["text-global", "office:text"],
15
+ ["spreadsheet", "office:spreadsheet"],
16
+ ["presentation", "office:presentation"],
17
+ ["drawing", "office:drawing"],
18
+ ["chart", "office:chart"]
19
+ ]);
20
+ const RENAMED_ELEMENTS = /* @__PURE__ */ new Map([
21
+ ["office:font-decls", "office:font-face-decls"],
22
+ ["office:script", "office:scripts"],
23
+ ["office:script-data", "office:script"],
24
+ ["office:events", "office:event-listeners"],
25
+ ["style:page-master", "style:page-layout"],
26
+ ["text:ordered-list", "text:list"],
27
+ ["text:unordered-list", "text:list"],
28
+ ["text:tab-stop", "text:tab"],
29
+ ["text:footnote-body", "text:note-body"],
30
+ ["text:endnote-body", "text:note-body"],
31
+ ["text:footnote-citation", "text:note-citation"],
32
+ ["text:endnote-citation", "text:note-citation"],
33
+ ["text:database-select", "text:database-row-select"],
34
+ ["text:index-entry-chapter-number", "text:index-entry-chapter"],
35
+ ["table:dependences", "table:dependencies"],
36
+ ["table:dependence", "table:dependency"]
37
+ ]);
38
+ const NOTE_ELEMENTS = /* @__PURE__ */ new Map([
39
+ ["text:footnote", {
40
+ tag: "text:note",
41
+ noteClass: "footnote"
42
+ }],
43
+ ["text:endnote", {
44
+ tag: "text:note",
45
+ noteClass: "endnote"
46
+ }],
47
+ ["text:footnote-ref", {
48
+ tag: "text:note-ref",
49
+ noteClass: "footnote"
50
+ }],
51
+ ["text:endnote-ref", {
52
+ tag: "text:note-ref",
53
+ noteClass: "endnote"
54
+ }],
55
+ ["text:footnotes-configuration", {
56
+ tag: "text:notes-configuration",
57
+ noteClass: "footnote"
58
+ }],
59
+ ["text:endnotes-configuration", {
60
+ tag: "text:notes-configuration",
61
+ noteClass: "endnote"
62
+ }]
63
+ ]);
64
+ const RENAMED_ATTRIBUTES = /* @__PURE__ */ new Map([
65
+ ["style:page-master-name", "style:page-layout-name"],
66
+ ["style:leader-char", "style:leader-text"],
67
+ ["text:count-in-floating-frames", "text:count-in-text-boxes"],
68
+ ["form:service-name", "form:control-implementation"],
69
+ ["form:column-style-name", "form:text-style-name"],
70
+ ["form:property-type", "office:value-type"]
71
+ ]);
72
+ const VALUE_ATTRIBUTE_LOCAL_NAMES = [
73
+ "value-type",
74
+ "value",
75
+ "currency",
76
+ "date-value",
77
+ "time-value",
78
+ "boolean-value",
79
+ "string-value"
80
+ ];
81
+ const CELL_ELEMENTS = /* @__PURE__ */ new Set([
82
+ "table:table-cell",
83
+ "table:covered-table-cell",
84
+ "table:change-track-table-cell"
85
+ ]);
86
+ const TEXT_VALUE_ELEMENTS = /* @__PURE__ */ new Set([
87
+ "text:variable-decl",
88
+ "text:variable-set",
89
+ "text:variable-input",
90
+ "text:variable-get",
91
+ "text:user-field-decl",
92
+ "text:user-field-get",
93
+ "text:user-field-input",
94
+ "text:expression"
95
+ ]);
96
+ const FRAME_SHAPES = /* @__PURE__ */ new Set([
97
+ "draw:text-box",
98
+ "draw:image",
99
+ "draw:object",
100
+ "draw:object-ole",
101
+ "draw:applet",
102
+ "draw:plugin",
103
+ "draw:floating-frame"
104
+ ]);
105
+ const FRAME_ATTRIBUTES = /* @__PURE__ */ new Set([
106
+ "draw:z-index",
107
+ "draw:id",
108
+ "draw:layer",
109
+ "draw:style-name",
110
+ "presentation:style-name",
111
+ "draw:transform",
112
+ "draw:name",
113
+ "table:end-cell-address",
114
+ "table:end-x",
115
+ "table:end-y",
116
+ "table:table-background",
117
+ "text:anchor-type",
118
+ "text:anchor-page-number",
119
+ "draw:text-style-name",
120
+ "svg:x",
121
+ "svg:y",
122
+ "svg:width",
123
+ "svg:height",
124
+ "style:rel-width",
125
+ "style:rel-height",
126
+ "presentation:class",
127
+ "presentation:placeholder",
128
+ "presentation:user-transformed"
129
+ ]);
130
+ const FRAME_CHILD_ELEMENTS = /* @__PURE__ */ new Set([
131
+ "office:event-listeners",
132
+ "draw:glue-point",
133
+ "draw:image-map",
134
+ "svg:desc",
135
+ "svg:title",
136
+ "draw:contour-polygon",
137
+ "draw:contour-path"
138
+ ]);
139
+ const PACKAGE_HREF_ELEMENTS = /* @__PURE__ */ new Set([
140
+ "draw:image",
141
+ "draw:object",
142
+ "draw:object-ole",
143
+ "draw:applet",
144
+ "draw:plugin",
145
+ "draw:floating-frame",
146
+ "draw:fill-image",
147
+ "style:background-image",
148
+ "text:list-level-style-image"
149
+ ]);
150
+ const INCH_TOKEN = /(^|\s)(-?(?:\d+(?:\.\d+)?|\.\d+))inch(?=\s|$)/g;
151
+ function carriesNoLength(attributeName) {
152
+ return attributeName.endsWith("name") || attributeName === "xlink:href";
153
+ }
154
+ function normaliseAttributeValue(name, value) {
155
+ if (carriesNoLength(name)) return value;
156
+ return value.replace(INCH_TOKEN, "$1$2in");
157
+ }
158
+ function renameQName(qname, prefixes) {
159
+ const colon = qname.indexOf(":");
160
+ if (colon < 0) return qname;
161
+ const canonical = prefixes.get(qname.slice(0, colon));
162
+ return canonical === void 0 ? qname : `${canonical}:${qname.slice(colon + 1)}`;
163
+ }
164
+ function transformNamespaceDeclaration(attribute) {
165
+ const canonical = CANONICAL_PREFIX_BY_URI.get(attribute.value);
166
+ if (canonical === void 0 || !isOdfNamespacePrefix(canonical)) return attribute;
167
+ return {
168
+ name: attribute.name === "xmlns" ? "xmlns" : `xmlns:${canonical}`,
169
+ value: require_ns.ODF_NAMESPACES[canonical]
170
+ };
171
+ }
172
+ const DOCUMENT_ROOT_ELEMENTS = /* @__PURE__ */ new Set([
173
+ "office:document",
174
+ "office:document-content",
175
+ "office:document-styles",
176
+ "office:document-meta",
177
+ "office:document-settings"
178
+ ]);
179
+ function transformAttributes(element, tag, prefixes) {
180
+ const out = [];
181
+ for (const attribute of element.attributes) {
182
+ if (attribute.name === "xmlns" || attribute.name.startsWith("xmlns:")) {
183
+ out.push(transformNamespaceDeclaration(attribute));
184
+ continue;
185
+ }
186
+ const name = renameQName(attribute.name, prefixes);
187
+ if (name === "office:class" && DOCUMENT_ROOT_ELEMENTS.has(tag)) continue;
188
+ const value = normaliseAttributeValue(name, attribute.value);
189
+ out.push({
190
+ name: renameAttributeFor(tag, name),
191
+ value: rewriteHref(tag, name, value)
192
+ });
193
+ }
194
+ return out;
195
+ }
196
+ function renameAttributeFor(tag, name) {
197
+ const renamed = RENAMED_ATTRIBUTES.get(name);
198
+ if (renamed !== void 0) return renamed;
199
+ if (name === "text:level" && (tag === "text:h" || tag === "text:p")) return "text:outline-level";
200
+ if (CELL_ELEMENTS.has(tag)) {
201
+ if (name === "table:validation-name") return "table:content-validation-name";
202
+ for (const local of VALUE_ATTRIBUTE_LOCAL_NAMES) if (name === `table:${local}`) return `office:${local}`;
203
+ }
204
+ if (TEXT_VALUE_ELEMENTS.has(tag)) {
205
+ for (const local of VALUE_ATTRIBUTE_LOCAL_NAMES) if (name === `text:${local}`) return `office:${local}`;
206
+ }
207
+ if (tag === "style:column") {
208
+ if (name === "fo:margin-left") return "fo:start-indent";
209
+ if (name === "fo:margin-right") return "fo:end-indent";
210
+ }
211
+ return name;
212
+ }
213
+ function rewriteHref(tag, name, value) {
214
+ if (name === "xlink:href" && value.startsWith("#") && PACKAGE_HREF_ELEMENTS.has(tag)) return value.slice(1);
215
+ return value;
216
+ }
217
+ function withoutAttributes(attributes, drop) {
218
+ return attributes.filter((attribute) => !drop.has(attribute.name));
219
+ }
220
+ function element(tag, attributes, children) {
221
+ return {
222
+ type: "element",
223
+ tag,
224
+ attributes,
225
+ children
226
+ };
227
+ }
228
+ function textElement(tag, value) {
229
+ return element(tag, [], [{
230
+ type: "text",
231
+ value
232
+ }]);
233
+ }
234
+ const MOVED_TO_CHILD_ELEMENT = /* @__PURE__ */ new Map([["office:annotation", /* @__PURE__ */ new Map([
235
+ ["office:author", "dc:creator"],
236
+ ["office:create-date", "dc:date"],
237
+ ["office:create-date-string", "meta:date-string"]
238
+ ])], ["office:change-info", /* @__PURE__ */ new Map([["office:chg-author", "dc:creator"], ["office:chg-date-time", "dc:date"]])]]);
239
+ function transformNodes(nodes, context) {
240
+ const out = [];
241
+ for (const node of nodes) {
242
+ if (node.type !== "element") {
243
+ out.push(node);
244
+ continue;
245
+ }
246
+ out.push(...transformElement(node, context));
247
+ }
248
+ return out;
249
+ }
250
+ function transformElement(source, context) {
251
+ const renamedTag = renameQName(source.tag, context.prefixes);
252
+ if (renamedTag === "style:properties") {
253
+ const staged = element(renamedTag, transformAttributes(source, renamedTag, context.prefixes), transformNodes(source.children, {
254
+ ...context,
255
+ propertyTypes: void 0
256
+ }));
257
+ if (context.propertyTypes === void 0) return [staged];
258
+ return require_ooo1_properties.splitStyleProperties(staged, context.propertyTypes);
259
+ }
260
+ if (renamedTag === "meta:keywords") return transformNodes(source.children, {
261
+ ...context,
262
+ propertyTypes: void 0
263
+ });
264
+ const attributes = transformAttributes(source, renamedTag, context.prefixes);
265
+ const childContext = {
266
+ ...context,
267
+ propertyTypes: require_ooo1_properties.propertyTypesForContainer({
268
+ ...source,
269
+ tag: renamedTag,
270
+ attributes
271
+ })
272
+ };
273
+ const children = transformNodes(source.children, childContext);
274
+ if (renamedTag === "office:body") return [buildBody(attributes, children, context.documentClass)];
275
+ const note = NOTE_ELEMENTS.get(renamedTag);
276
+ if (note !== void 0) return [element(note.tag, [...attributes, {
277
+ name: "text:note-class",
278
+ value: note.noteClass
279
+ }], children)];
280
+ if (renamedTag === "table:sub-table") return [element("table:table", [...attributes, {
281
+ name: "table:is-sub-table",
282
+ value: "true"
283
+ }], children)];
284
+ if (renamedTag === "style:font-decl") return [element("style:font-face", attributes.map((attribute) => {
285
+ if (attribute.name === "fo:font-family") return {
286
+ name: "svg:font-family",
287
+ value: attribute.value
288
+ };
289
+ if (attribute.name === "style:font-style-name") return {
290
+ name: "style:font-adornments",
291
+ value: attribute.value
292
+ };
293
+ return attribute;
294
+ }), children)];
295
+ const moved = MOVED_TO_CHILD_ELEMENT.get(renamedTag);
296
+ if (moved !== void 0) {
297
+ const promoted = [];
298
+ for (const [from, to] of moved) {
299
+ const value = attributes.find((attribute) => attribute.name === from)?.value;
300
+ if (value !== void 0) promoted.push(textElement(to, value));
301
+ }
302
+ return [element(renamedTag, withoutAttributes(attributes, new Set(moved.keys())), [...promoted, ...children])];
303
+ }
304
+ if (FRAME_SHAPES.has(renamedTag)) return [buildFrame(renamedTag, attributes, children)];
305
+ if (renamedTag === "form:property") return [element(renamedTag, withoutAttributes(attributes, /* @__PURE__ */ new Set(["form:property-is-list"])), children)];
306
+ const renamed = RENAMED_ELEMENTS.get(renamedTag);
307
+ if (renamed !== void 0) return [element(renamed, attributes, children)];
308
+ return [element(renamedTag, attributes, children)];
309
+ }
310
+ function buildBody(attributes, children, documentClass) {
311
+ const genre = documentClass === void 0 ? void 0 : GENRE_ELEMENT_BY_CLASS.get(documentClass);
312
+ if (genre === void 0) return element("office:body", attributes, children);
313
+ return element("office:body", attributes, [element(genre, [], children)]);
314
+ }
315
+ function buildFrame(tag, attributes, children) {
316
+ const frameAttributes = attributes.filter((attribute) => FRAME_ATTRIBUTES.has(attribute.name));
317
+ const shapeAttributes = attributes.filter((attribute) => !FRAME_ATTRIBUTES.has(attribute.name));
318
+ const frameChildren = [];
319
+ const shapeChildren = [];
320
+ for (const child of children) if (child.type === "element" && FRAME_CHILD_ELEMENTS.has(child.tag)) frameChildren.push(child);
321
+ else shapeChildren.push(child);
322
+ return element("draw:frame", frameAttributes, [element(tag, shapeAttributes, shapeChildren), ...frameChildren]);
323
+ }
324
+ function prefixRenames(root) {
325
+ const renames = /* @__PURE__ */ new Map();
326
+ for (const attribute of root.attributes) {
327
+ if (!attribute.name.startsWith("xmlns:")) continue;
328
+ const declared = attribute.name.slice(6);
329
+ const canonical = CANONICAL_PREFIX_BY_URI.get(attribute.value);
330
+ if (canonical !== void 0 && canonical !== declared) renames.set(declared, canonical);
331
+ }
332
+ return renames;
333
+ }
334
+ function transformXmlPart(nodes) {
335
+ const root = require_xml_query.rootElement(nodes);
336
+ if (root === void 0) return [...nodes];
337
+ const prefixes = prefixRenames(root);
338
+ const classAttributeName = [...prefixes.entries()].find(([, canonical]) => canonical === "office")?.[0];
339
+ return transformNodes(nodes, {
340
+ prefixes,
341
+ documentClass: root.attributes.find((attribute) => attribute.name === "office:class" || classAttributeName !== void 0 && attribute.name === `${classAttributeName}:class`)?.value
342
+ });
343
+ }
344
+ const MANIFEST_PATH = "META-INF/manifest.xml";
345
+ const MIMETYPE_PATH = "mimetype";
346
+ function odfMediaTypeOf(pkg) {
347
+ const manifest = pkg.parts[MANIFEST_PATH];
348
+ if (manifest?.kind !== "xml") return;
349
+ const root = require_xml_query.rootElement(manifest.nodes);
350
+ if (root === void 0) return;
351
+ for (const entry of root.children) {
352
+ if (entry.type !== "element" || !entry.tag.endsWith(":file-entry")) continue;
353
+ if (entry.attributes.find((attribute) => attribute.name.endsWith(":full-path"))?.value !== "/") continue;
354
+ const mediaType = entry.attributes.find((attribute) => attribute.name.endsWith(":media-type"))?.value;
355
+ return mediaType === void 0 ? void 0 : require_ooo1_ns.odfMediaTypeForOoo1MediaType(mediaType);
356
+ }
357
+ }
358
+ function rewriteManifestMediaType(nodes, odfMediaType) {
359
+ return nodes.map((node) => {
360
+ if (node.type !== "element") return node;
361
+ return {
362
+ ...node,
363
+ children: node.children.map((entry) => {
364
+ if (entry.type !== "element" || entry.tag !== "manifest:file-entry") return entry;
365
+ if (entry.attributes.find((attribute) => attribute.name === "manifest:full-path")?.value !== "/") return entry;
366
+ return {
367
+ ...entry,
368
+ attributes: entry.attributes.map((attribute) => attribute.name === "manifest:media-type" ? {
369
+ name: attribute.name,
370
+ value: odfMediaType
371
+ } : attribute)
372
+ };
373
+ })
374
+ };
375
+ });
376
+ }
377
+ function transformOoo1Package(pkg) {
378
+ if (!require_ooo1_ns.isOoo1Package(pkg)) return pkg;
379
+ const odfMediaType = odfMediaTypeOf(pkg);
380
+ const parts = {};
381
+ for (const [path, part] of Object.entries(pkg.parts)) {
382
+ if (part.kind !== "xml") {
383
+ parts[path] = part;
384
+ continue;
385
+ }
386
+ const nodes = transformXmlPart(part.nodes);
387
+ parts[path] = path === MANIFEST_PATH && odfMediaType !== void 0 ? {
388
+ kind: "xml",
389
+ nodes: rewriteManifestMediaType(nodes, odfMediaType)
390
+ } : {
391
+ kind: "xml",
392
+ nodes
393
+ };
394
+ }
395
+ if (odfMediaType !== void 0 && parts[MIMETYPE_PATH] === void 0) parts[MIMETYPE_PATH] = {
396
+ kind: "binary",
397
+ base64: require_util_base64.bytesToBase64(new TextEncoder().encode(odfMediaType))
398
+ };
399
+ return { parts };
400
+ }
401
+ //#endregion
402
+ exports.transformOoo1Package = transformOoo1Package;
@@ -0,0 +1,2 @@
1
+ import { t as transformOoo1Package } from "../transform-SDysw9wx.cjs";
2
+ export { transformOoo1Package };
@@ -0,0 +1,2 @@
1
+ import { t as transformOoo1Package } from "../transform-DRr76JEu.js";
2
+ export { transformOoo1Package };