@docx4j/core-ts 0.1.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/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +126 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +10 -0
- package/dist/model/content/Body.d.mts +107 -0
- package/dist/model/content/Body.mjs +363 -0
- package/dist/model/content/Font.d.mts +46 -0
- package/dist/model/content/Font.mjs +50 -0
- package/dist/model/content/Paragraph.d.mts +99 -0
- package/dist/model/content/Paragraph.mjs +364 -0
- package/dist/model/content/Range.d.mts +34 -0
- package/dist/model/content/Range.mjs +94 -0
- package/dist/model/content/index.d.mts +7 -0
- package/dist/model/content/index.mjs +9 -0
- package/dist/model/content/search.d.mts +12 -0
- package/dist/model/content/search.mjs +68 -0
- package/dist/model/content/tree.d.mts +40 -0
- package/dist/model/content/tree.mjs +120 -0
- package/dist/model/index.d.mts +1 -0
- package/dist/model/index.mjs +2 -0
- package/dist/opc/ContentTypeManager.d.mts +37 -0
- package/dist/opc/ContentTypeManager.mjs +126 -0
- package/dist/opc/ContentTypes.d.mts +118 -0
- package/dist/opc/ContentTypes.mjs +139 -0
- package/dist/opc/FlatOpcPartStore.d.mts +42 -0
- package/dist/opc/FlatOpcPartStore.mjs +151 -0
- package/dist/opc/Load.d.mts +18 -0
- package/dist/opc/Load.mjs +123 -0
- package/dist/opc/PartName.d.mts +46 -0
- package/dist/opc/PartName.mjs +132 -0
- package/dist/opc/PartStore.d.mts +45 -0
- package/dist/opc/PartStore.mjs +54 -0
- package/dist/opc/Save.d.mts +9 -0
- package/dist/opc/Save.mjs +52 -0
- package/dist/opc/ZipPartStore.d.mts +32 -0
- package/dist/opc/ZipPartStore.mjs +117 -0
- package/dist/opc/exceptions.d.mts +17 -0
- package/dist/opc/exceptions.mjs +22 -0
- package/dist/opc/index.d.mts +12 -0
- package/dist/opc/index.mjs +13 -0
- package/dist/opc/mce/preprocessor.d.mts +15 -0
- package/dist/opc/mce/preprocessor.mjs +74 -0
- package/dist/opc/mce/understood.d.mts +1 -0
- package/dist/opc/mce/understood.mjs +98 -0
- package/dist/packages/OpcPackage.d.mts +62 -0
- package/dist/packages/OpcPackage.mjs +133 -0
- package/dist/packages/PresentationMLPackage.d.mts +12 -0
- package/dist/packages/PresentationMLPackage.mjs +39 -0
- package/dist/packages/SpreadsheetMLPackage.d.mts +12 -0
- package/dist/packages/SpreadsheetMLPackage.mjs +38 -0
- package/dist/packages/WordprocessingMLPackage.d.mts +49 -0
- package/dist/packages/WordprocessingMLPackage.mjs +130 -0
- package/dist/packages/index.d.mts +5 -0
- package/dist/packages/index.mjs +6 -0
- package/dist/packages/registry.d.mts +7 -0
- package/dist/packages/registry.mjs +18 -0
- package/dist/parts/BinaryPart.d.mts +38 -0
- package/dist/parts/BinaryPart.mjs +72 -0
- package/dist/parts/DefaultXmlPart.d.mts +29 -0
- package/dist/parts/DefaultXmlPart.mjs +63 -0
- package/dist/parts/Namespaces.d.mts +99 -0
- package/dist/parts/Namespaces.mjs +99 -0
- package/dist/parts/Part.d.mts +50 -0
- package/dist/parts/Part.mjs +68 -0
- package/dist/parts/PartRegistry.d.mts +20 -0
- package/dist/parts/PartRegistry.mjs +142 -0
- package/dist/parts/Parts.d.mts +13 -0
- package/dist/parts/Parts.mjs +28 -0
- package/dist/parts/RelationshipsPart.d.mts +66 -0
- package/dist/parts/RelationshipsPart.mjs +224 -0
- package/dist/parts/XmlPart.d.mts +46 -0
- package/dist/parts/XmlPart.mjs +87 -0
- package/dist/parts/customXml/index.d.mts +9 -0
- package/dist/parts/customXml/index.mjs +11 -0
- package/dist/parts/dml/index.d.mts +54 -0
- package/dist/parts/dml/index.mjs +83 -0
- package/dist/parts/docProps/index.d.mts +14 -0
- package/dist/parts/docProps/index.mjs +18 -0
- package/dist/parts/index.d.mts +14 -0
- package/dist/parts/index.mjs +15 -0
- package/dist/parts/pml/index.d.mts +53 -0
- package/dist/parts/pml/index.mjs +90 -0
- package/dist/parts/sml/index.d.mts +61 -0
- package/dist/parts/sml/index.mjs +107 -0
- package/dist/parts/wml/defaultStyles.d.mts +1 -0
- package/dist/parts/wml/defaultStyles.mjs +500 -0
- package/dist/parts/wml/index.d.mts +102 -0
- package/dist/parts/wml/index.mjs +213 -0
- package/dist/xml/dom.d.mts +22 -0
- package/dist/xml/dom.mjs +92 -0
- package/package.json +75 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PartStore, PartSink, PutOptions } from './PartStore.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* A zip container read from bytes (docx4j io3.stores.ZipPartStore). The central directory is
|
|
4
|
+
* read once; an entry is inflated on first `load` and then cached. ZIP64 archives are rejected.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ZipPartStore implements PartStore {
|
|
7
|
+
private readonly data;
|
|
8
|
+
private readonly entries;
|
|
9
|
+
private readonly order;
|
|
10
|
+
private readonly cache;
|
|
11
|
+
constructor(source: Uint8Array | ArrayBuffer);
|
|
12
|
+
partNames(): Iterable<string>;
|
|
13
|
+
has(partName: string): boolean;
|
|
14
|
+
size(partName: string): number | undefined;
|
|
15
|
+
load(partName: string): Promise<Uint8Array>;
|
|
16
|
+
loadSync(partName: string): Uint8Array;
|
|
17
|
+
private readCentralDirectory;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Writes a zip (docx4j ZipPartStore, the save half). Entries are written in `put` order, so the
|
|
21
|
+
* saver puts `[Content_Types].xml` first, as Word does; entries are deflated unless `compress`
|
|
22
|
+
* is false (already-compressed media).
|
|
23
|
+
*/
|
|
24
|
+
export declare class ZipPartSink implements PartSink<Uint8Array> {
|
|
25
|
+
private readonly files;
|
|
26
|
+
private readonly level;
|
|
27
|
+
constructor(options?: {
|
|
28
|
+
level?: number;
|
|
29
|
+
});
|
|
30
|
+
put(partName: string, bytes: Uint8Array, options?: PutOptions): void;
|
|
31
|
+
finish(): Promise<Uint8Array>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { inflateSync, zipSync } from 'fflate';
|
|
2
|
+
import { InvalidFormatException } from './exceptions.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* A zip container read from bytes (docx4j io3.stores.ZipPartStore). The central directory is
|
|
5
|
+
* read once; an entry is inflated on first `load` and then cached. ZIP64 archives are rejected.
|
|
6
|
+
*/
|
|
7
|
+
export class ZipPartStore {
|
|
8
|
+
constructor(source) {
|
|
9
|
+
this.entries = new Map();
|
|
10
|
+
this.order = [];
|
|
11
|
+
this.cache = new Map();
|
|
12
|
+
this.data = source instanceof Uint8Array ? source : new Uint8Array(source);
|
|
13
|
+
this.readCentralDirectory();
|
|
14
|
+
}
|
|
15
|
+
partNames() {
|
|
16
|
+
return this.order;
|
|
17
|
+
}
|
|
18
|
+
has(partName) {
|
|
19
|
+
return this.entries.has(partName);
|
|
20
|
+
}
|
|
21
|
+
size(partName) {
|
|
22
|
+
var _a;
|
|
23
|
+
return (_a = this.entries.get(partName)) === null || _a === void 0 ? void 0 : _a.size;
|
|
24
|
+
}
|
|
25
|
+
async load(partName) {
|
|
26
|
+
return this.loadSync(partName);
|
|
27
|
+
}
|
|
28
|
+
loadSync(partName) {
|
|
29
|
+
const cached = this.cache.get(partName);
|
|
30
|
+
if (cached)
|
|
31
|
+
return cached;
|
|
32
|
+
const e = this.entries.get(partName);
|
|
33
|
+
if (!e)
|
|
34
|
+
throw new Error(`No entry ${partName} in zip`);
|
|
35
|
+
const d = this.data;
|
|
36
|
+
const off = e.localHeaderOffset;
|
|
37
|
+
if (readU32(d, off) !== 0x04034b50)
|
|
38
|
+
throw new InvalidFormatException(`Bad local header for ${partName}`);
|
|
39
|
+
const nameLen = readU16(d, off + 26);
|
|
40
|
+
const extraLen = readU16(d, off + 28);
|
|
41
|
+
const start = off + 30 + nameLen + extraLen;
|
|
42
|
+
const raw = d.subarray(start, start + e.compressedSize);
|
|
43
|
+
let bytes;
|
|
44
|
+
if (e.method === 0)
|
|
45
|
+
bytes = raw.slice();
|
|
46
|
+
else if (e.method === 8)
|
|
47
|
+
bytes = inflateSync(raw);
|
|
48
|
+
else
|
|
49
|
+
throw new InvalidFormatException(`Unsupported compression method ${e.method} for ${partName}`);
|
|
50
|
+
this.cache.set(partName, bytes);
|
|
51
|
+
return bytes;
|
|
52
|
+
}
|
|
53
|
+
readCentralDirectory() {
|
|
54
|
+
const d = this.data;
|
|
55
|
+
// End of central directory record: signature 0x06054b50, at most 64 KB of comment after it.
|
|
56
|
+
let eocd = -1;
|
|
57
|
+
for (let i = d.length - 22; i >= 0 && i >= d.length - 22 - 0xffff; i--) {
|
|
58
|
+
if (readU32(d, i) === 0x06054b50) {
|
|
59
|
+
eocd = i;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (eocd < 0)
|
|
64
|
+
throw new InvalidFormatException('Not a zip file (no end of central directory record)');
|
|
65
|
+
const count = readU16(d, eocd + 10);
|
|
66
|
+
const cdOffset = readU32(d, eocd + 16);
|
|
67
|
+
if (count === 0xffff || cdOffset === 0xffffffff)
|
|
68
|
+
throw new InvalidFormatException('ZIP64 archives are not supported');
|
|
69
|
+
let p = cdOffset;
|
|
70
|
+
const decoder = new TextDecoder('utf-8');
|
|
71
|
+
for (let i = 0; i < count; i++) {
|
|
72
|
+
if (readU32(d, p) !== 0x02014b50)
|
|
73
|
+
throw new InvalidFormatException('Bad central directory entry');
|
|
74
|
+
const method = readU16(d, p + 10);
|
|
75
|
+
const compressedSize = readU32(d, p + 20);
|
|
76
|
+
const size = readU32(d, p + 24);
|
|
77
|
+
const nameLen = readU16(d, p + 28);
|
|
78
|
+
const extraLen = readU16(d, p + 30);
|
|
79
|
+
const commentLen = readU16(d, p + 32);
|
|
80
|
+
const localHeaderOffset = readU32(d, p + 42);
|
|
81
|
+
if (compressedSize === 0xffffffff || size === 0xffffffff || localHeaderOffset === 0xffffffff) {
|
|
82
|
+
throw new InvalidFormatException('ZIP64 archives are not supported');
|
|
83
|
+
}
|
|
84
|
+
const name = decoder.decode(d.subarray(p + 46, p + 46 + nameLen));
|
|
85
|
+
p += 46 + nameLen + extraLen + commentLen;
|
|
86
|
+
if (name.endsWith('/'))
|
|
87
|
+
continue; // directory entry
|
|
88
|
+
this.entries.set(name, { name, method, compressedSize, size, localHeaderOffset });
|
|
89
|
+
this.order.push(name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Writes a zip (docx4j ZipPartStore, the save half). Entries are written in `put` order, so the
|
|
95
|
+
* saver puts `[Content_Types].xml` first, as Word does; entries are deflated unless `compress`
|
|
96
|
+
* is false (already-compressed media).
|
|
97
|
+
*/
|
|
98
|
+
export class ZipPartSink {
|
|
99
|
+
constructor(options) {
|
|
100
|
+
var _a;
|
|
101
|
+
this.files = {};
|
|
102
|
+
this.level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 6;
|
|
103
|
+
}
|
|
104
|
+
put(partName, bytes, options) {
|
|
105
|
+
const name = partName.startsWith('/') ? partName.substring(1) : partName;
|
|
106
|
+
this.files[name] = [bytes, { level: (options === null || options === void 0 ? void 0 : options.compress) === false ? 0 : this.level }];
|
|
107
|
+
}
|
|
108
|
+
async finish() {
|
|
109
|
+
return zipSync(this.files);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function readU16(d, p) {
|
|
113
|
+
return d[p] | (d[p + 1] << 8);
|
|
114
|
+
}
|
|
115
|
+
function readU32(d, p) {
|
|
116
|
+
return (d[p] | (d[p + 1] << 8) | (d[p + 2] << 16) | (d[p + 3] << 24)) >>> 0;
|
|
117
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** docx4j Docx4JException: anything the packaging layer cannot recover from. */
|
|
2
|
+
export declare class Docx4JException extends Error {
|
|
3
|
+
readonly cause: unknown;
|
|
4
|
+
constructor(message: string, options?: {
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
/** docx4j InvalidFormatException: a package, part name or relationship violates Open Packaging. */
|
|
9
|
+
export declare class InvalidFormatException extends Docx4JException {
|
|
10
|
+
constructor(message: string, options?: {
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/** docx4j PartUnrecognisedException: no part class for a content type (the registry falls back before this is thrown). */
|
|
15
|
+
export declare class PartUnrecognisedException extends Docx4JException {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** docx4j Docx4JException: anything the packaging layer cannot recover from. */
|
|
2
|
+
export class Docx4JException extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'Docx4JException';
|
|
6
|
+
this.cause = options === null || options === void 0 ? void 0 : options.cause;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** docx4j InvalidFormatException: a package, part name or relationship violates Open Packaging. */
|
|
10
|
+
export class InvalidFormatException extends Docx4JException {
|
|
11
|
+
constructor(message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = 'InvalidFormatException';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** docx4j PartUnrecognisedException: no part class for a content type (the registry falls back before this is thrown). */
|
|
17
|
+
export class PartUnrecognisedException extends Docx4JException {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'PartUnrecognisedException';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { PartName, CONTENT_TYPES_NAME } from './PartName.mjs';
|
|
2
|
+
export { ContentTypes, IMAGE_CONTENT_TYPES_BY_EXTENSION, isXmlContentType, isStoredUncompressed } from './ContentTypes.mjs';
|
|
3
|
+
export { ContentTypeManager, CONTENT_TYPES_NS } from './ContentTypeManager.mjs';
|
|
4
|
+
export { type PartStore, type PartSink, type PutOptions, MemoryPartStore, MemoryPartSink } from './PartStore.mjs';
|
|
5
|
+
export { ZipPartStore, ZipPartSink } from './ZipPartStore.mjs';
|
|
6
|
+
export { FlatOpcPartStore, FlatOpcPartSink, type FlatOpcSinkOptions, PKG_NS } from './FlatOpcPartStore.mjs';
|
|
7
|
+
export { loadPackage, type LoadOptions } from './Load.mjs';
|
|
8
|
+
export { savePackage } from './Save.mjs';
|
|
9
|
+
export { mcePreprocess, createMcePreprocessor, resolveAlternateContent, MCE_NS, type McePreprocessOptions } from './mce/preprocessor.mjs';
|
|
10
|
+
export { UNDERSTOOD_NAMESPACES } from './mce/understood.mjs';
|
|
11
|
+
export { Docx4JException, InvalidFormatException, PartUnrecognisedException } from './exceptions.mjs';
|
|
12
|
+
export { parseXml, serializeXml, serializeXmlPart, createDocument, encodeText, decodeXmlText, stripXmlDeclaration, base64Encode, base64Decode, childElements, XML_DECLARATION } from '../xml/dom.mjs';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The Open Packaging layer: names, content types, containers, load and save.
|
|
2
|
+
export { PartName, CONTENT_TYPES_NAME } from './PartName.mjs';
|
|
3
|
+
export { ContentTypes, IMAGE_CONTENT_TYPES_BY_EXTENSION, isXmlContentType, isStoredUncompressed } from './ContentTypes.mjs';
|
|
4
|
+
export { ContentTypeManager, CONTENT_TYPES_NS } from './ContentTypeManager.mjs';
|
|
5
|
+
export { MemoryPartStore, MemoryPartSink } from './PartStore.mjs';
|
|
6
|
+
export { ZipPartStore, ZipPartSink } from './ZipPartStore.mjs';
|
|
7
|
+
export { FlatOpcPartStore, FlatOpcPartSink, PKG_NS } from './FlatOpcPartStore.mjs';
|
|
8
|
+
export { loadPackage } from './Load.mjs';
|
|
9
|
+
export { savePackage } from './Save.mjs';
|
|
10
|
+
export { mcePreprocess, createMcePreprocessor, resolveAlternateContent, MCE_NS } from './mce/preprocessor.mjs';
|
|
11
|
+
export { UNDERSTOOD_NAMESPACES } from './mce/understood.mjs';
|
|
12
|
+
export { Docx4JException, InvalidFormatException, PartUnrecognisedException } from './exceptions.mjs';
|
|
13
|
+
export { parseXml, serializeXml, serializeXmlPart, createDocument, encodeText, decodeXmlText, stripXmlDeclaration, base64Encode, base64Decode, childElements, XML_DECLARATION } from '../xml/dom.mjs';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { XmlPart } from '../../parts/XmlPart.mjs';
|
|
2
|
+
export declare const MCE_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006";
|
|
3
|
+
export interface McePreprocessOptions {
|
|
4
|
+
/** Namespaces counted as understood; defaults to the object model's modules. */
|
|
5
|
+
understood?: ReadonlySet<string>;
|
|
6
|
+
}
|
|
7
|
+
/** The default preprocessor, over the object model's namespaces. */
|
|
8
|
+
export declare function mcePreprocess(doc: Document, _part?: XmlPart<unknown>): void;
|
|
9
|
+
/** A preprocessor over a namespace set of your own. */
|
|
10
|
+
export declare function createMcePreprocessor(options: McePreprocessOptions): (doc: Document, part?: XmlPart<unknown>) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Resolves every mc:AlternateContent under a node, innermost first (a chosen branch may contain
|
|
13
|
+
* another). Returns the number resolved.
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveAlternateContent(root: Node, understood?: ReadonlySet<string>): number;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { UNDERSTOOD_NAMESPACES } from './understood.mjs';
|
|
2
|
+
export const MCE_NS = 'http://schemas.openxmlformats.org/markup-compatibility/2006';
|
|
3
|
+
/** The default preprocessor, over the object model's namespaces. */
|
|
4
|
+
export function mcePreprocess(doc, _part) {
|
|
5
|
+
resolveAlternateContent(doc, UNDERSTOOD_NAMESPACES);
|
|
6
|
+
}
|
|
7
|
+
/** A preprocessor over a namespace set of your own. */
|
|
8
|
+
export function createMcePreprocessor(options) {
|
|
9
|
+
var _a;
|
|
10
|
+
const understood = (_a = options.understood) !== null && _a !== void 0 ? _a : UNDERSTOOD_NAMESPACES;
|
|
11
|
+
return (doc) => resolveAlternateContent(doc, understood);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Resolves every mc:AlternateContent under a node, innermost first (a chosen branch may contain
|
|
15
|
+
* another). Returns the number resolved.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveAlternateContent(root, understood = UNDERSTOOD_NAMESPACES) {
|
|
18
|
+
let count = 0;
|
|
19
|
+
const visit = (node) => {
|
|
20
|
+
// Snapshot: replacing a child mutates the list.
|
|
21
|
+
const children = [];
|
|
22
|
+
for (let c = node.firstChild; c; c = c.nextSibling)
|
|
23
|
+
children.push(c);
|
|
24
|
+
for (const child of children) {
|
|
25
|
+
if (child.nodeType !== 1)
|
|
26
|
+
continue;
|
|
27
|
+
visit(child);
|
|
28
|
+
const el = child;
|
|
29
|
+
if (el.namespaceURI === MCE_NS && el.localName === 'AlternateContent') {
|
|
30
|
+
replaceAlternateContent(el, understood);
|
|
31
|
+
count++;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
visit(root);
|
|
36
|
+
return count;
|
|
37
|
+
}
|
|
38
|
+
function replaceAlternateContent(ac, understood) {
|
|
39
|
+
let chosen;
|
|
40
|
+
let fallback;
|
|
41
|
+
for (let c = ac.firstChild; c; c = c.nextSibling) {
|
|
42
|
+
if (c.nodeType !== 1)
|
|
43
|
+
continue;
|
|
44
|
+
const el = c;
|
|
45
|
+
if (el.namespaceURI !== MCE_NS)
|
|
46
|
+
continue;
|
|
47
|
+
if (el.localName === 'Choice' && !chosen && choiceUnderstood(el, understood))
|
|
48
|
+
chosen = el;
|
|
49
|
+
else if (el.localName === 'Fallback' && !fallback)
|
|
50
|
+
fallback = el;
|
|
51
|
+
}
|
|
52
|
+
const branch = chosen !== null && chosen !== void 0 ? chosen : fallback;
|
|
53
|
+
const parent = ac.parentNode;
|
|
54
|
+
if (branch) {
|
|
55
|
+
const moved = [];
|
|
56
|
+
for (let c = branch.firstChild; c; c = c.nextSibling)
|
|
57
|
+
moved.push(c);
|
|
58
|
+
for (const n of moved)
|
|
59
|
+
parent.insertBefore(n, ac);
|
|
60
|
+
}
|
|
61
|
+
parent.removeChild(ac);
|
|
62
|
+
}
|
|
63
|
+
function choiceUnderstood(choice, understood) {
|
|
64
|
+
var _a;
|
|
65
|
+
const requires = ((_a = choice.getAttribute('Requires')) !== null && _a !== void 0 ? _a : '').trim();
|
|
66
|
+
if (requires === '')
|
|
67
|
+
return false;
|
|
68
|
+
for (const prefix of requires.split(/\s+/)) {
|
|
69
|
+
const ns = choice.lookupNamespaceURI(prefix);
|
|
70
|
+
if (!ns || !understood.has(ns))
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const UNDERSTOOD_NAMESPACES: ReadonlySet<string>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The namespaces the object model has modules for: what "understanding" an mc:Choice means here.
|
|
2
|
+
// Mirrors the defaultElementNamespaceURI of every generated module of @docx4j/generated-objects-ts
|
|
3
|
+
// (0.1.0); regenerate when modules are added. Word takes the first mc:Choice whose Requires
|
|
4
|
+
// prefixes it all understands (ECMA-376 Part 3, 10.2.1), and so does this preprocessor.
|
|
5
|
+
export const UNDERSTOOD_NAMESPACES = new Set([
|
|
6
|
+
'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
|
|
7
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
|
|
8
|
+
'http://schemas.openxmlformats.org/package/2006/relationships',
|
|
9
|
+
'http://schemas.openxmlformats.org/markup-compatibility/2006',
|
|
10
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/math',
|
|
11
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/bibliography',
|
|
12
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/customXml',
|
|
13
|
+
'http://schemas.openxmlformats.org/schemaLibrary/2006/main',
|
|
14
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties',
|
|
15
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties',
|
|
16
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes',
|
|
17
|
+
'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
|
|
18
|
+
'http://purl.org/dc/elements/1.1/',
|
|
19
|
+
'http://purl.org/dc/terms/',
|
|
20
|
+
'http://schemas.microsoft.com/office/2006/coverPageProps',
|
|
21
|
+
'http://schemas.microsoft.com/office/2006/xmlPackage',
|
|
22
|
+
'http://schemas.openxmlformats.org/drawingml/2006/main',
|
|
23
|
+
'http://schemas.openxmlformats.org/drawingml/2006/picture',
|
|
24
|
+
'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing',
|
|
25
|
+
'http://schemas.openxmlformats.org/drawingml/2006/chart',
|
|
26
|
+
'http://schemas.openxmlformats.org/drawingml/2006/chartDrawing',
|
|
27
|
+
'http://schemas.openxmlformats.org/drawingml/2006/diagram',
|
|
28
|
+
'http://schemas.openxmlformats.org/drawingml/2006/compatibility',
|
|
29
|
+
'http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas',
|
|
30
|
+
'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
|
|
31
|
+
'http://schemas.microsoft.com/office/drawing/2007/8/2/chart',
|
|
32
|
+
'http://schemas.microsoft.com/office/drawing/2008/diagram',
|
|
33
|
+
'http://schemas.microsoft.com/office/drawing/2010/main',
|
|
34
|
+
'http://schemas.microsoft.com/office/drawing/2010/picture',
|
|
35
|
+
'http://schemas.microsoft.com/office/drawing/2010/chartDrawing',
|
|
36
|
+
'http://schemas.microsoft.com/office/drawing/2010/diagram',
|
|
37
|
+
'http://schemas.microsoft.com/office/drawing/2012/main',
|
|
38
|
+
'http://schemas.microsoft.com/office/drawing/2012/chart',
|
|
39
|
+
'http://schemas.microsoft.com/office/drawing/2012/chartStyle',
|
|
40
|
+
'http://schemas.microsoft.com/office/drawing/2013/main/command',
|
|
41
|
+
'http://schemas.microsoft.com/office/drawing/2014/main',
|
|
42
|
+
'http://schemas.microsoft.com/office/drawing/2014/chart',
|
|
43
|
+
'http://schemas.microsoft.com/office/drawing/2014/chart/ac',
|
|
44
|
+
'http://schemas.microsoft.com/office/drawing/2014/chartex',
|
|
45
|
+
'http://schemas.microsoft.com/office/drawing/2016/11/main',
|
|
46
|
+
'http://schemas.microsoft.com/office/drawing/2016/12/diagram',
|
|
47
|
+
'http://schemas.microsoft.com/office/drawing/2016/ink',
|
|
48
|
+
'http://schemas.microsoft.com/office/drawing/2016/SVG/main',
|
|
49
|
+
'http://schemas.microsoft.com/office/drawing/2017/03/chart',
|
|
50
|
+
'http://schemas.microsoft.com/office/drawing/2017/decorative',
|
|
51
|
+
'http://schemas.microsoft.com/office/drawing/2017/model3d',
|
|
52
|
+
'http://schemas.microsoft.com/office/drawing/2018/animation',
|
|
53
|
+
'http://schemas.microsoft.com/office/drawing/2018/animation/model3d',
|
|
54
|
+
'http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor',
|
|
55
|
+
'http://schemas.microsoft.com/office/thememl/2012/main',
|
|
56
|
+
'urn:schemas-microsoft-com:vml',
|
|
57
|
+
'urn:schemas-microsoft-com:office:office',
|
|
58
|
+
'urn:schemas-microsoft-com:office:word',
|
|
59
|
+
'urn:schemas-microsoft-com:office:excel',
|
|
60
|
+
'urn:schemas-microsoft-com:office:powerpoint',
|
|
61
|
+
'http://schemas.microsoft.com/office/word/2006/wordml',
|
|
62
|
+
'http://schemas.microsoft.com/office/word/2010/wordml',
|
|
63
|
+
'http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas',
|
|
64
|
+
'http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing',
|
|
65
|
+
'http://schemas.microsoft.com/office/word/2010/wordprocessingGroup',
|
|
66
|
+
'http://schemas.microsoft.com/office/word/2010/wordprocessingShape',
|
|
67
|
+
'http://schemas.microsoft.com/office/word/2012/wordml',
|
|
68
|
+
'http://schemas.microsoft.com/office/word/2012/wordprocessingDrawing',
|
|
69
|
+
'http://schemas.microsoft.com/office/word/2015/wordml/symex',
|
|
70
|
+
'http://schemas.microsoft.com/office/word/2016/wordml/cid',
|
|
71
|
+
'http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11',
|
|
72
|
+
'http://schemas.microsoft.com/office/webextensions/webextension/2010/11',
|
|
73
|
+
'http://schemas.microsoft.com/office/powerpoint/2010/main',
|
|
74
|
+
'http://schemas.microsoft.com/office/powerpoint/2012/main',
|
|
75
|
+
'http://schemas.microsoft.com/office/powerpoint/2013/main/command',
|
|
76
|
+
'http://schemas.microsoft.com/office/powerpoint/2014/inkAction',
|
|
77
|
+
'http://schemas.microsoft.com/office/powerpoint/2015/main',
|
|
78
|
+
'http://schemas.microsoft.com/office/powerpoint/2015/09/main',
|
|
79
|
+
'http://schemas.microsoft.com/office/powerpoint/2015/10/main',
|
|
80
|
+
'http://schemas.microsoft.com/office/powerpoint/2016/6/main',
|
|
81
|
+
'http://schemas.microsoft.com/office/powerpoint/2016/sectionzoom',
|
|
82
|
+
'http://schemas.microsoft.com/office/powerpoint/2016/slidezoom',
|
|
83
|
+
'http://schemas.microsoft.com/office/powerpoint/2016/summaryzoom',
|
|
84
|
+
'http://schemas.microsoft.com/office/powerpoint/2017/3/main',
|
|
85
|
+
'http://schemas.microsoft.com/office/powerpoint/2017/10/main',
|
|
86
|
+
'http://schemas.microsoft.com/office/powerpoint/2018/4/main',
|
|
87
|
+
'http://schemas.openxmlformats.org/presentationml/2006/main',
|
|
88
|
+
'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
|
|
89
|
+
'http://schemas.microsoft.com/office/excel/2006/main',
|
|
90
|
+
'http://schemas.microsoft.com/office/excel/2008/2/main',
|
|
91
|
+
'http://schemas.microsoft.com/office/excel/2010/spreadsheetDrawing',
|
|
92
|
+
'http://schemas.microsoft.com/ink/2010/main',
|
|
93
|
+
'http://www.w3.org/1998/Math/MathML',
|
|
94
|
+
'http://www.w3.org/2003/InkML',
|
|
95
|
+
'http://schemas.microsoft.com/office/2006/encryption',
|
|
96
|
+
'http://schemas.microsoft.com/office/2006/keyEncryptor/certificate',
|
|
97
|
+
'http://schemas.microsoft.com/office/2006/keyEncryptor/password',
|
|
98
|
+
]);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Relationship } from '@docx4j/generated-objects-ts/modules/org_docx4j_relationships';
|
|
2
|
+
import { PartName } from '../opc/PartName.mjs';
|
|
3
|
+
import { ContentTypeManager } from '../opc/ContentTypeManager.mjs';
|
|
4
|
+
import type { PartStore, PartSink } from '../opc/PartStore.mjs';
|
|
5
|
+
import { type FlatOpcSinkOptions } from '../opc/FlatOpcPartStore.mjs';
|
|
6
|
+
import { type LoadOptions } from '../opc/Load.mjs';
|
|
7
|
+
import { Part, type RelationshipSource } from '../parts/Part.mjs';
|
|
8
|
+
import { Parts } from '../parts/Parts.mjs';
|
|
9
|
+
import { RelationshipsPart, type AddPartBehaviour } from '../parts/RelationshipsPart.mjs';
|
|
10
|
+
import type { CustomXmlDataStoragePart } from '../parts/DefaultXmlPart.mjs';
|
|
11
|
+
import { DocPropsCorePart, DocPropsExtendedPart, DocPropsCustomPart } from '../parts/docProps/index.mjs';
|
|
12
|
+
/** Anything `OpcPackage.load` accepts: zip bytes, a flat OPC string, or a container. */
|
|
13
|
+
export type PackageSource = Uint8Array | ArrayBuffer | string | PartStore;
|
|
14
|
+
/**
|
|
15
|
+
* An Open Packaging Conventions package (docx4j org.docx4j.openpackaging.packages.OpcPackage):
|
|
16
|
+
* the content types, the package relationships and the parts reachable from them.
|
|
17
|
+
*/
|
|
18
|
+
export declare class OpcPackage implements RelationshipSource {
|
|
19
|
+
/** The pseudo part name of the package itself: the source of the package relationships. */
|
|
20
|
+
readonly partName: PartName;
|
|
21
|
+
contentTypeManager: ContentTypeManager;
|
|
22
|
+
relationshipsPart: RelationshipsPart;
|
|
23
|
+
readonly parts: Parts;
|
|
24
|
+
/** Where untouched parts are copied from on save. Undefined for a new package. */
|
|
25
|
+
sourcePartStore: PartStore | undefined;
|
|
26
|
+
/** Custom XML parts by the (lower-cased) itemID of their properties part. */
|
|
27
|
+
readonly customXmlDataStorageParts: Map<string, CustomXmlDataStoragePart>;
|
|
28
|
+
/** The options this package was loaded with. */
|
|
29
|
+
loadOptions: LoadOptions;
|
|
30
|
+
docPropsCorePart: DocPropsCorePart | undefined;
|
|
31
|
+
docPropsExtendedPart: DocPropsExtendedPart | undefined;
|
|
32
|
+
docPropsCustomPart: DocPropsCustomPart | undefined;
|
|
33
|
+
constructor();
|
|
34
|
+
get package(): OpcPackage;
|
|
35
|
+
/**
|
|
36
|
+
* Loads a package, picking the class (WordprocessingMLPackage, ...) from the main part's
|
|
37
|
+
* content type. A string is a flat OPC package; bytes are a zip; a PartStore is used as is.
|
|
38
|
+
*/
|
|
39
|
+
static load(source: PackageSource, options?: LoadOptions): Promise<OpcPackage>;
|
|
40
|
+
getPart(partName: PartName | string): Part | undefined;
|
|
41
|
+
getRelationshipsPart(): RelationshipsPart;
|
|
42
|
+
/** Adds a part as a target of the package (docProps, the main part). */
|
|
43
|
+
addTargetPart(target: Part, mode?: AddPartBehaviour, proposedRelId?: string): Relationship;
|
|
44
|
+
setPartShortcut(part: Part, relationshipType: string): boolean;
|
|
45
|
+
/** The main part: the target of the officeDocument relationship. */
|
|
46
|
+
getMainPart(): Part | undefined;
|
|
47
|
+
/** Unmarshals every XML part, so that `contents` is available synchronously everywhere. */
|
|
48
|
+
unmarshalAll(): Promise<void>;
|
|
49
|
+
/** Adds empty core properties if absent (docx4j addDocPropsCorePart). */
|
|
50
|
+
addDocPropsCorePart(): DocPropsCorePart;
|
|
51
|
+
addDocPropsExtendedPart(): DocPropsExtendedPart;
|
|
52
|
+
/** The package as a zip. */
|
|
53
|
+
save(options?: {
|
|
54
|
+
level?: number;
|
|
55
|
+
}): Promise<Uint8Array>;
|
|
56
|
+
/** The package as a flat OPC `pkg:package` string, for Office JS `insertOoxml()`. */
|
|
57
|
+
saveFlatOpc(options?: FlatOpcSinkOptions): Promise<string>;
|
|
58
|
+
/** Writes the package to any sink. */
|
|
59
|
+
saveTo<R>(sink: PartSink<R>): Promise<R>;
|
|
60
|
+
/** The `mso-application` progid written to flat OPC; subclasses know theirs. */
|
|
61
|
+
protected get progId(): string | undefined;
|
|
62
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { PartName } from '../opc/PartName.mjs';
|
|
2
|
+
import { ContentTypeManager } from '../opc/ContentTypeManager.mjs';
|
|
3
|
+
import { ZipPartStore, ZipPartSink } from '../opc/ZipPartStore.mjs';
|
|
4
|
+
import { FlatOpcPartStore, FlatOpcPartSink } from '../opc/FlatOpcPartStore.mjs';
|
|
5
|
+
import { loadPackage } from '../opc/Load.mjs';
|
|
6
|
+
import { savePackage } from '../opc/Save.mjs';
|
|
7
|
+
import { Docx4JException } from '../opc/exceptions.mjs';
|
|
8
|
+
import { Namespaces } from '../parts/Namespaces.mjs';
|
|
9
|
+
import { Parts } from '../parts/Parts.mjs';
|
|
10
|
+
import { XmlPart } from '../parts/XmlPart.mjs';
|
|
11
|
+
import { RelationshipsPart } from '../parts/RelationshipsPart.mjs';
|
|
12
|
+
import { DocPropsCorePart, DocPropsExtendedPart } from '../parts/docProps/index.mjs';
|
|
13
|
+
import { mcePreprocess } from '../opc/mce/preprocessor.mjs';
|
|
14
|
+
import { createPackageForContentType, registerGenericPackageClass } from './registry.mjs';
|
|
15
|
+
/**
|
|
16
|
+
* An Open Packaging Conventions package (docx4j org.docx4j.openpackaging.packages.OpcPackage):
|
|
17
|
+
* the content types, the package relationships and the parts reachable from them.
|
|
18
|
+
*/
|
|
19
|
+
export class OpcPackage {
|
|
20
|
+
constructor() {
|
|
21
|
+
/** The pseudo part name of the package itself: the source of the package relationships. */
|
|
22
|
+
this.partName = PartName.ROOT;
|
|
23
|
+
this.contentTypeManager = ContentTypeManager.createDefault();
|
|
24
|
+
this.parts = new Parts();
|
|
25
|
+
/** Custom XML parts by the (lower-cased) itemID of their properties part. */
|
|
26
|
+
this.customXmlDataStorageParts = new Map();
|
|
27
|
+
/** The options this package was loaded with. */
|
|
28
|
+
this.loadOptions = {};
|
|
29
|
+
this.relationshipsPart = RelationshipsPart.createPackageRels();
|
|
30
|
+
this.relationshipsPart.sourceP = this;
|
|
31
|
+
this.relationshipsPart.package = this;
|
|
32
|
+
}
|
|
33
|
+
get package() {
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Loads a package, picking the class (WordprocessingMLPackage, ...) from the main part's
|
|
38
|
+
* content type. A string is a flat OPC package; bytes are a zip; a PartStore is used as is.
|
|
39
|
+
*/
|
|
40
|
+
static async load(source, options = {}) {
|
|
41
|
+
const store = toStore(source);
|
|
42
|
+
const opts = { ...options };
|
|
43
|
+
if (opts.preprocessor === undefined && opts.mcePreprocess !== false)
|
|
44
|
+
opts.preprocessor = mcePreprocess;
|
|
45
|
+
const pkg = await loadPackage(store, createPackageForContentType, opts);
|
|
46
|
+
pkg.loadOptions = opts;
|
|
47
|
+
return pkg;
|
|
48
|
+
}
|
|
49
|
+
getPart(partName) {
|
|
50
|
+
return this.parts.get(partName);
|
|
51
|
+
}
|
|
52
|
+
getRelationshipsPart() {
|
|
53
|
+
return this.relationshipsPart;
|
|
54
|
+
}
|
|
55
|
+
/** Adds a part as a target of the package (docProps, the main part). */
|
|
56
|
+
addTargetPart(target, mode, proposedRelId) {
|
|
57
|
+
const rel = this.relationshipsPart.addPart(target, mode, proposedRelId);
|
|
58
|
+
this.setPartShortcut(target, target.relationshipType);
|
|
59
|
+
return rel;
|
|
60
|
+
}
|
|
61
|
+
setPartShortcut(part, relationshipType) {
|
|
62
|
+
switch (relationshipType) {
|
|
63
|
+
case Namespaces.PROPERTIES_CORE:
|
|
64
|
+
this.docPropsCorePart = part;
|
|
65
|
+
return true;
|
|
66
|
+
case Namespaces.PROPERTIES_EXTENDED:
|
|
67
|
+
this.docPropsExtendedPart = part;
|
|
68
|
+
return true;
|
|
69
|
+
case Namespaces.PROPERTIES_CUSTOM:
|
|
70
|
+
this.docPropsCustomPart = part;
|
|
71
|
+
return true;
|
|
72
|
+
default: return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** The main part: the target of the officeDocument relationship. */
|
|
76
|
+
getMainPart() {
|
|
77
|
+
var _a;
|
|
78
|
+
const rel = (_a = this.relationshipsPart.getRelationshipByType(Namespaces.DOCUMENT)) !== null && _a !== void 0 ? _a : this.relationshipsPart.getRelationshipByType(Namespaces.DOCUMENT_STRICT);
|
|
79
|
+
return rel ? this.relationshipsPart.getPart(rel) : undefined;
|
|
80
|
+
}
|
|
81
|
+
/** Unmarshals every XML part, so that `contents` is available synchronously everywhere. */
|
|
82
|
+
async unmarshalAll() {
|
|
83
|
+
for (const part of this.parts)
|
|
84
|
+
if (part instanceof XmlPart)
|
|
85
|
+
await part.getContents();
|
|
86
|
+
}
|
|
87
|
+
/** Adds empty core properties if absent (docx4j addDocPropsCorePart). */
|
|
88
|
+
addDocPropsCorePart() {
|
|
89
|
+
if (!this.docPropsCorePart) {
|
|
90
|
+
const part = new DocPropsCorePart();
|
|
91
|
+
part.setContents({});
|
|
92
|
+
this.addTargetPart(part);
|
|
93
|
+
}
|
|
94
|
+
return this.docPropsCorePart;
|
|
95
|
+
}
|
|
96
|
+
addDocPropsExtendedPart() {
|
|
97
|
+
if (!this.docPropsExtendedPart) {
|
|
98
|
+
const part = new DocPropsExtendedPart();
|
|
99
|
+
part.setContents({});
|
|
100
|
+
this.addTargetPart(part);
|
|
101
|
+
}
|
|
102
|
+
return this.docPropsExtendedPart;
|
|
103
|
+
}
|
|
104
|
+
/** The package as a zip. */
|
|
105
|
+
save(options) {
|
|
106
|
+
return this.saveTo(new ZipPartSink(options));
|
|
107
|
+
}
|
|
108
|
+
/** The package as a flat OPC `pkg:package` string, for Office JS `insertOoxml()`. */
|
|
109
|
+
saveFlatOpc(options) {
|
|
110
|
+
return this.saveTo(new FlatOpcPartSink({ progId: this.progId, ...options }));
|
|
111
|
+
}
|
|
112
|
+
/** Writes the package to any sink. */
|
|
113
|
+
saveTo(sink) {
|
|
114
|
+
return savePackage(this, sink);
|
|
115
|
+
}
|
|
116
|
+
/** The `mso-application` progid written to flat OPC; subclasses know theirs. */
|
|
117
|
+
get progId() {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function toStore(source) {
|
|
122
|
+
if (typeof source === 'string')
|
|
123
|
+
return new FlatOpcPartStore(source);
|
|
124
|
+
if (source instanceof Uint8Array || source instanceof ArrayBuffer)
|
|
125
|
+
return new ZipPartStore(source);
|
|
126
|
+
if (isPartStore(source))
|
|
127
|
+
return source;
|
|
128
|
+
throw new Docx4JException('Unsupported package source');
|
|
129
|
+
}
|
|
130
|
+
function isPartStore(value) {
|
|
131
|
+
return typeof value === 'object' && value !== null && typeof value.load === 'function' && typeof value.has === 'function';
|
|
132
|
+
}
|
|
133
|
+
registerGenericPackageClass(OpcPackage);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OpcPackage, type PackageSource } from './OpcPackage.mjs';
|
|
2
|
+
import type { LoadOptions } from '../opc/Load.mjs';
|
|
3
|
+
import type { Part } from '../parts/Part.mjs';
|
|
4
|
+
import type { MainPresentationPart } from '../parts/pml/index.mjs';
|
|
5
|
+
/** A pptx (docx4j PresentationMLPackage). */
|
|
6
|
+
export declare class PresentationMLPackage extends OpcPackage {
|
|
7
|
+
mainPresentationPart: MainPresentationPart | undefined;
|
|
8
|
+
static load(source: PackageSource, options?: LoadOptions): Promise<PresentationMLPackage>;
|
|
9
|
+
setPartShortcut(part: Part, relationshipType: string): boolean;
|
|
10
|
+
getMainPresentationPart(): MainPresentationPart;
|
|
11
|
+
protected get progId(): string;
|
|
12
|
+
}
|