fix: open a file whose extension names the wrong format

Which importer ran was decided by the extension alone, so a .docx saved or
renamed as .odt (and the reverse) was reported as a broken file. A failed
read is retried as the other format; a file that is neither still reports
the error for the extension it carries.
This commit is contained in:
Steffen Becker committed 2026-08-16 03:00:27 +02:00
1 parent 96863395f3
commit 273c533fce
2 files changed
+14 -3

No files matched your search

+13 -2
View File
@@ -590,14 +590,25 @@
if (!editor) return;
try {
const name = sourceName?.toLowerCase() ?? '';
const isDocx = name.endsWith('.docx') || name.endsWith('.dotx');
let isDocx = name.endsWith('.docx') || name.endsWith('.dotx');
// A template is loaded for its content but never bound as the handle, so the
// first Save prompts for a new document instead of overwriting the template.
const isTemplate = name.endsWith('.ott') || name.endsWith('.dotx');
// Pre-decode any images in a format the browser can't render (TIFF, …) to PNG.
// Lazy: the decoder loads only when such an image is present, else this is a no-op.
const converted = await convertUnsupportedImages(bytes);
const result = isDocx ? importDocx(bytes, converted) : importOdt(bytes, converted);
// An extension can be wrong (a renamed or mis-saved file) and both word processors
// go by content, so the other format is tried before the file is called broken —
// a file that is neither reports the error for the extension it carries.
let result;
try {
result = isDocx ? importDocx(bytes, converted) : importOdt(bytes, converted);
} catch (err) {
try {
result = isDocx ? importOdt(bytes, converted) : importDocx(bytes, converted);
isDocx = !isDocx;
} catch { throw err; }
}
const hasContent = editor.state.doc.textContent.length > 0 || editor.state.doc.childCount > 1;
if (hasContent && !confirm(t().dialogs.confirmReplace)) {
+1 -1
View File
@@ -5,7 +5,7 @@
## ODF import (`odt.ts`, `styleResolver.ts`)
The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(bytes)` and replaces the document via `setContent` (after a confirm when the current doc is non-empty), also adopting the file's page margins/orientation. It parses `content.xml`/`styles.xml` directly (fflate + `DOMParser`) rather than odf-kit's reader, which flattens cell blocks and drops row heights / list number formats.
The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(bytes)` and replaces the document via `setContent` (after a confirm when the current doc is non-empty), also adopting the file's page margins/orientation. It parses `content.xml`/`styles.xml` directly (fflate + `DOMParser`) rather than odf-kit's reader, which flattens cell blocks and drops row heights / list number formats. Which importer runs is chosen by extension, but a **failed read is retried as the other format** — a renamed or mis-saved file is what both word processors go by content for; a file that is neither reports the error for the extension it carries.
- **`StyleResolver`** flattens ODF style indirection: named + automatic styles from both XML files, `style:parent-style-name` chains rooted in `style:default-style`, font-face declarations (`style:font-name` → family), `rel-column-width`/`column-width`, `min-row-height`, list styles, and page geometry. `fo:font-family` and `style:font-name` shadow each other across chain levels (`layerTextProps`). A **percentage `fo:font-size`** (`"130%"` — how LibreOffice defines its heading styles) is resolved against the inherited size while walking the chain (`resolvePercentSize`); without it the file's size is dropped and our default silently replaces it.
- **LibreOffice's paragraph properties are not all inherited one by one.** Its two vertical