@folijs/core 0.4.0 → 0.5.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 +21 -0
- package/dist/model/schema.d.ts +9 -1
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +13 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/state/commands.d.ts +8 -1
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +87 -34
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.js +3 -3
- package/dist/state/comments.js.map +1 -1
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +2 -0
- package/dist/state/editor.js.map +1 -1
- package/dist/state/index.d.ts +4 -1
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +4 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +30 -19
- package/dist/state/links.js.map +1 -1
- package/dist/state/pictures.d.ts +51 -0
- package/dist/state/pictures.d.ts.map +1 -0
- package/dist/state/pictures.js +140 -0
- package/dist/state/pictures.js.map +1 -0
- package/dist/state/review.d.ts +1 -1
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +92 -13
- package/dist/state/review.js.map +1 -1
- package/dist/state/selection.d.ts +12 -1
- package/dist/state/selection.d.ts.map +1 -1
- package/dist/state/selection.js +12 -0
- package/dist/state/selection.js.map +1 -1
- package/dist/state/tablecommands.d.ts +77 -0
- package/dist/state/tablecommands.d.ts.map +1 -0
- package/dist/state/tablecommands.js +675 -0
- package/dist/state/tablecommands.js.map +1 -0
- package/dist/state/tables.d.ts +110 -0
- package/dist/state/tables.d.ts.map +1 -0
- package/dist/state/tables.js +729 -0
- package/dist/state/tables.js.map +1 -0
- package/dist/state/track.js +55 -6
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.js +1 -1
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +26 -9
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +9 -1
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +38 -16
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +114 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +170 -13
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +2 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +2 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/migrate.d.ts +2 -2
- package/dist/v3/migrate.d.ts.map +1 -1
- package/dist/v3/resolve.d.ts +50 -4
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +205 -15
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +4 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +166 -6
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/model/schema.ts +18 -2
- package/src/state/commands.ts +80 -28
- package/src/state/comments.ts +3 -3
- package/src/state/editor.ts +2 -0
- package/src/state/index.ts +34 -0
- package/src/state/links.ts +27 -17
- package/src/state/pictures.ts +168 -0
- package/src/state/review.ts +96 -14
- package/src/state/selection.ts +15 -1
- package/src/state/tablecommands.ts +741 -0
- package/src/state/tables.ts +760 -0
- package/src/state/track.ts +63 -6
- package/src/state/word.ts +1 -1
- package/src/state/wordclip.ts +28 -11
- package/src/state/wordformat.ts +35 -13
- package/src/v3/document.ts +285 -14
- package/src/v3/index.ts +14 -0
- package/src/v3/migrate.ts +2 -2
- package/src/v3/resolve.ts +259 -15
- package/src/v3/schema.ts +187 -7
package/src/v3/document.ts
CHANGED
|
@@ -17,14 +17,17 @@ import {
|
|
|
17
17
|
} from "./check.js";
|
|
18
18
|
import { migrateToV3 } from "./migrate.js";
|
|
19
19
|
import {
|
|
20
|
+
checkCellProperties,
|
|
21
|
+
checkRowProperties,
|
|
20
22
|
checkSection,
|
|
23
|
+
checkTableProperties,
|
|
21
24
|
checkParagraphProperties as paragraphProperties,
|
|
22
25
|
checkRunProperties as runProperties,
|
|
23
26
|
schema,
|
|
24
27
|
} from "./schema.js";
|
|
25
28
|
|
|
26
|
-
/** The schema version of documents in the Word model (ADR-0005). */
|
|
27
|
-
export const SCHEMA_VERSION =
|
|
29
|
+
/** The schema version of documents in the Word model (ADR-0005; v4 adds tables and pictures, ADR-0006). */
|
|
30
|
+
export const SCHEMA_VERSION = 4;
|
|
28
31
|
|
|
29
32
|
/** Page setup of a section, in twips (1/1440 inch). Missing parts take Word's defaults. */
|
|
30
33
|
export interface SectionJSON {
|
|
@@ -83,6 +86,96 @@ export interface ParagraphPropertiesJSON {
|
|
|
83
86
|
ext?: string;
|
|
84
87
|
}
|
|
85
88
|
|
|
89
|
+
/** A table measure: twips (`dxa`), fiftieths of a percent (`pct`), or `auto`/`nil` with 0. */
|
|
90
|
+
export interface MeasureJSON {
|
|
91
|
+
value: number;
|
|
92
|
+
type: "auto" | "dxa" | "pct" | "nil";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type Sides<T, Extra extends string = never> = Partial<
|
|
96
|
+
Record<"top" | "left" | "bottom" | "right" | "start" | "end" | Extra, T>
|
|
97
|
+
>;
|
|
98
|
+
|
|
99
|
+
/** Table properties (`w:tblPr`): a table's attributes, a table style's, a row's exceptions. */
|
|
100
|
+
export interface TablePropertiesJSON {
|
|
101
|
+
style?: string;
|
|
102
|
+
width?: MeasureJSON;
|
|
103
|
+
justification?: "left" | "center" | "right" | "start" | "end";
|
|
104
|
+
indent?: MeasureJSON;
|
|
105
|
+
layout?: "fixed" | "autofit";
|
|
106
|
+
borders?: Sides<BorderJSON, "insideH" | "insideV">;
|
|
107
|
+
cellMargins?: Sides<MeasureJSON>;
|
|
108
|
+
cellSpacing?: MeasureJSON;
|
|
109
|
+
shading?: ParagraphPropertiesJSON["shading"];
|
|
110
|
+
look?: {
|
|
111
|
+
firstRow?: boolean;
|
|
112
|
+
lastRow?: boolean;
|
|
113
|
+
firstColumn?: boolean;
|
|
114
|
+
lastColumn?: boolean;
|
|
115
|
+
noHBand?: boolean;
|
|
116
|
+
noVBand?: boolean;
|
|
117
|
+
};
|
|
118
|
+
rowBandSize?: number;
|
|
119
|
+
columnBandSize?: number;
|
|
120
|
+
ext?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Row properties (`w:trPr`). */
|
|
124
|
+
export interface RowPropertiesJSON {
|
|
125
|
+
height?: { value: number; rule?: "auto" | "exact" | "atLeast" };
|
|
126
|
+
cantSplit?: boolean;
|
|
127
|
+
header?: boolean;
|
|
128
|
+
gridBefore?: number;
|
|
129
|
+
widthBefore?: MeasureJSON;
|
|
130
|
+
gridAfter?: number;
|
|
131
|
+
widthAfter?: MeasureJSON;
|
|
132
|
+
ext?: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Cell properties (`w:tcPr`). */
|
|
136
|
+
export interface CellPropertiesJSON {
|
|
137
|
+
width?: MeasureJSON;
|
|
138
|
+
gridSpan?: number;
|
|
139
|
+
vMerge?: "restart" | "continue";
|
|
140
|
+
borders?: Sides<BorderJSON, "insideH" | "insideV" | "tl2br" | "tr2bl">;
|
|
141
|
+
shading?: ParagraphPropertiesJSON["shading"];
|
|
142
|
+
margins?: Sides<MeasureJSON>;
|
|
143
|
+
verticalAlign?: "top" | "center" | "bottom" | "both";
|
|
144
|
+
textDirection?: string;
|
|
145
|
+
noWrap?: boolean;
|
|
146
|
+
ext?: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The parts of a table style a conditional format (`w:tblStylePr`) can apply to. */
|
|
150
|
+
export const TABLE_CONDITIONS = [
|
|
151
|
+
"wholeTable",
|
|
152
|
+
"band1Vert",
|
|
153
|
+
"band2Vert",
|
|
154
|
+
"band1Horz",
|
|
155
|
+
"band2Horz",
|
|
156
|
+
"firstCol",
|
|
157
|
+
"lastCol",
|
|
158
|
+
"firstRow",
|
|
159
|
+
"lastRow",
|
|
160
|
+
"nwCell",
|
|
161
|
+
"neCell",
|
|
162
|
+
"swCell",
|
|
163
|
+
"seCell",
|
|
164
|
+
] as const;
|
|
165
|
+
export type TableCondition = (typeof TABLE_CONDITIONS)[number];
|
|
166
|
+
|
|
167
|
+
/** A table style's conditional format (`w:tblStylePr`): what its part of the table looks like. */
|
|
168
|
+
export interface TableConditionJSON {
|
|
169
|
+
type: Exclude<TableCondition, "wholeTable">;
|
|
170
|
+
paragraph?: ParagraphPropertiesJSON;
|
|
171
|
+
run?: MarkJSON[];
|
|
172
|
+
table?: TablePropertiesJSON;
|
|
173
|
+
row?: RowPropertiesJSON;
|
|
174
|
+
cell?: CellPropertiesJSON;
|
|
175
|
+
/** The `w:tblStylePr` this was read from, verbatim. */
|
|
176
|
+
ext?: string;
|
|
177
|
+
}
|
|
178
|
+
|
|
86
179
|
export interface StyleJSON {
|
|
87
180
|
/** `w:styleId`, what paragraphs and runs reference. With duplicates, the first wins, as in Word. */
|
|
88
181
|
id: string;
|
|
@@ -91,14 +184,36 @@ export interface StyleJSON {
|
|
|
91
184
|
basedOn?: string;
|
|
92
185
|
next?: string;
|
|
93
186
|
link?: string;
|
|
94
|
-
/** The default style of its type (Normal, Default Paragraph Font…). */
|
|
187
|
+
/** The default style of its type (Normal, Default Paragraph Font, Normal Table…). */
|
|
95
188
|
isDefault?: boolean;
|
|
96
189
|
paragraph?: ParagraphPropertiesJSON;
|
|
97
190
|
run?: MarkJSON[];
|
|
98
|
-
/**
|
|
191
|
+
/** A table style's whole-table properties. */
|
|
192
|
+
table?: TablePropertiesJSON;
|
|
193
|
+
row?: RowPropertiesJSON;
|
|
194
|
+
cell?: CellPropertiesJSON;
|
|
195
|
+
/** A table style's conditional formats (first row, bands…). */
|
|
196
|
+
conditions?: TableConditionJSON[];
|
|
197
|
+
/** The `w:style` this was read from, verbatim (UI priority…). */
|
|
99
198
|
ext?: string;
|
|
100
199
|
}
|
|
101
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Where a picture's bytes are (schema v4): a part of the original package, a URL the app gave
|
|
203
|
+
* (its upload hook's), or, when the app opts into it, a `data:` URL. At least one.
|
|
204
|
+
*/
|
|
205
|
+
export interface ResourceJSON {
|
|
206
|
+
/** `image/png`, `image/jpeg`, `image/svg+xml`… */
|
|
207
|
+
contentType: string;
|
|
208
|
+
/** Bytes, when known. */
|
|
209
|
+
size?: number;
|
|
210
|
+
/** The package part (`word/media/image1.png`). */
|
|
211
|
+
part?: string;
|
|
212
|
+
url?: string;
|
|
213
|
+
/** A `data:` URL holding the bytes. */
|
|
214
|
+
data?: string;
|
|
215
|
+
}
|
|
216
|
+
|
|
102
217
|
export interface NumberingLevelJSON {
|
|
103
218
|
level: number;
|
|
104
219
|
start?: number;
|
|
@@ -179,11 +294,23 @@ export interface FoliDocument {
|
|
|
179
294
|
numbering: { abstracts: AbstractNumberingJSON[]; instances: NumberingInstanceJSON[] };
|
|
180
295
|
theme?: ThemeJSON;
|
|
181
296
|
comments: CommentJSON[];
|
|
297
|
+
/** The pictures' bytes, by the key `picture` nodes give (none when missing). */
|
|
298
|
+
resources?: Record<string, ResourceJSON>;
|
|
182
299
|
content: NodeJSON;
|
|
183
300
|
}
|
|
184
301
|
|
|
302
|
+
/** A document of schema v3 (no tables or pictures in the model), as `migrateToV3` gives it. */
|
|
303
|
+
export type FoliDocumentV3 = Omit<FoliDocument, "schemaVersion" | "resources"> & {
|
|
304
|
+
schemaVersion: 3;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/** Schema v3 → v4: the version only (a table kept opaque stays so until imported again). */
|
|
308
|
+
export function migrateToV4(doc: FoliDocumentV3): FoliDocument {
|
|
309
|
+
return { ...doc, schemaVersion: SCHEMA_VERSION };
|
|
310
|
+
}
|
|
311
|
+
|
|
185
312
|
/**
|
|
186
|
-
* A new document: `content` (schema
|
|
313
|
+
* A new document: `content` (schema v4; default: one empty paragraph) on A4 with 25.4 mm
|
|
187
314
|
* margins, with the styles and list definitions the editor's commands use (Normal, Heading 1–6,
|
|
188
315
|
* List Paragraph, Hyperlink; bullets and numbers) and 11 pt Georgia, 1.5 lines: the look a
|
|
189
316
|
* migrated 0.2.0 document has. `section` (in twips: `mmToTwips`) is merged over that page:
|
|
@@ -195,11 +322,9 @@ export function createDocument(
|
|
|
195
322
|
section?: SectionJSON,
|
|
196
323
|
): FoliDocument {
|
|
197
324
|
const empty = { type: "doc", content: [] } as const satisfies NodeJSON;
|
|
198
|
-
const blank =
|
|
199
|
-
schemaVersion: 2,
|
|
200
|
-
|
|
201
|
-
content: empty as never,
|
|
202
|
-
});
|
|
325
|
+
const blank = migrateToV4(
|
|
326
|
+
migrateToV3({ schemaVersion: 2, settings: DEFAULT_SETTINGS, content: empty as never }),
|
|
327
|
+
);
|
|
203
328
|
const base = blank.section;
|
|
204
329
|
return {
|
|
205
330
|
...blank,
|
|
@@ -227,7 +352,28 @@ export function createDocument(
|
|
|
227
352
|
export function parseDocument(input: unknown): FoliDocument {
|
|
228
353
|
if (!isRecord(input)) throw new FoliDocumentError("Document must be an object");
|
|
229
354
|
const version = input.schemaVersion;
|
|
230
|
-
if (version === 1 || version === 2)
|
|
355
|
+
if (version === 1 || version === 2) {
|
|
356
|
+
return parseDocument(migrateToV4(migrateToV3(parseDocumentV2(input))));
|
|
357
|
+
}
|
|
358
|
+
if (version === 3) {
|
|
359
|
+
if (Object.hasOwn(input, "resources")) fail("resources", "unknown key (schema 4 and later)");
|
|
360
|
+
const doc = parseDocument({ ...input, schemaVersion: SCHEMA_VERSION });
|
|
361
|
+
// Tables and pictures are schema 4's: a schema 3 document holding one is not one.
|
|
362
|
+
for (const [path, content] of [
|
|
363
|
+
["content", doc.content],
|
|
364
|
+
...doc.comments.map((c, i) => [`comments[${i}].content`, { content: c.content }] as const),
|
|
365
|
+
] as const) {
|
|
366
|
+
const found = nodeOfV4(content);
|
|
367
|
+
if (found) fail(path, `${found} is a schema 4 node (the document says schema 3)`);
|
|
368
|
+
}
|
|
369
|
+
doc.styles.forEach((style, i) => {
|
|
370
|
+
for (const key of ["table", "row", "cell", "conditions"]) {
|
|
371
|
+
if (Object.hasOwn(style, key))
|
|
372
|
+
fail(`styles[${i}]`, `unknown key ${key} (schema 4 and later)`);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
return doc;
|
|
376
|
+
}
|
|
231
377
|
if (version !== SCHEMA_VERSION) {
|
|
232
378
|
const newer = Number.isInteger(version) && (version as number) > SCHEMA_VERSION;
|
|
233
379
|
throw new FoliDocumentError(
|
|
@@ -255,6 +401,7 @@ export function parseDocument(input: unknown): FoliDocument {
|
|
|
255
401
|
);
|
|
256
402
|
if (input.theme !== undefined) check("theme", input.theme, checkTheme);
|
|
257
403
|
check("comments", input.comments, listOf(checkComment));
|
|
404
|
+
if (input.resources !== undefined) check("resources", input.resources, checkResources);
|
|
258
405
|
const styles = input.styles as StyleJSON[];
|
|
259
406
|
const comments = input.comments as CommentJSON[];
|
|
260
407
|
noCycles(
|
|
@@ -280,7 +427,8 @@ export function parseDocument(input: unknown): FoliDocument {
|
|
|
280
427
|
styles,
|
|
281
428
|
numbering: input.numbering,
|
|
282
429
|
theme: input.theme,
|
|
283
|
-
|
|
430
|
+
resources: input.resources,
|
|
431
|
+
}) as Pick<FoliDocument, "section" | "defaults" | "styles" | "numbering" | "theme" | "resources">;
|
|
284
432
|
return {
|
|
285
433
|
schemaVersion: SCHEMA_VERSION,
|
|
286
434
|
section: envelope.section,
|
|
@@ -292,18 +440,67 @@ export function parseDocument(input: unknown): FoliDocument {
|
|
|
292
440
|
...structuredClone({ ...c, content: [] }),
|
|
293
441
|
content: blocks(c.content, `comments[${i}].content`).map((block) => block.toJSON()),
|
|
294
442
|
})),
|
|
443
|
+
...(envelope.resources === undefined ? {} : { resources: envelope.resources }),
|
|
295
444
|
content: content.toJSON(),
|
|
296
445
|
};
|
|
297
446
|
}
|
|
298
447
|
|
|
299
|
-
/**
|
|
300
|
-
|
|
448
|
+
/**
|
|
449
|
+
* Parses untrusted schema v4 content into a `doc` node, with Word's rules the content
|
|
450
|
+
* expressions do not say (`fragment`: content to paste, which a table may end): a table is never
|
|
451
|
+
* followed by a table (Word would merge them) nor ends
|
|
452
|
+
* the document, and a paragraph in a cell ends no section and holds no page or column break.
|
|
453
|
+
*/
|
|
454
|
+
export function parseContent(value: unknown, path = "content", fragment = false): Node {
|
|
301
455
|
const doc = schema.nodeFromJSON(value, path);
|
|
302
456
|
if (doc.type !== schema.topNodeType) fail(path, 'root node must be of type "doc"');
|
|
303
457
|
if (doc.marks.length) fail(`${path}.marks`, "doc cannot have marks");
|
|
458
|
+
wordRules(doc, path, false, fragment);
|
|
304
459
|
return doc;
|
|
305
460
|
}
|
|
306
461
|
|
|
462
|
+
function wordRules(parent: Node, path: string, inCell: boolean, fragment = false): void {
|
|
463
|
+
parent.forEach((node, _offset, index) => {
|
|
464
|
+
const at = `${path}.content[${index}]`;
|
|
465
|
+
if (node.type.name === "table") {
|
|
466
|
+
const next = parent.maybeChild(index + 1);
|
|
467
|
+
if (next?.type.name === "table") {
|
|
468
|
+
fail(at, "a table cannot follow a table (Word would merge them): a paragraph goes between");
|
|
469
|
+
}
|
|
470
|
+
if (!next && parent.type.name === "doc" && !fragment) {
|
|
471
|
+
fail(at, "a table cannot end the document: a paragraph follows it, as in Word");
|
|
472
|
+
}
|
|
473
|
+
node.forEach((row, _r, r) => {
|
|
474
|
+
row.forEach((cell, _c, c) => {
|
|
475
|
+
wordRules(cell, `${at}.content[${r}].content[${c}]`, true);
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
} else if (inCell && node.type.name === "paragraph") {
|
|
479
|
+
if (node.attrs.section)
|
|
480
|
+
fail(`${at}.attrs.section`, "a paragraph in a table cannot end a section");
|
|
481
|
+
node.forEach((inline, _i, i) => {
|
|
482
|
+
if (inline.type.name === "pageBreak" || inline.type.name === "columnBreak") {
|
|
483
|
+
fail(`${at}.content[${i}]`, `a ${inline.type.name} cannot be in a table`);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const V4_NODES = new Set(["table", "tableRow", "tableCell", "picture"]);
|
|
491
|
+
|
|
492
|
+
/** The first schema 4 node type in validated content JSON, if any. */
|
|
493
|
+
function nodeOfV4(content: NodeJSON | { content: NodeJSON[] }): string | undefined {
|
|
494
|
+
const stack: unknown[] = [content];
|
|
495
|
+
while (stack.length) {
|
|
496
|
+
const node = stack.pop();
|
|
497
|
+
if (!isRecord(node)) continue;
|
|
498
|
+
if (V4_NODES.has(node.type as string)) return node.type as string;
|
|
499
|
+
if (Array.isArray(node.content)) for (const child of node.content) stack.push(child);
|
|
500
|
+
}
|
|
501
|
+
return undefined;
|
|
502
|
+
}
|
|
503
|
+
|
|
307
504
|
const ENVELOPE_KEYS = [
|
|
308
505
|
"schemaVersion",
|
|
309
506
|
"section",
|
|
@@ -312,6 +509,7 @@ const ENVELOPE_KEYS = [
|
|
|
312
509
|
"numbering",
|
|
313
510
|
"theme",
|
|
314
511
|
"comments",
|
|
512
|
+
"resources",
|
|
315
513
|
"content",
|
|
316
514
|
];
|
|
317
515
|
|
|
@@ -363,6 +561,19 @@ function blocks(content: NodeJSON[], path: string): Node[] {
|
|
|
363
561
|
return list;
|
|
364
562
|
}
|
|
365
563
|
|
|
564
|
+
const checkCondition: Check = shape(
|
|
565
|
+
{
|
|
566
|
+
type: oneOf(TABLE_CONDITIONS.filter((c) => c !== "wholeTable")),
|
|
567
|
+
paragraph: paragraphProperties,
|
|
568
|
+
run: runProperties,
|
|
569
|
+
table: checkTableProperties,
|
|
570
|
+
row: checkRowProperties,
|
|
571
|
+
cell: checkCellProperties,
|
|
572
|
+
ext: xml,
|
|
573
|
+
},
|
|
574
|
+
["type"],
|
|
575
|
+
);
|
|
576
|
+
|
|
366
577
|
const checkStyle: Check = shape(
|
|
367
578
|
{
|
|
368
579
|
id: text(),
|
|
@@ -374,11 +585,71 @@ const checkStyle: Check = shape(
|
|
|
374
585
|
isDefault: bool,
|
|
375
586
|
paragraph: paragraphProperties,
|
|
376
587
|
run: runProperties,
|
|
588
|
+
table: checkTableProperties,
|
|
589
|
+
row: checkRowProperties,
|
|
590
|
+
cell: checkCellProperties,
|
|
591
|
+
conditions: listOf(checkCondition, TABLE_CONDITIONS.length),
|
|
377
592
|
ext: xml,
|
|
378
593
|
},
|
|
379
594
|
["id", "type"],
|
|
380
595
|
);
|
|
381
596
|
|
|
597
|
+
const SAFE_PROTOCOLS = new Set(["http:", "https:", "blob:"]);
|
|
598
|
+
|
|
599
|
+
/** A URL's scheme as a browser resolves it (a relative URL is the base's, https). */
|
|
600
|
+
function protocolOf(url: string): string {
|
|
601
|
+
try {
|
|
602
|
+
return new URL(url, "https://base.invalid/").protocol;
|
|
603
|
+
} catch {
|
|
604
|
+
return "";
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** Pictures only: `image/png`, `image/svg+xml`… */
|
|
609
|
+
const IMAGE_TYPE = /^image\/[A-Za-z0-9][\w.+-]{0,126}$/;
|
|
610
|
+
|
|
611
|
+
export const checkResource: Check = (v) => {
|
|
612
|
+
const error = shape(
|
|
613
|
+
{
|
|
614
|
+
contentType: (t) =>
|
|
615
|
+
typeof t === "string" && IMAGE_TYPE.test(t) ? undefined : "must be an image media type",
|
|
616
|
+
size: int(0, Number.MAX_SAFE_INTEGER),
|
|
617
|
+
part: text(1024),
|
|
618
|
+
// Relative, or http(s) or blob: no scheme that runs code (`javascript:`) where it is shown.
|
|
619
|
+
// Read as browsers read it (leading spaces, controls and inner tabs dropped).
|
|
620
|
+
url: (u) =>
|
|
621
|
+
text(8192)(u) ??
|
|
622
|
+
(SAFE_PROTOCOLS.has(protocolOf(u as string))
|
|
623
|
+
? undefined
|
|
624
|
+
: "must be relative, http(s) or blob"),
|
|
625
|
+
data: (d) =>
|
|
626
|
+
typeof d === "string" && /^data:image\/[\w.+-]+[;,]/i.test(d) && d.length <= 67_108_864
|
|
627
|
+
? undefined
|
|
628
|
+
: "must be a data: URL of an image",
|
|
629
|
+
},
|
|
630
|
+
["contentType"],
|
|
631
|
+
)(v);
|
|
632
|
+
if (error) return error;
|
|
633
|
+
const r = v as ResourceJSON;
|
|
634
|
+
return r.part === undefined && r.url === undefined && r.data === undefined
|
|
635
|
+
? "needs part, url or data"
|
|
636
|
+
: undefined;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const checkResources: Check = (v) => {
|
|
640
|
+
if (!isRecord(v)) return "must be an object";
|
|
641
|
+
const entries = Object.entries(v);
|
|
642
|
+
if (entries.length > 100_000) return "must have at most 100000 resources";
|
|
643
|
+
for (const [key, value] of entries) {
|
|
644
|
+
// An own `__proto__` key would read as the prototype wherever a missing key is looked up.
|
|
645
|
+
const keyError = key === "__proto__" ? "is not a resource key" : text()(key);
|
|
646
|
+
if (keyError) return `${key.slice(0, 64)} ${keyError}`;
|
|
647
|
+
const error = checkResource(value);
|
|
648
|
+
if (error) return `${key} ${error}`;
|
|
649
|
+
}
|
|
650
|
+
return undefined;
|
|
651
|
+
};
|
|
652
|
+
|
|
382
653
|
const checkLevel: Check = shape(
|
|
383
654
|
{
|
|
384
655
|
level: int(0, 8),
|
package/src/v3/index.ts
CHANGED
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
export {
|
|
2
2
|
type AbstractNumberingJSON,
|
|
3
3
|
type BorderJSON,
|
|
4
|
+
type CellPropertiesJSON,
|
|
4
5
|
type CommentJSON,
|
|
5
6
|
createDocument,
|
|
6
7
|
type FoliDocument,
|
|
8
|
+
type FoliDocumentV3,
|
|
9
|
+
type MeasureJSON,
|
|
10
|
+
migrateToV4,
|
|
7
11
|
type NumberingInstanceJSON,
|
|
8
12
|
type NumberingLevelJSON,
|
|
9
13
|
type ParagraphPropertiesJSON,
|
|
10
14
|
parseContent,
|
|
11
15
|
parseDocument,
|
|
16
|
+
type ResourceJSON,
|
|
17
|
+
type RowPropertiesJSON,
|
|
12
18
|
SCHEMA_VERSION,
|
|
13
19
|
type SectionJSON,
|
|
14
20
|
type StyleJSON,
|
|
21
|
+
TABLE_CONDITIONS,
|
|
22
|
+
type TableCondition,
|
|
23
|
+
type TableConditionJSON,
|
|
24
|
+
type TablePropertiesJSON,
|
|
15
25
|
type ThemeJSON,
|
|
16
26
|
} from "./document.js";
|
|
17
27
|
export { migrateToV3 } from "./migrate.js";
|
|
18
28
|
export {
|
|
29
|
+
type CellFormat,
|
|
30
|
+
type CellPlace,
|
|
19
31
|
formatNumber,
|
|
20
32
|
type ListMarker,
|
|
21
33
|
type ParagraphFormat,
|
|
22
34
|
Resolver,
|
|
23
35
|
type RunFormat,
|
|
36
|
+
type TableFormat,
|
|
37
|
+
tableConditions,
|
|
24
38
|
} from "./resolve.js";
|
|
25
39
|
export {
|
|
26
40
|
checkSection,
|
package/src/v3/migrate.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { MarkJSON, NodeJSON } from "../model/index.js";
|
|
|
3
3
|
import type { BlockJSON, InlineJSON, MarkNameJSON } from "../schema.js";
|
|
4
4
|
import type {
|
|
5
5
|
AbstractNumberingJSON,
|
|
6
|
-
|
|
6
|
+
FoliDocumentV3,
|
|
7
7
|
NumberingInstanceJSON,
|
|
8
8
|
SectionJSON,
|
|
9
9
|
StyleJSON,
|
|
@@ -25,7 +25,7 @@ import { mmToTwips } from "./units.js";
|
|
|
25
25
|
* - `settings` becomes the section, in twips (rounded to the twip: under 0.02 mm).
|
|
26
26
|
* The styles and numbering definitions the result references are added.
|
|
27
27
|
*/
|
|
28
|
-
export function migrateToV3(doc: FoliDocumentV2):
|
|
28
|
+
export function migrateToV3(doc: FoliDocumentV2): FoliDocumentV3 {
|
|
29
29
|
const instances: NumberingInstanceJSON[] = [];
|
|
30
30
|
const content = blocks(doc.content.content, { instances });
|
|
31
31
|
return {
|