@folijs/docx 0.6.0 → 1.0.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/CHANGELOG.md +124 -0
- package/README.md +12 -3
- package/dist/custom.d.ts +2 -0
- package/dist/custom.d.ts.map +1 -0
- package/dist/custom.js +216 -0
- package/dist/custom.js.map +1 -0
- package/dist/export/balance.d.ts +1 -16
- package/dist/export/balance.d.ts.map +1 -1
- package/dist/export/balance.js +1 -0
- package/dist/export/balance.js.map +1 -1
- package/dist/export/content.d.ts +1 -19
- package/dist/export/content.d.ts.map +1 -1
- package/dist/export/content.js +2 -0
- package/dist/export/content.js.map +1 -1
- package/dist/export/controls.d.ts +1 -17
- package/dist/export/controls.d.ts.map +1 -1
- package/dist/export/controls.js +1 -0
- package/dist/export/controls.js.map +1 -1
- package/dist/export/index.d.ts +8 -2
- package/dist/export/index.d.ts.map +1 -1
- package/dist/export/index.js +6 -2
- package/dist/export/index.js.map +1 -1
- package/dist/export/parts.d.ts +1 -24
- package/dist/export/parts.d.ts.map +1 -1
- package/dist/export/parts.js +23 -9
- package/dist/export/parts.js.map +1 -1
- package/dist/export/pictures.d.ts +0 -45
- package/dist/export/pictures.d.ts.map +1 -1
- package/dist/export/pictures.js +9 -2
- package/dist/export/pictures.js.map +1 -1
- package/dist/export/properties.d.ts +1 -41
- package/dist/export/properties.d.ts.map +1 -1
- package/dist/export/properties.js +19 -14
- package/dist/export/properties.js.map +1 -1
- package/dist/export/tables.d.ts +0 -19
- package/dist/export/tables.d.ts.map +1 -1
- package/dist/export/tables.js +15 -3
- package/dist/export/tables.js.map +1 -1
- package/dist/export/xmlout.d.ts +1 -61
- package/dist/export/xmlout.d.ts.map +1 -1
- package/dist/export/xmlout.js +34 -7
- package/dist/export/xmlout.js.map +1 -1
- package/dist/import/content.d.ts +1 -43
- package/dist/import/content.d.ts.map +1 -1
- package/dist/import/content.js +4 -1
- package/dist/import/content.js.map +1 -1
- package/dist/import/controls.d.ts +1 -13
- package/dist/import/controls.d.ts.map +1 -1
- package/dist/import/controls.js +5 -7
- package/dist/import/controls.js.map +1 -1
- package/dist/import/index.d.ts +1 -9
- package/dist/import/index.d.ts.map +1 -1
- package/dist/import/index.js +12 -5
- package/dist/import/index.js.map +1 -1
- package/dist/import/ooxml.d.ts +1 -41
- package/dist/import/ooxml.d.ts.map +1 -1
- package/dist/import/ooxml.js +40 -8
- package/dist/import/ooxml.js.map +1 -1
- package/dist/import/parts.d.ts +1 -33
- package/dist/import/parts.d.ts.map +1 -1
- package/dist/import/parts.js +35 -9
- package/dist/import/parts.js.map +1 -1
- package/dist/import/pictures.d.ts +1 -33
- package/dist/import/pictures.d.ts.map +1 -1
- package/dist/import/pictures.js +7 -1
- package/dist/import/pictures.js.map +1 -1
- package/dist/import/properties.d.ts +1 -45
- package/dist/import/properties.d.ts.map +1 -1
- package/dist/import/properties.js +30 -7
- package/dist/import/properties.js.map +1 -1
- package/dist/import/report.d.ts +0 -10
- package/dist/import/report.d.ts.map +1 -1
- package/dist/import/report.js +1 -0
- package/dist/import/report.js.map +1 -1
- package/dist/import/stories.d.ts +1 -18
- package/dist/import/stories.d.ts.map +1 -1
- package/dist/import/stories.js +16 -4
- package/dist/import/stories.js.map +1 -1
- package/dist/import/tables.d.ts +0 -24
- package/dist/import/tables.d.ts.map +1 -1
- package/dist/import/tables.js +25 -6
- package/dist/import/tables.js.map +1 -1
- package/dist/package.d.ts +8 -4
- package/dist/package.d.ts.map +1 -1
- package/dist/package.js +38 -4
- package/dist/package.js.map +1 -1
- package/dist/settings.d.ts +1 -21
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +17 -4
- package/dist/settings.js.map +1 -1
- package/dist/xml.d.ts +8 -0
- package/dist/xml.d.ts.map +1 -1
- package/dist/xml.js +30 -2
- package/dist/xml.js.map +1 -1
- package/dist/zip.d.ts +0 -47
- package/dist/zip.d.ts.map +1 -1
- package/dist/zip.js +11 -2
- package/dist/zip.js.map +1 -1
- package/package.json +5 -4
- package/src/custom.ts +254 -0
- package/src/export/balance.ts +1 -0
- package/src/export/content.ts +3 -0
- package/src/export/controls.ts +1 -0
- package/src/export/index.ts +16 -5
- package/src/export/parts.ts +23 -8
- package/src/export/pictures.ts +13 -3
- package/src/export/properties.ts +22 -16
- package/src/export/tables.ts +15 -3
- package/src/export/xmlout.ts +36 -7
- package/src/import/content.ts +6 -7
- package/src/import/controls.ts +6 -2
- package/src/import/index.ts +31 -23
- package/src/import/ooxml.ts +40 -8
- package/src/import/parts.ts +43 -18
- package/src/import/pictures.ts +8 -1
- package/src/import/properties.ts +32 -7
- package/src/import/report.ts +1 -0
- package/src/import/stories.ts +16 -4
- package/src/import/tables.ts +25 -6
- package/src/package.ts +56 -6
- package/src/settings.ts +17 -4
- package/src/xml.ts +42 -2
- package/src/zip.ts +17 -5
package/src/xml.ts
CHANGED
|
@@ -67,6 +67,8 @@ export interface XmlDocument {
|
|
|
67
67
|
/** Everything at the top level in order: declaration, comments, whitespace, the root. */
|
|
68
68
|
readonly children: readonly XmlNode[];
|
|
69
69
|
readonly root: XmlElement;
|
|
70
|
+
/** How many nodes the source holds: elements, attributes, text, comments, instructions. */
|
|
71
|
+
readonly nodes: number;
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
/** Source that is not well-formed XML; `offset` is where in the source. */
|
|
@@ -80,6 +82,12 @@ export class XmlError extends Error {
|
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
|
|
85
|
+
/**
|
|
86
|
+
* More nodes than `maxNodes` allows.
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
export class XmlLimitError extends XmlError {}
|
|
90
|
+
|
|
83
91
|
const XML_NS = "http://www.w3.org/XML/1998/namespace";
|
|
84
92
|
const XMLNS_NS = "http://www.w3.org/2000/xmlns/";
|
|
85
93
|
const ENTITIES: Record<string, string> = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'" };
|
|
@@ -103,6 +111,12 @@ export interface ParseXmlOptions {
|
|
|
103
111
|
* tables, content controls) stay far below, and code walking the tree recursively is safe.
|
|
104
112
|
*/
|
|
105
113
|
readonly maxDepth?: number;
|
|
114
|
+
/**
|
|
115
|
+
* Most nodes accepted: elements, attributes, text, comments and processing instructions, each
|
|
116
|
+
* one, since each costs memory. No limit by default here; `readPackage` and `importDocx` set
|
|
117
|
+
* one for a package (its `maxNodes`).
|
|
118
|
+
*/
|
|
119
|
+
readonly maxNodes?: number;
|
|
106
120
|
}
|
|
107
121
|
|
|
108
122
|
/** The first character XML 1.0 does not allow (outside its Char production), or -1. */
|
|
@@ -124,7 +138,26 @@ function illegalCharacter(source: string): number {
|
|
|
124
138
|
|
|
125
139
|
/** Parses `source`; throws {@link XmlError} where it is not well-formed. */
|
|
126
140
|
export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocument {
|
|
141
|
+
return parseXmlCosting(source, options);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* `parseXml`, an element costing `cost(namespace, local name)` nodes of `maxNodes` (default 1).
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
export function parseXmlCosting(
|
|
149
|
+
source: string,
|
|
150
|
+
options: ParseXmlOptions,
|
|
151
|
+
cost?: (ns: string, local: string) => number,
|
|
152
|
+
): XmlDocument {
|
|
127
153
|
const maxDepth = options.maxDepth ?? 1000;
|
|
154
|
+
const maxNodes = options.maxNodes ?? Number.POSITIVE_INFINITY;
|
|
155
|
+
let nodes = 0;
|
|
156
|
+
/** Every node read costs memory, whatever its kind: counted before it is kept. */
|
|
157
|
+
const charge = (offset: number, n = 1) => {
|
|
158
|
+
nodes += n;
|
|
159
|
+
if (nodes > maxNodes) throw new XmlLimitError(`More than ${maxNodes} nodes`, offset);
|
|
160
|
+
};
|
|
128
161
|
if (source.charCodeAt(0) === 0xfeff) {
|
|
129
162
|
throw new XmlError("Byte order mark in the text: decode the bytes without it", 0);
|
|
130
163
|
}
|
|
@@ -151,6 +184,7 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
151
184
|
}
|
|
152
185
|
const cdataEnd = raw.indexOf("]]>");
|
|
153
186
|
if (cdataEnd !== -1) throw new XmlError('"]]>" in text', at + cdataEnd);
|
|
187
|
+
charge(at);
|
|
154
188
|
siblings().push({
|
|
155
189
|
kind: "text",
|
|
156
190
|
text: decode(raw, false, at),
|
|
@@ -167,12 +201,14 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
167
201
|
if (close === -1) throw new XmlError("Unclosed comment", at);
|
|
168
202
|
const dashes = source.indexOf("--", at + 4);
|
|
169
203
|
if (dashes < close) throw new XmlError('"--" in a comment', dashes);
|
|
204
|
+
charge(at);
|
|
170
205
|
siblings().push({ kind: "comment", start: at, end: close + 3 });
|
|
171
206
|
at = close + 3;
|
|
172
207
|
} else if (source.startsWith("<![CDATA[", at)) {
|
|
173
208
|
if (stack.length === 0) throw new XmlError("CDATA outside the root element", at);
|
|
174
209
|
const close = source.indexOf("]]>", at + 9);
|
|
175
210
|
if (close === -1) throw new XmlError("Unclosed CDATA section", at);
|
|
211
|
+
charge(at);
|
|
176
212
|
siblings().push({
|
|
177
213
|
kind: "text",
|
|
178
214
|
text: source.slice(at + 9, close).replace(/\r\n?/g, "\n"),
|
|
@@ -192,6 +228,7 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
192
228
|
if (declaration && (at !== 0 || target !== "xml")) {
|
|
193
229
|
throw new XmlError("XML declaration not at the start", at);
|
|
194
230
|
}
|
|
231
|
+
charge(at);
|
|
195
232
|
siblings().push({
|
|
196
233
|
kind: declaration ? "declaration" : "instruction",
|
|
197
234
|
start: at,
|
|
@@ -219,7 +256,7 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
219
256
|
const unclosed = stack[stack.length - 1];
|
|
220
257
|
if (unclosed) throw new XmlError(`Unclosed element <${unclosed.name}>`, unclosed.start);
|
|
221
258
|
if (!root) throw new XmlError("No root element", 0);
|
|
222
|
-
return { source, children: top, root };
|
|
259
|
+
return { source, children: top, root, nodes };
|
|
223
260
|
|
|
224
261
|
function finish(element: XmlElement, start: number): void {
|
|
225
262
|
if (stack.length === 0) {
|
|
@@ -265,6 +302,7 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
265
302
|
if (value.includes("<")) throw new XmlError(`"<" in attribute ${attrName}`, open);
|
|
266
303
|
if (names.has(attrName)) throw new XmlError(`Duplicate attribute ${attrName}`, next);
|
|
267
304
|
names.add(attrName);
|
|
305
|
+
charge(next);
|
|
268
306
|
raw.push({
|
|
269
307
|
name: attrName,
|
|
270
308
|
value: decode(value, true, open + 1),
|
|
@@ -319,10 +357,12 @@ export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocu
|
|
|
319
357
|
if (stack.length >= maxDepth) {
|
|
320
358
|
throw new XmlError(`Elements nested deeper than ${maxDepth}`, start);
|
|
321
359
|
}
|
|
360
|
+
const ns = resolve(scope, qualified.prefix, start);
|
|
361
|
+
charge(start, cost?.(ns, qualified.local) ?? 1);
|
|
322
362
|
const element = {
|
|
323
363
|
kind: "element" as const,
|
|
324
364
|
...qualified,
|
|
325
|
-
ns
|
|
365
|
+
ns,
|
|
326
366
|
attributes,
|
|
327
367
|
start,
|
|
328
368
|
openEnd: at,
|
package/src/zip.ts
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
* replaced entries are compressed again.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* A file in the zip. `raw` is its data as stored (compressed when `method` is 8).
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
11
14
|
export interface ZipEntry {
|
|
12
15
|
readonly name: string;
|
|
13
16
|
/** The name's bytes as stored, written back unchanged. */
|
|
@@ -57,6 +60,7 @@ const textDecoder = new TextDecoder();
|
|
|
57
60
|
* Lists the entries of a zip from its central directory, in the directory's order. Nothing is
|
|
58
61
|
* decompressed: see {@link inflateEntry}. Every entry is checked against its local header, and
|
|
59
62
|
* entries may not overlap: Foli reads what any other zip reader would.
|
|
63
|
+
* @internal
|
|
60
64
|
*/
|
|
61
65
|
export function readZip(bytes: Uint8Array, limits: ZipLimits = {}): ZipEntry[] {
|
|
62
66
|
const maxEntries = limits.maxEntries ?? 10_000;
|
|
@@ -227,7 +231,10 @@ function findEnd(view: DataView): number {
|
|
|
227
231
|
throw new ZipError("Not a zip archive");
|
|
228
232
|
}
|
|
229
233
|
|
|
230
|
-
/**
|
|
234
|
+
/**
|
|
235
|
+
* The entry's data, decompressed, checked against its size and CRC-32.
|
|
236
|
+
* @internal
|
|
237
|
+
*/
|
|
231
238
|
export async function inflateEntry(entry: ZipEntry): Promise<Uint8Array> {
|
|
232
239
|
const data =
|
|
233
240
|
entry.method === 0
|
|
@@ -265,6 +272,7 @@ async function inflateRaw(raw: Uint8Array, size: number, name: string): Promise<
|
|
|
265
272
|
/**
|
|
266
273
|
* A new deflated entry holding `data`. With `like` (the entry it replaces) it keeps that entry's
|
|
267
274
|
* name bytes, name encoding flag, time and date.
|
|
275
|
+
* @internal
|
|
268
276
|
*/
|
|
269
277
|
export async function deflateEntry(
|
|
270
278
|
name: string,
|
|
@@ -296,8 +304,9 @@ export async function deflateEntry(
|
|
|
296
304
|
* The zip of `entries`, in order; each entry's data written as it is in `raw`. An entry read
|
|
297
305
|
* from a zip keeps its headers byte for byte (only its offset in the directory changes), so a
|
|
298
306
|
* zip written back with its entries unchanged is the same file, short of a trailing comment.
|
|
307
|
+
* @internal
|
|
299
308
|
*/
|
|
300
|
-
export function writeZip(entries: readonly ZipEntry[]): Uint8Array {
|
|
309
|
+
export function writeZip(entries: readonly ZipEntry[]): Uint8Array<ArrayBuffer> {
|
|
301
310
|
const parts: Uint8Array[] = [];
|
|
302
311
|
const directory: Uint8Array[] = [];
|
|
303
312
|
let offset = 0;
|
|
@@ -371,7 +380,7 @@ function streamOf(data: Uint8Array): ReadableStream<BufferSource> {
|
|
|
371
380
|
});
|
|
372
381
|
}
|
|
373
382
|
|
|
374
|
-
function concat(chunks: readonly Uint8Array[]): Uint8Array {
|
|
383
|
+
function concat(chunks: readonly Uint8Array[]): Uint8Array<ArrayBuffer> {
|
|
375
384
|
const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
|
|
376
385
|
let at = 0;
|
|
377
386
|
for (const chunk of chunks) {
|
|
@@ -383,7 +392,10 @@ function concat(chunks: readonly Uint8Array[]): Uint8Array {
|
|
|
383
392
|
|
|
384
393
|
let table: Uint32Array | undefined;
|
|
385
394
|
|
|
386
|
-
/**
|
|
395
|
+
/**
|
|
396
|
+
* CRC-32 (IEEE), as zip stores it.
|
|
397
|
+
* @internal
|
|
398
|
+
*/
|
|
387
399
|
export function crc32(data: Uint8Array): number {
|
|
388
400
|
if (!table) {
|
|
389
401
|
table = new Uint32Array(256);
|