@beyondwork/docx-react-component 1.0.1 → 1.0.3
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.md +44 -104
- package/package.json +50 -30
- package/src/README.md +85 -0
- package/src/api/README.md +22 -0
- package/src/api/public-types.ts +525 -0
- package/src/compare/diff-engine.ts +530 -0
- package/src/compare/export-redlines.ts +162 -0
- package/src/compare/snapshot.ts +37 -0
- package/src/component-inventory.md +99 -0
- package/src/core/README.md +10 -0
- package/src/core/commands/README.md +3 -0
- package/src/core/commands/formatting-commands.ts +161 -0
- package/src/core/commands/image-commands.ts +144 -0
- package/src/core/commands/index.ts +1013 -0
- package/src/core/commands/list-commands.ts +370 -0
- package/src/core/commands/review-commands.ts +108 -0
- package/src/core/commands/text-commands.ts +119 -0
- package/src/core/schema/README.md +3 -0
- package/src/core/schema/text-schema.ts +512 -0
- package/src/core/selection/README.md +3 -0
- package/src/core/selection/mapping.ts +238 -0
- package/src/core/selection/review-anchors.ts +94 -0
- package/src/core/state/README.md +3 -0
- package/src/core/state/editor-state.ts +580 -0
- package/src/core/state/text-transaction.ts +276 -0
- package/src/formats/xlsx/io/parse-shared-strings.ts +41 -0
- package/src/formats/xlsx/io/parse-sheet.ts +289 -0
- package/src/formats/xlsx/io/parse-styles.ts +57 -0
- package/src/formats/xlsx/io/parse-workbook.ts +75 -0
- package/src/formats/xlsx/io/xlsx-session.ts +306 -0
- package/src/formats/xlsx/model/cell.ts +189 -0
- package/src/formats/xlsx/model/sheet.ts +244 -0
- package/src/formats/xlsx/model/styles.ts +118 -0
- package/src/formats/xlsx/model/workbook.ts +449 -0
- package/src/index.ts +45 -0
- package/src/io/README.md +10 -0
- package/src/io/docx-session.ts +1763 -0
- package/src/io/export/README.md +3 -0
- package/src/io/export/export-session.ts +165 -0
- package/src/io/export/minimal-docx.ts +115 -0
- package/src/io/export/reattach-preserved-parts.ts +54 -0
- package/src/io/export/serialize-comments.ts +876 -0
- package/src/io/export/serialize-footnotes.ts +217 -0
- package/src/io/export/serialize-headers-footers.ts +200 -0
- package/src/io/export/serialize-main-document.ts +982 -0
- package/src/io/export/serialize-numbering.ts +97 -0
- package/src/io/export/serialize-revisions.ts +389 -0
- package/src/io/export/serialize-runtime-revisions.ts +265 -0
- package/src/io/export/serialize-tables.ts +147 -0
- package/src/io/export/split-review-boundaries.ts +194 -0
- package/src/io/normalize/README.md +3 -0
- package/src/io/normalize/normalize-text.ts +437 -0
- package/src/io/ooxml/README.md +3 -0
- package/src/io/ooxml/parse-comments.ts +779 -0
- package/src/io/ooxml/parse-complex-content.ts +287 -0
- package/src/io/ooxml/parse-fields.ts +438 -0
- package/src/io/ooxml/parse-footnotes.ts +403 -0
- package/src/io/ooxml/parse-headers-footers.ts +483 -0
- package/src/io/ooxml/parse-inline-media.ts +431 -0
- package/src/io/ooxml/parse-main-document.ts +1846 -0
- package/src/io/ooxml/parse-numbering.ts +425 -0
- package/src/io/ooxml/parse-revisions.ts +658 -0
- package/src/io/ooxml/parse-shapes.ts +271 -0
- package/src/io/ooxml/parse-tables.ts +568 -0
- package/src/io/ooxml/parse-theme.ts +314 -0
- package/src/io/ooxml/part-manifest.ts +136 -0
- package/src/io/ooxml/revision-boundaries.ts +351 -0
- package/src/io/opc/README.md +3 -0
- package/src/io/opc/corrupt-package.ts +166 -0
- package/src/io/opc/docx-package.ts +74 -0
- package/src/io/opc/package-reader.ts +325 -0
- package/src/io/opc/package-writer.ts +273 -0
- package/src/legal/bookmarks.ts +196 -0
- package/src/legal/cross-references.ts +356 -0
- package/src/legal/defined-terms.ts +203 -0
- package/src/model/README.md +3 -0
- package/src/model/canonical-document.ts +1911 -0
- package/src/model/cds-1.0.0.ts +196 -0
- package/src/model/snapshot.ts +393 -0
- package/src/preservation/README.md +3 -0
- package/src/preservation/markup-compatibility.ts +48 -0
- package/src/preservation/opaque-fragment-store.ts +89 -0
- package/src/preservation/opaque-region.ts +233 -0
- package/src/preservation/package-preservation.ts +120 -0
- package/src/preservation/preserved-part-manifest.ts +56 -0
- package/src/preservation/relationship-retention.ts +57 -0
- package/src/preservation/store.ts +185 -0
- package/src/review/README.md +16 -0
- package/src/review/store/README.md +3 -0
- package/src/review/store/comment-anchors.ts +70 -0
- package/src/review/store/comment-remapping.ts +154 -0
- package/src/review/store/comment-store.ts +331 -0
- package/src/review/store/comment-thread.ts +109 -0
- package/src/review/store/revision-actions.ts +394 -0
- package/src/review/store/revision-store.ts +303 -0
- package/src/review/store/revision-types.ts +168 -0
- package/src/review/store/runtime-comment-store.ts +43 -0
- package/src/runtime/README.md +3 -0
- package/src/runtime/ai-action-policy.ts +764 -0
- package/src/runtime/document-runtime.ts +967 -0
- package/src/runtime/read-only-diagnostics-runtime.ts +232 -0
- package/src/runtime/review-runtime.ts +44 -0
- package/src/runtime/revision-runtime.ts +107 -0
- package/src/runtime/session-capabilities.ts +138 -0
- package/src/runtime/surface-projection.ts +570 -0
- package/src/runtime/table-commands.ts +87 -0
- package/src/runtime/table-schema.ts +140 -0
- package/src/runtime/virtualized-rendering.ts +258 -0
- package/src/ui/README.md +30 -0
- package/src/ui/WordReviewEditor.tsx +1506 -0
- package/src/ui/comments/README.md +3 -0
- package/src/ui/compatibility/README.md +3 -0
- package/src/ui/editor-surface/README.md +3 -0
- package/src/ui/headless/comment-decoration-model.ts +124 -0
- package/src/ui/headless/revision-decoration-model.ts +128 -0
- package/src/ui/headless/selection-helpers.ts +34 -0
- package/src/ui/headless/use-editor-keyboard.ts +98 -0
- package/src/ui/review/README.md +3 -0
- package/src/ui/shared/revision-filters.ts +31 -0
- package/src/ui/status/README.md +3 -0
- package/src/ui/theme/README.md +3 -0
- package/src/ui/toolbar/README.md +3 -0
- package/src/ui-tailwind/chrome/tw-alert-banner.tsx +48 -0
- package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +44 -0
- package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +58 -0
- package/src/ui-tailwind/chrome/use-before-unload.ts +20 -0
- package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +139 -0
- package/src/ui-tailwind/editor-surface/pm-decorations.ts +98 -0
- package/src/ui-tailwind/editor-surface/pm-position-map.ts +123 -0
- package/src/ui-tailwind/editor-surface/pm-schema.ts +452 -0
- package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +327 -0
- package/src/ui-tailwind/editor-surface/search-plugin.ts +157 -0
- package/src/ui-tailwind/editor-surface/tw-caret.tsx +12 -0
- package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +150 -0
- package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +118 -0
- package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +52 -0
- package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +151 -0
- package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +215 -0
- package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +111 -0
- package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +122 -0
- package/src/ui-tailwind/index.ts +61 -0
- package/src/ui-tailwind/review/tw-comment-sidebar.tsx +276 -0
- package/src/ui-tailwind/review/tw-health-panel.tsx +120 -0
- package/src/ui-tailwind/review/tw-review-rail.tsx +120 -0
- package/src/ui-tailwind/review/tw-revision-sidebar.tsx +164 -0
- package/src/ui-tailwind/status/tw-status-bar.tsx +58 -0
- package/src/ui-tailwind/theme/editor-theme.css +190 -0
- package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +48 -0
- package/src/ui-tailwind/toolbar/tw-toolbar.tsx +231 -0
- package/src/ui-tailwind/tw-review-workspace.tsx +140 -0
- package/src/validation/README.md +3 -0
- package/src/validation/compatibility-engine.ts +317 -0
- package/src/validation/compatibility-report.ts +160 -0
- package/src/validation/diagnostics.ts +203 -0
- package/src/validation/import-diagnostics.ts +128 -0
- package/src/validation/low-priority-word-surfaces.ts +373 -0
- package/dist/chunk-32W6IVQE.js +0 -7725
- package/dist/chunk-32W6IVQE.js.map +0 -1
- package/dist/index.cjs +0 -23722
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -7
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -16011
- package/dist/index.js.map +0 -1
- package/dist/public-types-DqCURAz8.d.cts +0 -1152
- package/dist/public-types-DqCURAz8.d.ts +0 -1152
- package/dist/tailwind.cjs +0 -8295
- package/dist/tailwind.cjs.map +0 -1
- package/dist/tailwind.d.cts +0 -323
- package/dist/tailwind.d.ts +0 -323
- package/dist/tailwind.js +0 -553
- package/dist/tailwind.js.map +0 -1
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ThemeColorScheme,
|
|
3
|
+
ThemeDefinition,
|
|
4
|
+
ThemeFontScheme,
|
|
5
|
+
} from "../../model/canonical-document.ts";
|
|
6
|
+
|
|
7
|
+
// ---- XML node types (inline, no external dep) ----
|
|
8
|
+
|
|
9
|
+
interface XmlElementNode {
|
|
10
|
+
type: "element";
|
|
11
|
+
name: string;
|
|
12
|
+
attributes: Record<string, string>;
|
|
13
|
+
children: XmlNode[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface XmlTextNode {
|
|
17
|
+
type: "text";
|
|
18
|
+
text: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type XmlNode = XmlElementNode | XmlTextNode;
|
|
22
|
+
|
|
23
|
+
// ---- Well-known DrawingML color slot names ----
|
|
24
|
+
|
|
25
|
+
const COLOR_SLOTS = [
|
|
26
|
+
"dk1",
|
|
27
|
+
"lt1",
|
|
28
|
+
"dk2",
|
|
29
|
+
"lt2",
|
|
30
|
+
"accent1",
|
|
31
|
+
"accent2",
|
|
32
|
+
"accent3",
|
|
33
|
+
"accent4",
|
|
34
|
+
"accent5",
|
|
35
|
+
"accent6",
|
|
36
|
+
"hlink",
|
|
37
|
+
"folHlink",
|
|
38
|
+
] as const;
|
|
39
|
+
|
|
40
|
+
// ---- Public API ----
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parse a DrawingML theme1.xml part into a ThemeDefinition.
|
|
44
|
+
* Returns an empty ThemeDefinition if the XML is empty or malformed.
|
|
45
|
+
*/
|
|
46
|
+
export function parseThemeXml(xml: string): ThemeDefinition {
|
|
47
|
+
if (!xml.trim()) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const root = parseXml(xml);
|
|
52
|
+
const themeElement = findChildElementOptional(root, "theme");
|
|
53
|
+
if (!themeElement) {
|
|
54
|
+
return {};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const themeName =
|
|
58
|
+
themeElement.attributes["name"] ??
|
|
59
|
+
themeElement.attributes["a:name"] ??
|
|
60
|
+
undefined;
|
|
61
|
+
|
|
62
|
+
const themeElements = findChildElementOptional(themeElement, "themeElements");
|
|
63
|
+
if (!themeElements) {
|
|
64
|
+
return { ...(themeName ? { name: themeName } : {}) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const colorScheme = parseColorScheme(
|
|
68
|
+
findChildElementOptional(themeElements, "clrScheme"),
|
|
69
|
+
);
|
|
70
|
+
const fontScheme = parseFontScheme(
|
|
71
|
+
findChildElementOptional(themeElements, "fontScheme"),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const result: ThemeDefinition = {};
|
|
75
|
+
if (themeName) {
|
|
76
|
+
result.name = themeName;
|
|
77
|
+
}
|
|
78
|
+
if (colorScheme) {
|
|
79
|
+
result.colorScheme = colorScheme;
|
|
80
|
+
}
|
|
81
|
+
if (fontScheme) {
|
|
82
|
+
result.fontScheme = fontScheme;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---- Internal helpers ----
|
|
89
|
+
|
|
90
|
+
function parseColorScheme(
|
|
91
|
+
element: XmlElementNode | undefined,
|
|
92
|
+
): ThemeColorScheme | undefined {
|
|
93
|
+
if (!element) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const schemeName =
|
|
98
|
+
element.attributes["name"] ?? element.attributes["a:name"] ?? "";
|
|
99
|
+
const colors: Record<string, string> = {};
|
|
100
|
+
|
|
101
|
+
for (const slot of COLOR_SLOTS) {
|
|
102
|
+
const slotElement = findChildElementOptional(element, slot);
|
|
103
|
+
if (!slotElement) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const color = extractColorValue(slotElement);
|
|
108
|
+
if (color) {
|
|
109
|
+
colors[slot] = color;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { name: schemeName, colors };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function extractColorValue(slotElement: XmlElementNode): string | undefined {
|
|
117
|
+
for (const child of slotElement.children) {
|
|
118
|
+
if (child.type !== "element") {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const name = localName(child.name);
|
|
123
|
+
|
|
124
|
+
if (name === "srgbClr") {
|
|
125
|
+
const val = child.attributes["val"] ?? child.attributes["a:val"] ?? "";
|
|
126
|
+
if (val) {
|
|
127
|
+
return `#${val.toUpperCase()}`;
|
|
128
|
+
}
|
|
129
|
+
} else if (name === "sysClr") {
|
|
130
|
+
// Use lastClr as the resolved color value
|
|
131
|
+
const lastClr =
|
|
132
|
+
child.attributes["lastClr"] ??
|
|
133
|
+
child.attributes["a:lastClr"] ??
|
|
134
|
+
"";
|
|
135
|
+
if (lastClr) {
|
|
136
|
+
return `#${lastClr.toUpperCase()}`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function parseFontScheme(
|
|
145
|
+
element: XmlElementNode | undefined,
|
|
146
|
+
): ThemeFontScheme | undefined {
|
|
147
|
+
if (!element) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const schemeName =
|
|
152
|
+
element.attributes["name"] ?? element.attributes["a:name"] ?? "";
|
|
153
|
+
|
|
154
|
+
const majorFontElement = findChildElementOptional(element, "majorFont");
|
|
155
|
+
const minorFontElement = findChildElementOptional(element, "minorFont");
|
|
156
|
+
|
|
157
|
+
const majorFont = majorFontElement
|
|
158
|
+
? extractFontTypeface(majorFontElement)
|
|
159
|
+
: undefined;
|
|
160
|
+
const minorFont = minorFontElement
|
|
161
|
+
? extractFontTypeface(minorFontElement)
|
|
162
|
+
: undefined;
|
|
163
|
+
|
|
164
|
+
const result: ThemeFontScheme = { name: schemeName };
|
|
165
|
+
if (majorFont) {
|
|
166
|
+
result.majorFont = majorFont;
|
|
167
|
+
}
|
|
168
|
+
if (minorFont) {
|
|
169
|
+
result.minorFont = minorFont;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function extractFontTypeface(
|
|
176
|
+
fontGroupElement: XmlElementNode,
|
|
177
|
+
): string | undefined {
|
|
178
|
+
const latinElement = findChildElementOptional(fontGroupElement, "latin");
|
|
179
|
+
if (latinElement) {
|
|
180
|
+
return (
|
|
181
|
+
latinElement.attributes["typeface"] ??
|
|
182
|
+
latinElement.attributes["a:typeface"] ??
|
|
183
|
+
undefined
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function findChildElementOptional(
|
|
190
|
+
node: XmlElementNode,
|
|
191
|
+
childLocalName: string,
|
|
192
|
+
): XmlElementNode | undefined {
|
|
193
|
+
return node.children.find(
|
|
194
|
+
(entry): entry is XmlElementNode =>
|
|
195
|
+
entry.type === "element" && localName(entry.name) === childLocalName,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function localName(name: string): string {
|
|
200
|
+
const idx = name.indexOf(":");
|
|
201
|
+
return idx >= 0 ? name.slice(idx + 1) : name;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ---- Minimal XML parser ----
|
|
205
|
+
|
|
206
|
+
function parseXml(xml: string): XmlElementNode {
|
|
207
|
+
const root: XmlElementNode = {
|
|
208
|
+
type: "element",
|
|
209
|
+
name: "__root__",
|
|
210
|
+
attributes: {},
|
|
211
|
+
children: [],
|
|
212
|
+
};
|
|
213
|
+
const stack: XmlElementNode[] = [root];
|
|
214
|
+
let cursor = 0;
|
|
215
|
+
|
|
216
|
+
while (cursor < xml.length) {
|
|
217
|
+
if (xml.startsWith("<!--", cursor)) {
|
|
218
|
+
const end = xml.indexOf("-->", cursor);
|
|
219
|
+
cursor = end >= 0 ? end + 3 : xml.length;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (xml.startsWith("<?", cursor)) {
|
|
224
|
+
const end = xml.indexOf("?>", cursor);
|
|
225
|
+
cursor = end >= 0 ? end + 2 : xml.length;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (xml.startsWith("<![CDATA[", cursor)) {
|
|
230
|
+
const end = xml.indexOf("]]>", cursor);
|
|
231
|
+
const textEnd = end >= 0 ? end : xml.length;
|
|
232
|
+
stack[stack.length - 1]?.children.push({
|
|
233
|
+
type: "text",
|
|
234
|
+
text: xml.slice(cursor + 9, textEnd),
|
|
235
|
+
});
|
|
236
|
+
cursor = end >= 0 ? end + 3 : xml.length;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (xml[cursor] !== "<") {
|
|
241
|
+
const nextTag = xml.indexOf("<", cursor);
|
|
242
|
+
const end = nextTag >= 0 ? nextTag : xml.length;
|
|
243
|
+
const text = decodeXmlEntities(xml.slice(cursor, end));
|
|
244
|
+
if (text.trim().length > 0 || (text.length > 0 && stack.length > 1)) {
|
|
245
|
+
stack[stack.length - 1]?.children.push({ type: "text", text });
|
|
246
|
+
}
|
|
247
|
+
cursor = end;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (xml[cursor + 1] === "/") {
|
|
252
|
+
const end = xml.indexOf(">", cursor);
|
|
253
|
+
if (end < 0) break;
|
|
254
|
+
stack.pop();
|
|
255
|
+
cursor = end + 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const tagEnd = xml.indexOf(">", cursor);
|
|
260
|
+
if (tagEnd < 0) break;
|
|
261
|
+
|
|
262
|
+
const tagContent = xml.slice(cursor + 1, tagEnd);
|
|
263
|
+
const selfClosing = tagContent.endsWith("/");
|
|
264
|
+
const normalized = selfClosing ? tagContent.slice(0, -1).trimEnd() : tagContent;
|
|
265
|
+
|
|
266
|
+
const spaceIndex = normalized.search(/\s/);
|
|
267
|
+
const tagName = spaceIndex >= 0 ? normalized.slice(0, spaceIndex) : normalized;
|
|
268
|
+
const attrString = spaceIndex >= 0 ? normalized.slice(spaceIndex + 1) : "";
|
|
269
|
+
const attributes = parseAttributes(attrString);
|
|
270
|
+
|
|
271
|
+
const element: XmlElementNode = {
|
|
272
|
+
type: "element",
|
|
273
|
+
name: tagName,
|
|
274
|
+
attributes,
|
|
275
|
+
children: [],
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
stack[stack.length - 1]?.children.push(element);
|
|
279
|
+
|
|
280
|
+
if (!selfClosing) {
|
|
281
|
+
stack.push(element);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
cursor = tagEnd + 1;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return root;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function parseAttributes(attrString: string): Record<string, string> {
|
|
291
|
+
const attrs: Record<string, string> = {};
|
|
292
|
+
const pattern = /([A-Za-z_:][A-Za-z0-9:._-]*)\s*=\s*("([^"]*)"|'([^']*)')/gu;
|
|
293
|
+
for (const match of attrString.matchAll(pattern)) {
|
|
294
|
+
const name = match[1];
|
|
295
|
+
const value = match[3] ?? match[4] ?? "";
|
|
296
|
+
if (name) {
|
|
297
|
+
attrs[name] = decodeXmlEntities(value);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return attrs;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function decodeXmlEntities(text: string): string {
|
|
304
|
+
return text
|
|
305
|
+
.replace(/&/g, "&")
|
|
306
|
+
.replace(/</g, "<")
|
|
307
|
+
.replace(/>/g, ">")
|
|
308
|
+
.replace(/"/g, '"')
|
|
309
|
+
.replace(/'/g, "'")
|
|
310
|
+
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(Number.parseInt(dec, 10)))
|
|
311
|
+
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) =>
|
|
312
|
+
String.fromCodePoint(Number.parseInt(hex, 16)),
|
|
313
|
+
);
|
|
314
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
export const CONTENT_TYPES_PATH = "/[Content_Types].xml";
|
|
2
|
+
export const PACKAGE_RELATIONSHIPS_PATH = "/_rels/.rels";
|
|
3
|
+
|
|
4
|
+
export type OpcCompressionMethod = "store" | "deflate";
|
|
5
|
+
export type OpcSurfaceKind = "content" | "relationships" | "content-types";
|
|
6
|
+
export type OpcRelationshipTargetMode = "internal" | "external";
|
|
7
|
+
|
|
8
|
+
export interface OpcRelationship {
|
|
9
|
+
id: string;
|
|
10
|
+
type: string;
|
|
11
|
+
target: string;
|
|
12
|
+
targetMode: OpcRelationshipTargetMode;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface OpcPartManifestEntry {
|
|
16
|
+
path: string;
|
|
17
|
+
surfaceKind: OpcSurfaceKind;
|
|
18
|
+
contentType: string | null;
|
|
19
|
+
relationshipsPartPath: string | null;
|
|
20
|
+
relationships: OpcRelationship[];
|
|
21
|
+
compression: OpcCompressionMethod;
|
|
22
|
+
byteLength: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface OpcContentTypesManifest {
|
|
26
|
+
defaults: Record<string, string>;
|
|
27
|
+
overrides: Record<string, string>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface OpcPackageManifest {
|
|
31
|
+
contentTypes: OpcContentTypesManifest;
|
|
32
|
+
packageRelationships: OpcRelationship[];
|
|
33
|
+
parts: OpcPartManifestEntry[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface OpcPackagePart {
|
|
37
|
+
path: string;
|
|
38
|
+
surfaceKind: OpcSurfaceKind;
|
|
39
|
+
contentType: string | null;
|
|
40
|
+
relationshipsPartPath: string | null;
|
|
41
|
+
relationships: OpcRelationship[];
|
|
42
|
+
compression: OpcCompressionMethod;
|
|
43
|
+
bytes: Uint8Array;
|
|
44
|
+
crc32: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function normalizePartPath(path: string): string {
|
|
48
|
+
const normalized = path.replace(/\\/g, "/").trim();
|
|
49
|
+
if (normalized.length === 0) {
|
|
50
|
+
throw new Error("OPC part path must not be empty.");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const withLeadingSlash = normalized.startsWith("/") ? normalized : `/${normalized}`;
|
|
54
|
+
const segments = withLeadingSlash.split("/");
|
|
55
|
+
const resolved: string[] = [];
|
|
56
|
+
|
|
57
|
+
for (const segment of segments) {
|
|
58
|
+
if (segment === "" || segment === ".") {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (segment === "..") {
|
|
63
|
+
if (resolved.length === 0) {
|
|
64
|
+
throw new Error(`OPC part path escapes package root: ${path}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
resolved.pop();
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
resolved.push(segment);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return `/${resolved.join("/")}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function getExtension(partPath: string): string {
|
|
78
|
+
const normalized = normalizePartPath(partPath);
|
|
79
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
80
|
+
const lastDot = normalized.lastIndexOf(".");
|
|
81
|
+
if (lastDot <= lastSlash) {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return normalized.slice(lastDot + 1).toLowerCase();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function getRelationshipsPartPath(partPath?: string | null): string {
|
|
89
|
+
if (!partPath) {
|
|
90
|
+
return PACKAGE_RELATIONSHIPS_PATH;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const normalized = normalizePartPath(partPath);
|
|
94
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
95
|
+
const directory = lastSlash <= 0 ? "" : normalized.slice(0, lastSlash);
|
|
96
|
+
const filename = normalized.slice(lastSlash + 1);
|
|
97
|
+
return `${directory}/_rels/${filename}.rels`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function getSourcePartPathFromRelationshipsPart(path: string): string | null {
|
|
101
|
+
const normalized = normalizePartPath(path);
|
|
102
|
+
if (normalized === PACKAGE_RELATIONSHIPS_PATH) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const marker = "/_rels/";
|
|
107
|
+
const markerIndex = normalized.lastIndexOf(marker);
|
|
108
|
+
if (markerIndex === -1 || !normalized.endsWith(".rels")) {
|
|
109
|
+
throw new Error(`Invalid OPC relationships part path: ${path}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return normalizePartPath(
|
|
113
|
+
`${normalized.slice(0, markerIndex)}${normalized.slice(markerIndex + marker.length, -".rels".length)}`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function resolveRelationshipTarget(
|
|
118
|
+
sourcePartPath: string | null,
|
|
119
|
+
relationship: Pick<OpcRelationship, "target" | "targetMode">,
|
|
120
|
+
): string {
|
|
121
|
+
if (relationship.targetMode === "external") {
|
|
122
|
+
return relationship.target;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (relationship.target.startsWith("/")) {
|
|
126
|
+
return normalizePartPath(relationship.target);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const basePath = sourcePartPath ? normalizePartPath(sourcePartPath) : "/";
|
|
130
|
+
const baseDir = basePath === "/" ? "/" : basePath.slice(0, basePath.lastIndexOf("/") + 1);
|
|
131
|
+
return normalizePartPath(`${baseDir}${relationship.target}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function comparePartPaths(a: string, b: string): number {
|
|
135
|
+
return normalizePartPath(a).localeCompare(normalizePartPath(b));
|
|
136
|
+
}
|