@file-viewer/renderer-spreadsheet 3.3.3 → 3.3.5
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/README.en.md +14 -0
- package/README.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/spreadsheet/autoRowHeight.d.ts +16 -0
- package/dist/spreadsheet/autoRowHeight.js +208 -0
- package/dist/spreadsheet/chartRenderer.js +203 -132
- package/dist/spreadsheet/comments.d.ts +40 -0
- package/dist/spreadsheet/comments.js +388 -0
- package/dist/spreadsheet/drawingAutoHeight.d.ts +7 -0
- package/dist/spreadsheet/drawingAutoHeight.js +51 -0
- package/dist/spreadsheet/sourceRegion.d.ts +37 -0
- package/dist/spreadsheet/sourceRegion.js +54 -0
- package/dist/spreadsheet/state.d.ts +7 -1
- package/dist/spreadsheet/state.js +6 -0
- package/dist/spreadsheet/view.d.ts +9 -4
- package/dist/spreadsheet/view.js +98 -27
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.d.ts +61 -2
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +137 -28
- package/dist/spreadsheet/worker/sheetjs/chartParser.d.ts +4 -3
- package/dist/spreadsheet/worker/sheetjs/chartParser.js +143 -29
- package/dist/spreadsheet/worker/sheetjs/commentParser.d.ts +7 -0
- package/dist/spreadsheet/worker/sheetjs/commentParser.js +232 -0
- package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.d.ts +4 -0
- package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.js +90 -0
- package/dist/spreadsheet/worker/sheetjs/parser.d.ts +40 -3
- package/dist/spreadsheet/worker/sheetjs/parser.js +452 -54
- package/dist/spreadsheet/worker/sheetjs/richDataParser.js +5 -2
- package/dist/spreadsheet/worker/sheetjs/rowSizing.d.ts +10 -0
- package/dist/spreadsheet/worker/sheetjs/rowSizing.js +156 -0
- package/dist/spreadsheet/worker/sheetjs/sheetLayout.d.ts +2 -0
- package/dist/spreadsheet/worker/sheetjs/sourceAnchor.d.ts +10 -0
- package/dist/spreadsheet/worker/sheetjs/sourceAnchor.js +286 -0
- package/dist/spreadsheet/worker/sheetjs/textEncoding.d.ts +1 -1
- package/dist/spreadsheet/worker/sheetjs/textEncoding.js +1 -1
- package/dist/spreadsheet/worker/sheetjs/workbookFont.d.ts +4 -0
- package/dist/spreadsheet/worker/sheetjs/workbookFont.js +15 -0
- package/dist/spreadsheet/worker/sheetjs/xmlBudget.d.ts +11 -0
- package/dist/spreadsheet/worker/sheetjs/xmlBudget.js +39 -0
- package/dist/spreadsheet/worker/type.d.ts +158 -0
- package/dist/spreadsheet.js +150 -39
- package/dist/worker/sheet.worker.js +43 -59
- package/package.json +23 -3
|
@@ -8,7 +8,10 @@ const METADATA_RELATIONSHIP_SUFFIX = '/sheetmetadata';
|
|
|
8
8
|
const RICH_VALUE_RELATIONSHIP_SUFFIX = '/rdrichvalue';
|
|
9
9
|
const RICH_VALUE_STRUCTURE_RELATIONSHIP_SUFFIX = '/rdrichvaluestructure';
|
|
10
10
|
const RICH_VALUE_RELATION_LIST_SUFFIX = '/richvaluerel';
|
|
11
|
-
const
|
|
11
|
+
const WPS_CELL_IMAGE_RELATIONSHIPS = new Set([
|
|
12
|
+
'http://www.wps.cn/officeDocument/2017/relationships/cellimage',
|
|
13
|
+
'http://www.wps.cn/officeDocument/2020/cellImage'
|
|
14
|
+
].map(type => type.toLowerCase()));
|
|
12
15
|
const RICH_VALUE_METADATA_NAME = 'XLRICHVALUE';
|
|
13
16
|
const LOCAL_IMAGE_STRUCTURE_TYPE = '_localImage';
|
|
14
17
|
const LOCAL_IMAGE_KEY_SUFFIX = 'LocalImageIdentifier';
|
|
@@ -267,7 +270,7 @@ export const parseSpreadsheetCellImages = async (data) => {
|
|
|
267
270
|
metadataRichValueIndexes = parseMetadataRichValueIndexes(metadataDocument);
|
|
268
271
|
richValueImages = await loadRichValueImages(zip, parseRichValueStructures(richValueStructureDocument), parseRichValues(richValueDocument), parseRichValueRelationshipIds(richValueRelationListDocument), richValueRelationships, contentTypes);
|
|
269
272
|
}
|
|
270
|
-
const wpsCellImageRelationship = workbookRelationships.find((relationship) => (relationship.type.toLowerCase()
|
|
273
|
+
const wpsCellImageRelationship = workbookRelationships.find((relationship) => (WPS_CELL_IMAGE_RELATIONSHIPS.has(relationship.type.toLowerCase())));
|
|
271
274
|
const wpsCellImages = await loadWpsCellImages(zip, wpsCellImageRelationship, contentTypes);
|
|
272
275
|
if (!richValueImages.size && !wpsCellImages.size) {
|
|
273
276
|
return result;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type JSZip from 'jszip';
|
|
2
|
+
export interface StoredRowSizing {
|
|
3
|
+
fixedRows: number[];
|
|
4
|
+
autoHeightDisabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare const createRowSizingScanner: () => {
|
|
7
|
+
push: (chunk: string) => void;
|
|
8
|
+
finish: () => StoredRowSizing;
|
|
9
|
+
};
|
|
10
|
+
export declare const readStoredRowSizing: (zip: JSZip, part: string) => Promise<StoredRowSizing>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const decodeAttribute = (value) => value.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (_, token) => {
|
|
2
|
+
if (token[0] !== '#')
|
|
3
|
+
return ({ amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }[token] || '');
|
|
4
|
+
const code = token[1].toLowerCase() === 'x'
|
|
5
|
+
? parseInt(token.slice(2), 16)
|
|
6
|
+
: parseInt(token.slice(1), 10);
|
|
7
|
+
return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : '';
|
|
8
|
+
});
|
|
9
|
+
export const createRowSizingScanner = () => {
|
|
10
|
+
let buffer = '', skipping = '', lastRow = 0;
|
|
11
|
+
const fixed = new Set();
|
|
12
|
+
const push = (chunk) => {
|
|
13
|
+
var _a;
|
|
14
|
+
buffer += chunk;
|
|
15
|
+
for (;;) {
|
|
16
|
+
if (skipping) {
|
|
17
|
+
const end = buffer.indexOf(skipping);
|
|
18
|
+
if (end < 0) {
|
|
19
|
+
buffer = buffer.slice(-skipping.length);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
buffer = buffer.slice(end + skipping.length);
|
|
23
|
+
skipping = '';
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const start = buffer.indexOf('<');
|
|
27
|
+
if (start < 0) {
|
|
28
|
+
buffer = '';
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
buffer = buffer.slice(start);
|
|
32
|
+
if (buffer.startsWith('<!--')) {
|
|
33
|
+
buffer = buffer.slice(4);
|
|
34
|
+
skipping = '-->';
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (buffer.startsWith('<![CDATA[')) {
|
|
38
|
+
buffer = buffer.slice(9);
|
|
39
|
+
skipping = ']]>';
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (/^<!DOCTYPE/i.test(buffer))
|
|
43
|
+
throw new Error('Row sizing does not expand XML document types');
|
|
44
|
+
let end = -1, quote = '';
|
|
45
|
+
for (let i = 1; i < buffer.length; i++) {
|
|
46
|
+
const c = buffer[i];
|
|
47
|
+
if (quote) {
|
|
48
|
+
if (c === quote)
|
|
49
|
+
quote = '';
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (c === '"' || c === "'")
|
|
53
|
+
quote = c;
|
|
54
|
+
else if (c === '>') {
|
|
55
|
+
end = i;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (end < 0) {
|
|
60
|
+
if (buffer.length > 65536)
|
|
61
|
+
throw new Error('Row sizing XML tag exceeds the preview limit');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const tag = buffer.slice(0, end + 1);
|
|
65
|
+
buffer = buffer.slice(end + 1);
|
|
66
|
+
const row = /^<(?:[\w.-]+:)?row\b/.exec(tag);
|
|
67
|
+
if (!row)
|
|
68
|
+
continue;
|
|
69
|
+
const attrs = {};
|
|
70
|
+
let tail = tag.slice(row[0].length);
|
|
71
|
+
while (tail.trim() && !/^\s*\/?>/.test(tail)) {
|
|
72
|
+
const attr = /^\s+([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(tail);
|
|
73
|
+
if (!attr)
|
|
74
|
+
throw new Error('Invalid row sizing attribute');
|
|
75
|
+
attrs[attr[1]] = decodeAttribute((_a = attr[2]) !== null && _a !== void 0 ? _a : attr[3]);
|
|
76
|
+
tail = tail.slice(attr[0].length);
|
|
77
|
+
}
|
|
78
|
+
const index = attrs.r === undefined ? lastRow + 1 : Number(attrs.r);
|
|
79
|
+
if (!Number.isSafeInteger(index) || index < 1 || index > 1048576)
|
|
80
|
+
throw new Error('Invalid source row index');
|
|
81
|
+
lastRow = index;
|
|
82
|
+
const hasHeight = Object.prototype.hasOwnProperty.call(attrs, 'ht');
|
|
83
|
+
const validHeight = hasHeight &&
|
|
84
|
+
attrs.ht.trim() !== '' &&
|
|
85
|
+
Number.isFinite(Number(attrs.ht)) &&
|
|
86
|
+
Number(attrs.ht) >= 0;
|
|
87
|
+
const custom = attrs.customHeight !== undefined &&
|
|
88
|
+
!/^(?:0|false)$/i.test(attrs.customHeight);
|
|
89
|
+
if ((custom && !validHeight) || (hasHeight && !validHeight))
|
|
90
|
+
fixed.add(index - 1);
|
|
91
|
+
if (fixed.size > 100000)
|
|
92
|
+
throw new Error('Row sizing hints exceed the preview limit');
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
push,
|
|
97
|
+
finish: () => {
|
|
98
|
+
if (skipping || buffer.trim())
|
|
99
|
+
throw new Error('Incomplete row sizing XML');
|
|
100
|
+
return { fixedRows: [...fixed] };
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
export const readStoredRowSizing = async (zip, part) => {
|
|
105
|
+
const file = zip.file(part);
|
|
106
|
+
if (!file)
|
|
107
|
+
return { fixedRows: [], autoHeightDisabled: true };
|
|
108
|
+
try {
|
|
109
|
+
return await new Promise((resolve, reject) => {
|
|
110
|
+
const scanner = createRowSizingScanner(), decoder = new TextDecoder();
|
|
111
|
+
const stream = file.internalStream('uint8array');
|
|
112
|
+
let finished = false, bytes = 0;
|
|
113
|
+
const fail = (error) => {
|
|
114
|
+
if (finished)
|
|
115
|
+
return;
|
|
116
|
+
finished = true;
|
|
117
|
+
stream.pause();
|
|
118
|
+
reject(error);
|
|
119
|
+
};
|
|
120
|
+
stream.on('data', (chunk) => {
|
|
121
|
+
if (finished)
|
|
122
|
+
return;
|
|
123
|
+
bytes += chunk.byteLength;
|
|
124
|
+
if (bytes > 128 * 1024 * 1024) {
|
|
125
|
+
fail(new Error('Row sizing XML exceeds the preview limit'));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
scanner.push(decoder.decode(chunk, { stream: true }));
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
fail(error);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
stream.on('error', fail);
|
|
136
|
+
stream.on('end', () => {
|
|
137
|
+
if (finished)
|
|
138
|
+
return;
|
|
139
|
+
try {
|
|
140
|
+
scanner.push(decoder.decode());
|
|
141
|
+
const result = scanner.finish();
|
|
142
|
+
finished = true;
|
|
143
|
+
resolve(result);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
fail(error);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
stream.resume();
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
console.warn('[file-viewer] Automatic row sizing disabled: source row metadata could not be verified.', error);
|
|
154
|
+
return { fixedRows: [], autoHeightDisabled: true };
|
|
155
|
+
}
|
|
156
|
+
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type JSZip from 'jszip';
|
|
2
2
|
import type { WorkBook } from 'styled-exceljs';
|
|
3
|
+
import type { StoredRowSizing } from './rowSizing.js';
|
|
3
4
|
export interface StoredSheetLayout {
|
|
4
5
|
columnWidth?: number;
|
|
5
6
|
rowHeightPt?: number;
|
|
7
|
+
rowSizing?: StoredRowSizing;
|
|
6
8
|
maximumDigitWidth?: number;
|
|
7
9
|
}
|
|
8
10
|
export declare const readWorkbookDigitWidth: (workbook?: WorkBook | null) => number;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DrawingImage } from 'styled-exceljs';
|
|
2
|
+
import type { SheetSourceAnchor, SheetImageSourceSummary } from '../type.js';
|
|
3
|
+
export declare const validSourceReference: (anchor: any, reference: any, kind: "image" | "chart") => boolean;
|
|
4
|
+
export declare const readSourceAnchor: (element: any, reference?: any, kind?: "image" | "chart") => SheetSourceAnchor;
|
|
5
|
+
export declare const unknownSourceAnchor: (reason: string) => SheetSourceAnchor;
|
|
6
|
+
export declare const readImageSourceProvenance: (raw: unknown, images: DrawingImage[], drawingPresent?: boolean) => {
|
|
7
|
+
anchors: Map<DrawingImage, SheetSourceAnchor>;
|
|
8
|
+
summary: SheetImageSourceSummary;
|
|
9
|
+
};
|
|
10
|
+
export declare const readImageSourceAnchors: (raw: unknown, images: DrawingImage[]) => Map<DrawingImage, SheetSourceAnchor>;
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { DOMParser } from '@xmldom/xmldom';
|
|
2
|
+
const local = (node) => { var _a; return (node === null || node === void 0 ? void 0 : node.localName) || ((_a = node === null || node === void 0 ? void 0 : node.nodeName) === null || _a === void 0 ? void 0 : _a.split(':').pop()) || ''; };
|
|
3
|
+
const child = (node, name) => {
|
|
4
|
+
for (let current = node === null || node === void 0 ? void 0 : node.firstChild; current; current = current.nextSibling)
|
|
5
|
+
if (current.nodeType === 1 && local(current) === name)
|
|
6
|
+
return current;
|
|
7
|
+
return undefined;
|
|
8
|
+
};
|
|
9
|
+
const XDR = 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing';
|
|
10
|
+
const MAIN = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
|
11
|
+
const CHART = 'http://schemas.openxmlformats.org/drawingml/2006/chart';
|
|
12
|
+
const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
13
|
+
const isElement = (node, name, namespace) => (node === null || node === void 0 ? void 0 : node.nodeType) === 1 && local(node) === name && node.namespaceURI === namespace;
|
|
14
|
+
const soleChild = (parent, node, name, namespace) => {
|
|
15
|
+
let count = 0;
|
|
16
|
+
for (let current = parent === null || parent === void 0 ? void 0 : parent.firstChild; current; current = current.nextSibling)
|
|
17
|
+
if (current.nodeType === 1 && local(current) === name)
|
|
18
|
+
count++;
|
|
19
|
+
return count === 1 && (node === null || node === void 0 ? void 0 : node.parentNode) === parent && isElement(node, name, namespace);
|
|
20
|
+
};
|
|
21
|
+
const plainAttribute = (element, name) => {
|
|
22
|
+
var _a;
|
|
23
|
+
let count = 0, value;
|
|
24
|
+
for (let index = 0; index < (((_a = element === null || element === void 0 ? void 0 : element.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0); index++) {
|
|
25
|
+
const attribute = element.attributes.item(index);
|
|
26
|
+
if (local(attribute) !== name)
|
|
27
|
+
continue;
|
|
28
|
+
count++;
|
|
29
|
+
if (!attribute.namespaceURI)
|
|
30
|
+
value = attribute.value;
|
|
31
|
+
}
|
|
32
|
+
return { value, valid: count === 0 || (count === 1 && value !== undefined) };
|
|
33
|
+
};
|
|
34
|
+
const validMarkerSource = (anchor, name) => {
|
|
35
|
+
const point = child(anchor, name);
|
|
36
|
+
if (!point)
|
|
37
|
+
return true;
|
|
38
|
+
if (!soleChild(anchor, point, name, XDR))
|
|
39
|
+
return false;
|
|
40
|
+
return ['row', 'col', 'rowOff', 'colOff'].every(key => {
|
|
41
|
+
const field = child(point, key);
|
|
42
|
+
if (!soleChild(point, field, key, XDR))
|
|
43
|
+
return false;
|
|
44
|
+
for (let current = field.firstChild; current; current = current.nextSibling)
|
|
45
|
+
if (current.nodeType === 1)
|
|
46
|
+
return false;
|
|
47
|
+
return true;
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
export const validSourceReference = (anchor, reference, kind) => {
|
|
51
|
+
var _a;
|
|
52
|
+
const path = kind === 'image'
|
|
53
|
+
? [['blip', MAIN], ['blipFill', XDR], ['pic', XDR]]
|
|
54
|
+
: [['chart', CHART], ['graphicData', MAIN], ['graphic', MAIN], ['graphicFrame', XDR]];
|
|
55
|
+
let current = reference;
|
|
56
|
+
for (const [name, namespace] of path) {
|
|
57
|
+
if (!soleChild(current === null || current === void 0 ? void 0 : current.parentNode, current, name, namespace))
|
|
58
|
+
return false;
|
|
59
|
+
if (name === 'graphicData') {
|
|
60
|
+
const uri = plainAttribute(current, 'uri');
|
|
61
|
+
if (!uri.valid || uri.value !== CHART)
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
current = current.parentNode;
|
|
65
|
+
}
|
|
66
|
+
if (current !== anchor)
|
|
67
|
+
return false;
|
|
68
|
+
const name = kind === 'image' ? 'embed' : 'id';
|
|
69
|
+
let matches = 0;
|
|
70
|
+
for (let index = 0; index < (((_a = reference === null || reference === void 0 ? void 0 : reference.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0); index++) {
|
|
71
|
+
const attribute = reference.attributes.item(index);
|
|
72
|
+
if (local(attribute) !== name)
|
|
73
|
+
continue;
|
|
74
|
+
if (attribute.namespaceURI !== REL || !attribute.value)
|
|
75
|
+
return false;
|
|
76
|
+
matches++;
|
|
77
|
+
}
|
|
78
|
+
return matches === 1;
|
|
79
|
+
};
|
|
80
|
+
const marker = (node) => {
|
|
81
|
+
if (!node)
|
|
82
|
+
return undefined;
|
|
83
|
+
const raw = Object.fromEntries(['row', 'col', 'rowOff', 'colOff'].map(key => { var _a, _b; return [key, (_b = (_a = child(node, key)) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : null]; }));
|
|
84
|
+
const value = Object.fromEntries(Object.entries(raw).map(([key, text]) => [key, text === null || !text.trim() ? NaN : Number(text)]));
|
|
85
|
+
return { raw, value };
|
|
86
|
+
};
|
|
87
|
+
const validMarker = (point) => !!point && Number.isSafeInteger(point.row) && Number.isSafeInteger(point.col)
|
|
88
|
+
&& point.row >= 0 && point.row < 1048576 && point.col >= 0 && point.col < 16384
|
|
89
|
+
&& Number.isFinite(point.rowOff) && Number.isFinite(point.colOff);
|
|
90
|
+
export const readSourceAnchor = (element, reference, kind) => {
|
|
91
|
+
var _a, _b, _c;
|
|
92
|
+
const type = local(element), edit = plainAttribute(element, 'editAs'), editAs = edit.value;
|
|
93
|
+
const from = marker(child(element, 'from')), to = marker(child(element, 'to'));
|
|
94
|
+
const source = { status: 'valid', type, editAs, fromPresent: !!from, toPresent: !!to,
|
|
95
|
+
from: from === null || from === void 0 ? void 0 : from.value, to: to === null || to === void 0 ? void 0 : to.value, fromRaw: from === null || from === void 0 ? void 0 : from.raw, toRaw: to === null || to === void 0 ? void 0 : to.raw };
|
|
96
|
+
const ordered = validMarker(from === null || from === void 0 ? void 0 : from.value) && validMarker(to === null || to === void 0 ? void 0 : to.value)
|
|
97
|
+
&& to.value.row >= from.value.row && to.value.col >= from.value.col;
|
|
98
|
+
const root = element === null || element === void 0 ? void 0 : element.parentNode;
|
|
99
|
+
if (!isElement(root, 'wsDr', XDR) || root !== ((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.documentElement) || element.namespaceURI !== XDR)
|
|
100
|
+
source.reason = 'unknown-anchor-context';
|
|
101
|
+
else if (!validMarkerSource(element, 'from') || !validMarkerSource(element, 'to'))
|
|
102
|
+
source.reason = 'unknown-marker-source';
|
|
103
|
+
else if (!edit.valid)
|
|
104
|
+
source.reason = 'unknown-edit-as-attribute';
|
|
105
|
+
else if (kind && !validSourceReference(element, reference, kind))
|
|
106
|
+
source.reason = 'unknown-reference-source';
|
|
107
|
+
else if (!['oneCellAnchor', 'twoCellAnchor', 'absoluteAnchor'].includes(type))
|
|
108
|
+
source.reason = 'unknown-anchor-mode';
|
|
109
|
+
else if (editAs !== undefined && !['absolute', 'oneCell', 'twoCell'].includes(editAs))
|
|
110
|
+
source.reason = 'unknown-edit-as';
|
|
111
|
+
else if (type !== 'absoluteAnchor' && (!validMarker(from === null || from === void 0 ? void 0 : from.value) || (type === 'twoCellAnchor' && !ordered) || (to && !ordered)))
|
|
112
|
+
source.reason = 'invalid-anchor-endpoints';
|
|
113
|
+
const truncated = [];
|
|
114
|
+
for (const key of ['type', 'editAs'])
|
|
115
|
+
if ((((_b = source[key]) === null || _b === void 0 ? void 0 : _b.length) || 0) > 128) {
|
|
116
|
+
source[key] = source[key].slice(0, 128);
|
|
117
|
+
truncated.push(key);
|
|
118
|
+
}
|
|
119
|
+
for (const side of ['fromRaw', 'toRaw'])
|
|
120
|
+
for (const key of ['row', 'col', 'rowOff', 'colOff']) {
|
|
121
|
+
const value = (_c = source[side]) === null || _c === void 0 ? void 0 : _c[key];
|
|
122
|
+
if (value && value.length > 64) {
|
|
123
|
+
source[side][key] = value.slice(0, 64);
|
|
124
|
+
truncated.push(side + '.' + key);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (truncated.length) {
|
|
128
|
+
source.truncatedFields = truncated;
|
|
129
|
+
source.reason = 'source-field-budget';
|
|
130
|
+
}
|
|
131
|
+
if (source.reason)
|
|
132
|
+
source.status = 'unknown';
|
|
133
|
+
return source;
|
|
134
|
+
};
|
|
135
|
+
export const unknownSourceAnchor = (reason) => ({ status: 'unknown', reason });
|
|
136
|
+
export const readImageSourceProvenance = (raw, images, drawingPresent = true) => {
|
|
137
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
138
|
+
const result = new Map();
|
|
139
|
+
const summary = { scope: 'raw-drawing-image-references', status: 'complete',
|
|
140
|
+
observedReferences: 0, representedReferences: 0, unrepresentedReferences: 0,
|
|
141
|
+
countsComplete: true, reasons: [], reasonsTruncated: false };
|
|
142
|
+
const incomplete = (reason, countsComplete = true) => {
|
|
143
|
+
summary.status = 'partial';
|
|
144
|
+
summary.countsComplete && (summary.countsComplete = countsComplete);
|
|
145
|
+
if (!summary.reasons.includes(reason)) {
|
|
146
|
+
if (summary.reasons.length < 8)
|
|
147
|
+
summary.reasons.push(reason);
|
|
148
|
+
else
|
|
149
|
+
summary.reasonsTruncated = true;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
let matchingComplete = false;
|
|
153
|
+
const finish = () => {
|
|
154
|
+
summary.unrepresentedReferences = matchingComplete
|
|
155
|
+
? summary.observedReferences - summary.representedReferences : 0;
|
|
156
|
+
if (summary.unrepresentedReferences)
|
|
157
|
+
incomplete('unrepresented-raw-image-source');
|
|
158
|
+
return { anchors: result, summary };
|
|
159
|
+
};
|
|
160
|
+
if (!drawingPresent && raw == null && images.length === 0) {
|
|
161
|
+
summary.status = 'absent';
|
|
162
|
+
return finish();
|
|
163
|
+
}
|
|
164
|
+
const fail = (reason) => {
|
|
165
|
+
incomplete(reason, false);
|
|
166
|
+
for (let index = 0; index < Math.min(images.length, 4096); index++)
|
|
167
|
+
result.set(images[index], unknownSourceAnchor(reason));
|
|
168
|
+
return finish();
|
|
169
|
+
};
|
|
170
|
+
if (images.length > 4096)
|
|
171
|
+
return fail('image-source-budget');
|
|
172
|
+
if (typeof raw !== 'string')
|
|
173
|
+
return fail('missing-raw-source');
|
|
174
|
+
if (raw.length > 8 * 1024 * 1024)
|
|
175
|
+
return fail('raw-source-budget');
|
|
176
|
+
let sourceBytes = 0;
|
|
177
|
+
for (let index = 0; index < raw.length; index++) {
|
|
178
|
+
const code = raw.charCodeAt(index);
|
|
179
|
+
if (code < 0x80)
|
|
180
|
+
sourceBytes++;
|
|
181
|
+
else if (code < 0x800)
|
|
182
|
+
sourceBytes += 2;
|
|
183
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < raw.length && raw.charCodeAt(index + 1) >= 0xdc00 && raw.charCodeAt(index + 1) <= 0xdfff) {
|
|
184
|
+
sourceBytes += 4;
|
|
185
|
+
index++;
|
|
186
|
+
}
|
|
187
|
+
else
|
|
188
|
+
sourceBytes += 3;
|
|
189
|
+
if (sourceBytes > 8 * 1024 * 1024)
|
|
190
|
+
return fail('raw-source-byte-budget');
|
|
191
|
+
}
|
|
192
|
+
if (/<!DOCTYPE|<!ENTITY/i.test(raw))
|
|
193
|
+
return fail('unsupported-xml-declaration');
|
|
194
|
+
let markup = 0;
|
|
195
|
+
for (let offset = raw.indexOf('<'); offset !== -1; offset = raw.indexOf('<', offset + 1))
|
|
196
|
+
if (++markup > 65536)
|
|
197
|
+
return fail('raw-markup-budget');
|
|
198
|
+
let document, malformed = false;
|
|
199
|
+
try {
|
|
200
|
+
document = new DOMParser({ onError: () => { malformed = true; } }).parseFromString(raw, 'application/xml');
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return fail('unreadable-raw-source');
|
|
204
|
+
}
|
|
205
|
+
if (malformed || !(document === null || document === void 0 ? void 0 : document.documentElement))
|
|
206
|
+
return fail('unreadable-raw-source');
|
|
207
|
+
if (!isElement(document.documentElement, 'wsDr', XDR))
|
|
208
|
+
return fail('unknown-drawing-root');
|
|
209
|
+
const markerKey = (point) => point ? ['row', 'col', 'rowOff', 'colOff'].map(key => typeof point[key] + ':' + String(point[key])).join(',') : 'absent';
|
|
210
|
+
const keyFor = (id, type, from, to) => JSON.stringify([id, type, markerKey(from), markerKey(to)]);
|
|
211
|
+
const candidates = new Map();
|
|
212
|
+
let anchors = 0, nodes = 0, entries = 0;
|
|
213
|
+
for (let anchor = document.documentElement.firstChild; anchor; anchor = anchor.nextSibling) {
|
|
214
|
+
if (anchor.nodeType !== 1)
|
|
215
|
+
continue;
|
|
216
|
+
if (++anchors > 8192)
|
|
217
|
+
return fail('source-anchor-budget');
|
|
218
|
+
const source = readSourceAnchor(anchor);
|
|
219
|
+
if (source.status !== 'valid')
|
|
220
|
+
incomplete('unknown-anchor-source', false);
|
|
221
|
+
let current = anchor.firstChild;
|
|
222
|
+
while (current) {
|
|
223
|
+
if (++nodes > 65536)
|
|
224
|
+
return fail('source-node-budget');
|
|
225
|
+
if (current.nodeType === 1 && ['pic', 'blipFill'].includes(local(current))) {
|
|
226
|
+
if (!isElement(current, local(current), XDR) || (local(current) === 'blipFill' && !child(current, 'blip'))
|
|
227
|
+
|| (local(current) === 'pic' && !child(current, 'blipFill')))
|
|
228
|
+
incomplete('unclassified-image-container', false);
|
|
229
|
+
}
|
|
230
|
+
if (current.nodeType === 1 && local(current) === 'blip') {
|
|
231
|
+
if (entries >= 8192)
|
|
232
|
+
return fail('source-reference-budget');
|
|
233
|
+
entries++;
|
|
234
|
+
summary.observedReferences++;
|
|
235
|
+
const trusted = validSourceReference(anchor, current, 'image');
|
|
236
|
+
if (!trusted)
|
|
237
|
+
incomplete('unknown-image-reference', false);
|
|
238
|
+
const id = current.getAttributeNS(REL, 'embed');
|
|
239
|
+
if (!id)
|
|
240
|
+
incomplete('missing-image-relationship', false);
|
|
241
|
+
if (id) {
|
|
242
|
+
if (id.length > 256)
|
|
243
|
+
return fail('source-relationship-field-budget');
|
|
244
|
+
const key = keyFor(id, source.type, source.from, source.to), prior = candidates.get(key);
|
|
245
|
+
candidates.set(key, { source: trusted ? source : { ...source, status: 'unknown', reason: 'unknown-reference-source' }, count: ((prior === null || prior === void 0 ? void 0 : prior.count) || 0) + 1 });
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (current.firstChild)
|
|
249
|
+
current = current.firstChild;
|
|
250
|
+
else {
|
|
251
|
+
while (current && current !== anchor && !current.nextSibling)
|
|
252
|
+
current = current.parentNode;
|
|
253
|
+
current = !current || current === anchor ? null : current.nextSibling;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const vendorCounts = new Map();
|
|
258
|
+
for (const image of images) {
|
|
259
|
+
const key = keyFor(image.id, (_a = image.anchor) === null || _a === void 0 ? void 0 : _a.type, (_b = image.anchor) === null || _b === void 0 ? void 0 : _b.from, (_c = image.anchor) === null || _c === void 0 ? void 0 : _c.to);
|
|
260
|
+
vendorCounts.set(key, (vendorCounts.get(key) || 0) + 1);
|
|
261
|
+
}
|
|
262
|
+
for (let index = 0; index < images.length; index++) {
|
|
263
|
+
const image = images[index], key = keyFor(image.id, (_d = image.anchor) === null || _d === void 0 ? void 0 : _d.type, (_e = image.anchor) === null || _e === void 0 ? void 0 : _e.from, (_f = image.anchor) === null || _f === void 0 ? void 0 : _f.to), match = candidates.get(keyFor(image.id, (_g = image.anchor) === null || _g === void 0 ? void 0 : _g.type, (_h = image.anchor) === null || _h === void 0 ? void 0 : _h.from, (_j = image.anchor) === null || _j === void 0 ? void 0 : _j.to));
|
|
264
|
+
const relationship = image.rel;
|
|
265
|
+
if (((_k = relationship === null || relationship === void 0 ? void 0 : relationship.TargetMode) === null || _k === void 0 ? void 0 : _k.toLowerCase()) === 'external') {
|
|
266
|
+
incomplete('external-image-relationship');
|
|
267
|
+
result.set(image, unknownSourceAnchor('external-relationship'));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (relationship && !relationship.Target)
|
|
271
|
+
incomplete('unresolved-image-relationship');
|
|
272
|
+
if ((match === null || match === void 0 ? void 0 : match.count) === 1 && vendorCounts.get(key) === 1)
|
|
273
|
+
summary.representedReferences++;
|
|
274
|
+
else
|
|
275
|
+
incomplete(match ? 'ambiguous-image-source' : 'unmatched-vendor-image');
|
|
276
|
+
result.set(image, (match === null || match === void 0 ? void 0 : match.count) === 1
|
|
277
|
+
? vendorCounts.get(key) === 1 ? match.source : unknownSourceAnchor('ambiguous-vendor-match')
|
|
278
|
+
: unknownSourceAnchor(match ? 'ambiguous-source-match' : 'unmatched-source-anchor'));
|
|
279
|
+
}
|
|
280
|
+
for (const candidate of candidates.values())
|
|
281
|
+
if (candidate.count > 1)
|
|
282
|
+
incomplete('ambiguous-image-source');
|
|
283
|
+
matchingComplete = true;
|
|
284
|
+
return finish();
|
|
285
|
+
};
|
|
286
|
+
export const readImageSourceAnchors = (raw, images) => readImageSourceProvenance(raw, images).anchors;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ResolvedFileViewerTextEncoding } from '@file-viewer/core';
|
|
1
|
+
import { type ResolvedFileViewerTextEncoding } from '@file-viewer/core/headless';
|
|
2
2
|
export type SpreadsheetTextEncoding = 'auto' | 'utf-8' | 'gbk' | 'gb18030';
|
|
3
3
|
export interface SpreadsheetTextSource {
|
|
4
4
|
fileType?: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { decodeFileViewerTextBuffer, isValidFileViewerUtf8, resolveFileViewerTextEncoding, } from '@file-viewer/core';
|
|
1
|
+
import { decodeFileViewerTextBuffer, isValidFileViewerUtf8, resolveFileViewerTextEncoding, } from '@file-viewer/core/headless';
|
|
2
2
|
const TEXT_SPREADSHEET_EXTENSIONS = new Set(['csv', 'tsv']);
|
|
3
3
|
const TEXT_SPREADSHEET_MIME_TYPES = new Set([
|
|
4
4
|
'text/csv',
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const readWorkbookDefaultFont = (workbook) => {
|
|
2
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
3
|
+
const styles = workbook === null || workbook === void 0 ? void 0 : workbook.Styles;
|
|
4
|
+
if (!styles)
|
|
5
|
+
return undefined;
|
|
6
|
+
const format = (_a = styles.CellXf) === null || _a === void 0 ? void 0 : _a[0];
|
|
7
|
+
const normal = (_c = (_b = styles.CellStyles) === null || _b === void 0 ? void 0 : _b.find(style => style.builtinId === 0)) !== null && _c !== void 0 ? _c : (_d = styles.CellStyles) === null || _d === void 0 ? void 0 : _d.find(style => { var _a; return ((_a = style.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'normal'; });
|
|
8
|
+
const parentId = (_f = (_e = format === null || format === void 0 ? void 0 : format.xfId) !== null && _e !== void 0 ? _e : normal === null || normal === void 0 ? void 0 : normal.xfId) !== null && _f !== void 0 ? _f : 0;
|
|
9
|
+
const parent = (_g = styles.CellStyleXf) === null || _g === void 0 ? void 0 : _g[parentId];
|
|
10
|
+
const fontId = (_j = (_h = format === null || format === void 0 ? void 0 : format.fontId) !== null && _h !== void 0 ? _h : parent === null || parent === void 0 ? void 0 : parent.fontId) !== null && _j !== void 0 ? _j : 0;
|
|
11
|
+
if (!Number.isSafeInteger(fontId) || fontId < 0)
|
|
12
|
+
return undefined;
|
|
13
|
+
const font = (_k = styles.Fonts) === null || _k === void 0 ? void 0 : _k[fontId];
|
|
14
|
+
return font ? { ...font } : undefined;
|
|
15
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type JSZip from 'jszip';
|
|
2
|
+
declare module 'jszip' {
|
|
3
|
+
interface JSZipObject {
|
|
4
|
+
internalStream(type: 'uint8array'): JSZip.JSZipStreamHelper<Uint8Array>;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export interface XmlReadBudget {
|
|
8
|
+
maxPartBytes: number;
|
|
9
|
+
remainingBytes: number;
|
|
10
|
+
}
|
|
11
|
+
export declare const readBoundedZipText: (file: JSZip.JSZipObject, budget: XmlReadBudget) => Promise<string>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export const readBoundedZipText = (file, budget) => {
|
|
2
|
+
if (budget.remainingBytes <= 0)
|
|
3
|
+
return Promise.reject(new Error('Workbook XML exceeds the preview limit'));
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const stream = file.internalStream('uint8array');
|
|
6
|
+
const decoder = new TextDecoder();
|
|
7
|
+
const text = [];
|
|
8
|
+
let bytes = 0;
|
|
9
|
+
let finished = false;
|
|
10
|
+
stream.on('data', (chunk) => {
|
|
11
|
+
if (finished)
|
|
12
|
+
return;
|
|
13
|
+
bytes += chunk.byteLength;
|
|
14
|
+
budget.remainingBytes -= chunk.byteLength;
|
|
15
|
+
if (bytes > budget.maxPartBytes || budget.remainingBytes < 0) {
|
|
16
|
+
finished = true;
|
|
17
|
+
stream.pause();
|
|
18
|
+
text.length = 0;
|
|
19
|
+
reject(new Error('Workbook XML exceeds the preview limit'));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
text.push(decoder.decode(chunk, { stream: true }));
|
|
23
|
+
});
|
|
24
|
+
stream.on('error', (error) => {
|
|
25
|
+
if (finished)
|
|
26
|
+
return;
|
|
27
|
+
finished = true;
|
|
28
|
+
text.length = 0;
|
|
29
|
+
reject(error);
|
|
30
|
+
});
|
|
31
|
+
stream.on('end', () => {
|
|
32
|
+
if (finished)
|
|
33
|
+
return;
|
|
34
|
+
finished = true;
|
|
35
|
+
resolve(text.join('') + decoder.decode());
|
|
36
|
+
});
|
|
37
|
+
stream.resume();
|
|
38
|
+
});
|
|
39
|
+
};
|