34 lines
5.3 KiB
Markdown
34 lines
5.3 KiB
Markdown
# Training document preview shell
|
|
|
|
This local shell renders **DOCX, TXT, and Markdown** inside the native course card. PDF uses each platform's native PDF component. Legacy `.doc` is deliberately unsupported. All parser scripts ship with the application; no CDN, online converter, analytics, application token, or signed attachment URL is used in this page.
|
|
|
|
## Native host contract
|
|
|
|
- Open `index.html?type=docx|txt|md&requestId=<opaque>` on a trusted local origin. `requestId` is a newly generated document-load ID matching `[A-Za-z0-9_-]{1,80}` and contains no course/user/URL credentials.
|
|
- iOS uses `training-document://preview/`; Android uses intercepted `https://training-preview.invalid/`.
|
|
- Serve the current downloaded attachment bytes at the relative path `./document/<requestId>/file`. Only the exact active ID is valid. The native host downloads the remote URL and enforces its own identity, expiry, file type, size, and redirect policy. The shell never receives the remote URL or auth headers.
|
|
- Serve this directory's files only by an explicit asset allowlist, without filesystem path traversal. Deny requests for every other scheme, host, document ID, and path. The page's CSP is defense in depth; the native host must also block all remote navigation and subresources.
|
|
- Resource loading uses `XMLHttpRequest` so WKURLSchemeHandler can serve bytes. If WebKit requires a CORS response header, add it only to this trusted local resource response; never enable general remote access.
|
|
- iOS messages: `window.webkit.messageHandlers.trainingDocument.postMessage(event)`.
|
|
- Android messages: `window.TrainingDocument.postMessage(JSON.stringify(event))`. Expose only this tiny bridge on the trusted local main-frame origin. Prefer origin-scoped WebMessageListener when available.
|
|
- Every event has `{ type, requestId }`. Native must discard stale IDs and messages from subframes/other origins.
|
|
- `ready`: `{ type: "ready", requestId, height }`, sent after content render, embedded-image settlement, and two animation frames. Native must also verify its view is visible before counting study time; Android should use its visual state callback.
|
|
- `error`: `{ type: "error", requestId, code, message }`; stop study time and expose a native retry action. Do not count navigation/onload as successful rendering.
|
|
- `link`: `{ type: "link", requestId, url }`; sent only for HTTP/HTTPS anchor clicks. Native validates again and explicitly opens the external browser, pausing study time. The local shell never navigates away.
|
|
- A retry or attachment change creates a fresh request ID and reloads the page. A closed/replaced view must not restart timing after late events.
|
|
|
|
## Rendering and compatibility
|
|
|
|
- Libraries are pinned by exact version, upstream npm SHA-512 integrity and file SHA-256 in `vendor-manifest.json`.
|
|
- Upstream browser bundles are transformed only with `esbuild 0.28.2 --target=es2017 --minify --legal-comments=inline`. This removes ES2020 optional chaining/nullish syntax from docx-preview for older Android WebViews. `compat.js` supplies `globalThis`, `Array.flatMap`, and the `Promise.allSettled` used by docx-preview only when missing. No full polyfill framework is loaded. The pinned browser bundles do not use `Promise.finally`, `Object.fromEntries`, or `String.replaceAll`; these are deliberately not patched. iOS 16 and Android WebView 58+ with ES2017 support are the intended baseline; device rendering tests remain required.
|
|
- DOCX uses `docx-preview` with `renderAltChunks: false`, no experimental features or embedded-font loading. It renders into a detached tree, then DOMPurify sanitizes the result before insertion. Script/frame/object/form/SVG/MathML content and unsafe links are removed. Style URLs are filtered, and CSP blocks remote images/fonts/styles/connections. Legal embedded bitmap images use local blob/data URLs. Complex Word pagination, fonts, and unsupported drawings may differ from Microsoft Word.
|
|
- Markdown uses markdown-it with raw HTML, automatic linkification and typographic rewrites disabled, followed by DOMPurify. Markdown external/relative images do not perform network requests; their alt text is displayed instead. HTTP/HTTPS links are handed to native on a user click.
|
|
- TXT uses a text node, never `innerHTML`. UTF-8, UTF-16 BOM or detectable ASCII UTF-16 byte order, then GB18030 fallback are supported. Binary control characters, empty text, and undecodable input produce an error.
|
|
- Native and shell both cap source files at **30 MiB**. Text is capped at **5 MiB**. DOCX checks the ZIP central directory before inflation and bounds actual streamed inflation: at most **1000 entries**, **20 MiB per entry**, and **100 MiB total**. ZIP64, split/encrypted archives, unsupported compression, malformed files and missing required DOCX parts are rejected.
|
|
|
|
## Licensing and maintenance
|
|
|
|
See `NOTICE.md`, `licenses/`, and `vendor-manifest.json`. Keep the copyright headers, license texts, notices, and exact version metadata whenever updating these files. The same resource directory is copied byte-for-byte to Android `app/src/main/assets/training-document-preview/`.
|
|
|
|
To regenerate each bundle, download the exact upstream tarball in the manifest, verify its integrity, extract the named `upstreamSource`, run the esbuild command above, then verify the listed asset SHA-256. Do not load a remote "latest" bundle at runtime.
|