@file-viewer/renderer-text 3.1.0 → 3.1.2
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 +86 -1
- package/README.md +75 -1
- package/bin/copy-xml-profile-assets.mjs +59 -0
- package/dist/index.js +26 -3
- package/dist/patch.js +9 -2
- package/dist/xml-profiles.d.ts +4 -0
- package/dist/xml-profiles.js +5 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +174 -0
- package/dist/xmlEngines.d.ts +9 -0
- package/dist/xmlEngines.js +158 -0
- package/dist/xmlProfiles.d.ts +7 -0
- package/dist/xmlProfiles.js +145 -0
- package/dist/xmlRegistration.d.ts +8 -0
- package/dist/xmlRegistration.js +13 -0
- package/dist/xmlSafety.d.ts +19 -0
- package/dist/xmlSafety.js +146 -0
- package/dist/xmlWasmMemory.d.ts +7 -0
- package/dist/xmlWasmMemory.js +77 -0
- package/licenses/xml-profiles/NOTICE.md +31 -0
- package/licenses/xml-profiles/libxml2-xsd-Copyright.txt +23 -0
- package/licenses/xml-profiles/libxml2-xslt-Copyright.txt +24 -0
- package/licenses/xml-profiles/libxslt-Copyright.txt +53 -0
- package/licenses/xml-profiles/xmllint-wasm-COPYING.txt +21 -0
- package/licenses/xml-profiles/xslt-polyfill-LICENSE.txt +28 -0
- package/licenses/xml-profiles/zlib-LICENSE.txt +22 -0
- package/package.json +32 -6
package/README.en.md
CHANGED
|
@@ -44,7 +44,7 @@ const options = {
|
|
|
44
44
|
- With the text-tools capability installed, `patch` uses `diff2html` for side-by-side review and `bundle` / `bdl` enables Git bundle inspection.
|
|
45
45
|
- With the Mermaid capability installed, fenced Mermaid blocks render as diagrams. Without it, the source stays visible with the exact CLI enablement command.
|
|
46
46
|
- HTML/HTM opens a static page preview with a source-view toggle. `options.text.htmlView: 'source'` starts with the original source. Inline CSS and embedded images are preserved; scripts, forms, external navigation, and external resource requests are blocked by sanitization, CSP, and an opaque sandbox. This is not a website runtime.
|
|
47
|
-
- XML, Vue, and similar files
|
|
47
|
+
- XML, Vue, and similar files default to escaped source previews. XML can opt into the XSD/XSLT profiles below. HTML source supports the same highlighting, formatting, and large-text virtualization options.
|
|
48
48
|
- Markdown uses `marked` for a read-only reading surface with dark/light theme support, table scrolling, and a unified zoom provider.
|
|
49
49
|
- Markdown no longer falls back to source because of the general large-text threshold. Set `options.text.markdownVirtualizeAboveBytes` only when an application must bound exceptionally large Markdown files.
|
|
50
50
|
- Does not depend on any online service or public CDN, making it suitable for intranet logs, configs, snippets, README files, and knowledge-base attachments.
|
|
@@ -52,3 +52,88 @@ const options = {
|
|
|
52
52
|
## Migration Note
|
|
53
53
|
|
|
54
54
|
Standard/full includes base code, text, and Markdown without installing `diff2html`, `pako`, or Mermaid. Optional uploads show `npx file-viewer-cli add text-tools --write` or `add mermaid-markdown --write`; `preset-all` is only for explicit all/debug use.
|
|
55
|
+
|
|
56
|
+
## Optional XML Profiles
|
|
57
|
+
|
|
58
|
+
XML profiles use real libxml2 XML Schema 1.0 and libxslt XSLT 1.0 WASM engines.
|
|
59
|
+
The ordinary text/standard/full installation does not install these optional peers
|
|
60
|
+
or replace the browser's `XSLTProcessor`. To enable profiles, install the pinned
|
|
61
|
+
engines and copy their assets:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.29
|
|
65
|
+
pnpm exec file-viewer-xml-assets public/file-viewer/xml
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The asset helper copies installed files without downloading or installing anything.
|
|
69
|
+
It preserves unrelated destination files and includes JS/WASM, `licenses/`, and a
|
|
70
|
+
SHA-256 `manifest.json`. Keep the notices with redistributed assets.
|
|
71
|
+
|
|
72
|
+
Register once, then pass the options to Vanilla/Web Component or any standard component:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { enableFileViewerXmlProfiles } from '@file-viewer/renderer-text/xml-profiles'
|
|
76
|
+
|
|
77
|
+
const disableXmlProfiles = enableFileViewerXmlProfiles()
|
|
78
|
+
const options = {
|
|
79
|
+
xml: {
|
|
80
|
+
profilesUrl: '/xml-profiles/profiles.json',
|
|
81
|
+
runtime: {
|
|
82
|
+
xsdWorkerUrl: '/file-viewer/xml/xmllint-browser.mjs',
|
|
83
|
+
xsltModuleUrl: '/file-viewer/xml/xslt-wasm.js',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
// Call disableXmlProfiles() when registration is no longer needed.
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Keep `xmllint.wasm` beside `xmllint-browser.mjs`. Serve JS/MJS as `text/javascript`
|
|
91
|
+
and WASM as `application/wasm`. Omitted runtime URLs resolve under
|
|
92
|
+
`xml/` in the configured asset base, or relative to the page when no asset base is
|
|
93
|
+
configured. The host CSP must permit `worker-src blob:` and WASM compilation
|
|
94
|
+
(`script-src 'wasm-unsafe-eval'` where supported). All profile and runtime resources
|
|
95
|
+
must use same-origin HTTP(S) without redirects. Serve them with the application
|
|
96
|
+
for offline/intranet use; no public CDN or document upload is involved.
|
|
97
|
+
|
|
98
|
+
Example manifest:
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"profiles": [{
|
|
103
|
+
"id": "invoice-v1",
|
|
104
|
+
"match": {
|
|
105
|
+
"rootNamespace": { "enabled": true, "root": "invoice", "namespace": "urn:example:invoice:v1" },
|
|
106
|
+
"xsd": { "enabled": true }
|
|
107
|
+
},
|
|
108
|
+
"xsd": "./invoice.xsd",
|
|
109
|
+
"xslt": "./invoice.xsl"
|
|
110
|
+
}]
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Resource paths resolve against the manifest directory. Alternatively, supply
|
|
115
|
+
`xml.profiles` and `xml.baseUrl` instead of `profilesUrl`. Each profile must enable
|
|
116
|
+
at least one check. Root/namespace and XSD checks are independent; all enabled
|
|
117
|
+
checks must pass. Exactly one matching profile triggers transformation. No match,
|
|
118
|
+
ambiguity, validation errors and processing failures retain the XML source and
|
|
119
|
+
report diagnostics through `xml.onDiagnostic`, the standard `onDiagnostic`, and
|
|
120
|
+
the diagnostics panel.
|
|
121
|
+
|
|
122
|
+
Generated HTML is sanitized and displayed in an opaque iframe with an empty
|
|
123
|
+
sandbox and a CSP blocking scripts and external resources. `View Source` and
|
|
124
|
+
`View Rendered` reuse the original input buffer and cached result without another
|
|
125
|
+
download, validation or transformation. Source is never DOM serialization and
|
|
126
|
+
pretty printing is disabled for this view. The original remains the download
|
|
127
|
+
source. Use `xml.initialView: 'source'` or customize button copy with `xml.labels`.
|
|
128
|
+
|
|
129
|
+
DTD/entity declarations, XInclude, schema include/import/redefine/override,
|
|
130
|
+
XSLT include/import, `document()` and extension instructions fail closed. There
|
|
131
|
+
is no external-resource resolver in this version; such documents fall back to source.
|
|
132
|
+
The default operation deadline is 15 seconds, capped at 60 seconds. Hard ceilings
|
|
133
|
+
are 4 MiB XML, 2 MiB per schema/stylesheet, 8 MiB output, 16 MiB total resources,
|
|
134
|
+
and 32 profiles; `xml.limits` may lower them. Each engine's WASM linear memory is
|
|
135
|
+
capped at 64 MiB. Timeout, cancellation and unload terminate workers and revoke Blob URLs.
|
|
136
|
+
|
|
137
|
+
The source repository's `test/fixtures/issue-305/` contains redistributable
|
|
138
|
+
valid/invalid XML, XSD, XSLT and a manifest. See [XML profiles notices](./licenses/xml-profiles/NOTICE.md)
|
|
139
|
+
for verified licenses and upstream sources.
|
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ const options = {
|
|
|
44
44
|
- 安装 text-tools capability 后,`patch` 使用 `diff2html` 渲染左右比对视图,`bundle` / `bdl` 才启用 Git bundle 结构检查。
|
|
45
45
|
- 安装 Mermaid capability 后,Markdown 内嵌 Mermaid 图才会渲染;未安装时保留源码并显示精确 CLI 启用命令。
|
|
46
46
|
- HTML / HTM 默认显示静态页面,提供页面与源码切换;`options.text.htmlView: 'source'` 可默认查看原始源码。页面保留内联 CSS 和内嵌图片,通过净化、CSP 和独立沙箱阻止脚本、表单、外链跳转和外部资源请求,不用于运行完整网站。
|
|
47
|
-
- XML / Vue
|
|
47
|
+
- XML / Vue 等默认按源码转义展示。XML 可显式启用下方的 XSD/XSLT profiles;HTML 源码视图继续支持高亮、格式化和大文本虚拟化。
|
|
48
48
|
- Markdown 使用 `marked` 输出只读阅读面,并保留明暗主题、表格滚动和统一缩放 provider。
|
|
49
49
|
- Markdown 不再因为通用大文本阈值自动退化成源码;如业务必须限制超大 Markdown,可单独设置 `options.text.markdownVirtualizeAboveBytes`。
|
|
50
50
|
- 不绑定任何在线服务或公共 CDN,适合内网日志、配置、代码片段、README 和知识库附件预览。
|
|
@@ -52,3 +52,77 @@ const options = {
|
|
|
52
52
|
## 迁移说明
|
|
53
53
|
|
|
54
54
|
standard/full 默认包含基础代码、文本和 Markdown,不安装 `diff2html`、`pako` 或 Mermaid。打开可选格式时会提示运行 `npx file-viewer-cli add text-tools --write` 或 `add mermaid-markdown --write`;`preset-all` 仅用于显式全量/调试。
|
|
55
|
+
|
|
56
|
+
## 可选 XML Profiles
|
|
57
|
+
|
|
58
|
+
XML profiles 使用真实的 libxml2 XML Schema 1.0 和 libxslt XSLT 1.0 WASM 引擎。
|
|
59
|
+
普通 text/standard/full 不安装这两个可选引擎,也不会替换浏览器的 `XSLTProcessor`。
|
|
60
|
+
需要此功能时,安装固定版本并复制资源:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.29
|
|
64
|
+
pnpm exec file-viewer-xml-assets public/file-viewer/xml
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
复制命令不联网、不安装依赖。它会保留目标目录的其他文件,并写入两个引擎的
|
|
68
|
+
JS/WASM、`licenses/` 和带 SHA-256 的 `manifest.json`。部署时一并保留许可证。
|
|
69
|
+
|
|
70
|
+
先注册实现,再把 `xml` 配置传给 Vanilla/Web Component 或其他标准组件:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { enableFileViewerXmlProfiles } from '@file-viewer/renderer-text/xml-profiles'
|
|
74
|
+
|
|
75
|
+
const disableXmlProfiles = enableFileViewerXmlProfiles()
|
|
76
|
+
const options = {
|
|
77
|
+
xml: {
|
|
78
|
+
profilesUrl: '/xml-profiles/profiles.json',
|
|
79
|
+
runtime: {
|
|
80
|
+
xsdWorkerUrl: '/file-viewer/xml/xmllint-browser.mjs',
|
|
81
|
+
xsltModuleUrl: '/file-viewer/xml/xslt-wasm.js',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
// 不再需要全局注册时调用 disableXmlProfiles()。
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`xmllint.wasm` 必须与 `xmllint-browser.mjs` 位于同一目录。JS/MJS 使用
|
|
89
|
+
`text/javascript`,WASM 使用 `application/wasm`。省略 runtime URL 时,
|
|
90
|
+
默认从配置的 asset base 下的 `xml/` 查找;未配置 asset base 则相对于页面 URL。
|
|
91
|
+
页面 CSP 需允许 `worker-src blob:` 和 WASM 编译(支持该指令的浏览器可使用
|
|
92
|
+
`script-src 'wasm-unsafe-eval'`)。引擎和 profiles 仅允许同源 HTTP(S),不跟随重定向。
|
|
93
|
+
所有资源可随应用部署,运行时不依赖外网。
|
|
94
|
+
|
|
95
|
+
Manifest 示例:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"profiles": [{
|
|
100
|
+
"id": "invoice-v1",
|
|
101
|
+
"match": {
|
|
102
|
+
"rootNamespace": { "enabled": true, "root": "invoice", "namespace": "urn:example:invoice:v1" },
|
|
103
|
+
"xsd": { "enabled": true }
|
|
104
|
+
},
|
|
105
|
+
"xsd": "./invoice.xsd",
|
|
106
|
+
"xslt": "./invoice.xsl"
|
|
107
|
+
}]
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
相对路径以 manifest 所在目录为基准。也可以传 `xml.profiles` 数组和 `xml.baseUrl`,
|
|
112
|
+
替代 `profilesUrl`。每个 profile 至少启用一项检查;root/namespace 和 XSD 可各自启用,
|
|
113
|
+
同时启用时必须全部通过。仅有一个 profile 通过时才转换;多个匹配、无匹配或任何处理错误
|
|
114
|
+
都保留 XML 源码,并通过 `xml.onDiagnostic`、标准 `onDiagnostic` 和界面诊断栏说明原因。
|
|
115
|
+
|
|
116
|
+
转换后的 HTML 经过净化,放入无 sandbox 权限的隔离 iframe,并附带禁止脚本和外网的 CSP。
|
|
117
|
+
`View Source` / `View Rendered` 切换使用已保留的原始 buffer,不重新下载、校验或转换,
|
|
118
|
+
也不使用解析后重新序列化的 XML。源码视图强制关闭 pretty print;下载仍使用原文件。
|
|
119
|
+
可通过 `xml.initialView: 'source'` 默认查看源码,通过 `xml.labels` 定制按钮文字。
|
|
120
|
+
|
|
121
|
+
DTD、实体声明、XInclude、XSD include/import/redefine/override、XSLT include/import、
|
|
122
|
+
`document()` 和扩展指令默认拒绝。当前没有开放外部资源 resolver;需要这些机制的文件会回退源码。
|
|
123
|
+
默认总耗时上限 15 秒(最多 60 秒),XML 4 MiB、单个 schema/style 2 MiB、输出 8 MiB、
|
|
124
|
+
资源总计 16 MiB、最多 32 个 profiles。`xml.limits` 可降低这些上限,不能取消。
|
|
125
|
+
两个引擎的 WASM 线性内存均硬限 64 MiB;超时、取消和卸载会终止 Worker 并释放 Blob URL。
|
|
126
|
+
|
|
127
|
+
源仓库的 `test/fixtures/issue-305/` 提供可再分发的 valid/invalid XML、XSD、XSLT 和 manifest。
|
|
128
|
+
引擎许可证与具体上游来源见 [XML profiles notices](./licenses/xml-profiles/NOTICE.md)。
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { realpathSync } from 'node:fs'
|
|
4
|
+
import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises'
|
|
5
|
+
import { createHash } from 'node:crypto'
|
|
6
|
+
import { dirname, join, resolve } from 'node:path'
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
|
|
9
|
+
const require = createRequire(import.meta.url)
|
|
10
|
+
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
11
|
+
const engines = [
|
|
12
|
+
{ name: 'xmllint-wasm', version: '5.3.0', files: ['xmllint-browser.mjs', 'xmllint.wasm'] },
|
|
13
|
+
{ name: 'xslt-polyfill', version: '1.0.29', files: ['dist/xslt-wasm.js'] }
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/** Copy pinned optional runtimes and notices; never download or install packages. */
|
|
17
|
+
export async function copyXmlProfileAssets(destination = 'public/file-viewer/xml') {
|
|
18
|
+
const output = new Map()
|
|
19
|
+
for (const engine of engines) {
|
|
20
|
+
let manifest
|
|
21
|
+
try { manifest = require.resolve(`${engine.name}/package.json`) } catch {
|
|
22
|
+
throw new Error(`Install the optional XML engines first: pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.29 (missing ${engine.name}).`)
|
|
23
|
+
}
|
|
24
|
+
const metadata = JSON.parse(await readFile(manifest, 'utf8'))
|
|
25
|
+
if (metadata.version !== engine.version) throw new Error(`Expected ${engine.name}@${engine.version}; found ${metadata.version}.`)
|
|
26
|
+
for (const name of engine.files) output.set(name.split('/').at(-1), await readFile(join(dirname(manifest), name)))
|
|
27
|
+
}
|
|
28
|
+
const licenses = join(packageRoot, 'licenses/xml-profiles')
|
|
29
|
+
for (const name of await readdir(licenses)) output.set(`licenses/${name}`, await readFile(join(licenses, name)))
|
|
30
|
+
const files = Object.fromEntries([...output].map(([name, bytes]) => [name, {
|
|
31
|
+
bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex')
|
|
32
|
+
}]))
|
|
33
|
+
output.set('manifest.json', Buffer.from(JSON.stringify({
|
|
34
|
+
schema: 1, engines: engines.map(({ name, version }) => ({ name, version })), files
|
|
35
|
+
}, null, 2) + '\n'))
|
|
36
|
+
destination = resolve(destination)
|
|
37
|
+
// Resolve every dependency before writing, and touch only the named XML assets.
|
|
38
|
+
for (const [name, bytes] of output) {
|
|
39
|
+
const filename = join(destination, name)
|
|
40
|
+
await mkdir(dirname(filename), { recursive: true })
|
|
41
|
+
await writeFile(filename, bytes)
|
|
42
|
+
}
|
|
43
|
+
return { destination, files }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let entryPoint = false
|
|
47
|
+
try { entryPoint = !!process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url) } catch { /* Imported helper. */ }
|
|
48
|
+
if (entryPoint) {
|
|
49
|
+
if (process.argv.includes('--help')) {
|
|
50
|
+
console.log('Usage: file-viewer-xml-assets [destination-directory]\nDefault: public/file-viewer/xml\nRequires xmllint-wasm@5.3.0 and xslt-polyfill@1.0.29 installed by the host.')
|
|
51
|
+
} else if (process.argv.length > 3 || process.argv[2]?.startsWith('-')) {
|
|
52
|
+
console.error('Expected one destination directory.'); process.exitCode = 1
|
|
53
|
+
} else {
|
|
54
|
+
copyXmlProfileAssets(process.argv[2]).then(
|
|
55
|
+
({ destination }) => console.log(`XML profile assets copied to ${destination}`),
|
|
56
|
+
error => { console.error(error.message); process.exitCode = 1 }
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
|
+
import { getXmlProfilesLoader } from './xmlRegistration.js';
|
|
2
3
|
export { getFileViewerMermaidLoader, getFileViewerDiffToHtml, getFileViewerPakoLoader, registerFileViewerDiffToHtml, registerFileViewerMermaidLoader, registerFileViewerPakoLoader, } from './optionalCapabilities.js';
|
|
3
4
|
export { sanitizeFileViewerRichHtml } from './sanitizeHtml.js';
|
|
4
5
|
const textRendererIds = ['code', 'markdown'];
|
|
@@ -7,9 +8,31 @@ if (textDefinitions.length !== textRendererIds.length) {
|
|
|
7
8
|
throw new Error('@file-viewer/renderer-text could not locate the shared code/markdown format definitions.');
|
|
8
9
|
}
|
|
9
10
|
export const textRendererDefinitions = textDefinitions;
|
|
10
|
-
export const renderFileViewerCode = (buffer, target, type, context) =>
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export const renderFileViewerCode = async (buffer, target, type, context) => {
|
|
12
|
+
var _a, _b, _c, _d, _e;
|
|
13
|
+
if (/^(?:html|htm)$/i.test(type || '')) {
|
|
14
|
+
const { default: renderHtml } = await import('./html.js');
|
|
15
|
+
return renderHtml(buffer, target, type, context);
|
|
16
|
+
}
|
|
17
|
+
if ((type === null || type === void 0 ? void 0 : type.trim().toLowerCase()) === 'xml' && ((_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.xml)) {
|
|
18
|
+
const loader = getXmlProfilesLoader();
|
|
19
|
+
if (loader) {
|
|
20
|
+
const { default: renderXml } = await loader();
|
|
21
|
+
return renderXml(buffer, target, context);
|
|
22
|
+
}
|
|
23
|
+
const message = 'Enable @file-viewer/renderer-text/xml-profiles and deploy its self-hosted runtime assets.';
|
|
24
|
+
try {
|
|
25
|
+
(_c = (_b = context.options.xml).onDiagnostic) === null || _c === void 0 ? void 0 : _c.call(_b, { code: 'capability-unavailable', message });
|
|
26
|
+
}
|
|
27
|
+
catch { /* Host diagnostics must not prevent source preview. */ }
|
|
28
|
+
try {
|
|
29
|
+
(_e = (_d = context.options).onDiagnostic) === null || _e === void 0 ? void 0 : _e.call(_d, { code: 'xml-profile-capability-unavailable', level: 'warning', message });
|
|
30
|
+
}
|
|
31
|
+
catch { /* Host diagnostics must not prevent source preview. */ }
|
|
32
|
+
}
|
|
33
|
+
const { default: renderCode } = await import('./code.js');
|
|
34
|
+
return renderCode(buffer, target, type, context);
|
|
35
|
+
};
|
|
13
36
|
export const renderFileViewerMarkdown = (buffer, target, type, context) => import('./largeText.js').then(async ({ default: renderLargeText, shouldVirtualizeMarkdownBuffer }) => {
|
|
14
37
|
if (shouldVirtualizeMarkdownBuffer(buffer, context)) {
|
|
15
38
|
return renderLargeText(buffer, target, type || 'md', context);
|
package/dist/patch.js
CHANGED
|
@@ -11,9 +11,16 @@ const patchStyle = `
|
|
|
11
11
|
.patch-body .d2h-file-header{display:flex;align-items:center;gap:8px;padding:10px 12px;border-bottom:1px solid var(--patch-border);background:#f8fafc;color:var(--patch-muted);font-weight:800}
|
|
12
12
|
.patch-body .d2h-file-name-wrapper{display:flex;min-width:0;align-items:center;gap:8px}
|
|
13
13
|
.patch-body .d2h-file-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
14
|
+
.patch-body .d2h-files-diff{display:flex}
|
|
15
|
+
.patch-body .d2h-file-side-diff{flex:1 1 0;min-width:0;overflow-x:auto}
|
|
14
16
|
.patch-body .d2h-diff-table{width:100%;border-collapse:collapse;font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,'Liberation Mono',monospace;font-size:1em;line-height:1.58}
|
|
15
|
-
.patch-body .d2h-
|
|
16
|
-
.patch-body .d2h-code-side-
|
|
17
|
+
.patch-body .d2h-diff-table td{padding:0;vertical-align:top}
|
|
18
|
+
.patch-body td.d2h-code-side-linenumber,.patch-body td.d2h-code-linenumber{box-sizing:border-box;width:4.5em;min-width:4.5em;padding:0 8px;border-right:1px solid var(--patch-border);color:var(--patch-muted);text-align:right;white-space:nowrap;user-select:none}
|
|
19
|
+
/* Ignore template whitespace; only source spans preserve indentation. Long lines scroll without changing matched row heights. */
|
|
20
|
+
.patch-body .d2h-code-side-line,.patch-body .d2h-code-line{display:flex;min-height:1.58em;padding:0 10px;white-space:nowrap;word-break:normal;overflow-wrap:normal}
|
|
21
|
+
.patch-body .d2h-code-line-prefix{flex:0 0 1ch;white-space:pre;user-select:none}
|
|
22
|
+
.patch-body .d2h-code-line-ctn{display:block;flex:0 0 auto;white-space:pre}
|
|
23
|
+
.patch-body .d2h-emptyplaceholder{background:var(--patch-bg)}
|
|
17
24
|
.patch-body .d2h-ins{background:var(--patch-add)}
|
|
18
25
|
.patch-body .d2h-del{background:var(--patch-del)}
|
|
19
26
|
.patch-body .d2h-info{background:var(--patch-info);color:var(--patch-muted)}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { FileViewerXmlProfile, FileViewerXmlManifest, FileViewerXmlOptions, FileViewerXmlDiagnostic, FileViewerXmlDiagnosticCode } from '@file-viewer/core';
|
|
2
|
+
export type { FileViewerXmlRenderedInstance } from './xml.js';
|
|
3
|
+
/** Register the built-in WASM implementation. Loading and execution remain lazy. */
|
|
4
|
+
export declare function enableFileViewerXmlProfiles(): () => void;
|
package/dist/xml.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type FileRenderContext, type FileViewerRenderedInstance, type FileViewerXmlDiagnostic } from '@file-viewer/core';
|
|
2
|
+
export type FileViewerXmlRenderedInstance = FileViewerRenderedInstance & {
|
|
3
|
+
xml: {
|
|
4
|
+
readonly diagnostics: readonly FileViewerXmlDiagnostic[];
|
|
5
|
+
readonly profileId: string | undefined;
|
|
6
|
+
readonly view: 'source' | 'rendered';
|
|
7
|
+
setView(view: 'source' | 'rendered'): void;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export default function renderXml(buffer: ArrayBuffer, target: HTMLDivElement, context?: FileRenderContext): Promise<FileViewerXmlRenderedInstance>;
|
package/dist/xml.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { decodeFileViewerTextBuffer, disposeFileViewerRendered } from '@file-viewer/core';
|
|
2
|
+
import renderCode from './code.js';
|
|
3
|
+
import { createHtmlPreviewDocument } from './html.js';
|
|
4
|
+
import { applyXmlProfiles } from './xmlProfiles.js';
|
|
5
|
+
import { checkXmlAbort, XmlProfileError } from './xmlSafety.js';
|
|
6
|
+
export default async function renderXml(buffer, target, context) {
|
|
7
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
8
|
+
const options = (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.xml) !== null && _b !== void 0 ? _b : {};
|
|
9
|
+
const doc = target.ownerDocument;
|
|
10
|
+
const controller = new AbortController();
|
|
11
|
+
const signal = controller.signal;
|
|
12
|
+
const diagnostics = [];
|
|
13
|
+
const root = doc.createElement('div');
|
|
14
|
+
root.className = 'xml-profile-viewer';
|
|
15
|
+
root.dataset.xmlView = 'source';
|
|
16
|
+
const style = doc.createElement('style');
|
|
17
|
+
style.textContent = `
|
|
18
|
+
.xml-profile-viewer{height:100%;min-height:0;display:grid;grid-template-rows:auto minmax(0,1fr) auto;grid-template-columns:minmax(0,1fr)}
|
|
19
|
+
.xml-profile-controls{grid-row:1;display:flex;align-items:center;gap:8px;flex-wrap:wrap;padding:6px 10px;background:var(--file-viewer-panel,#f8fafc);color:var(--file-viewer-text,#172033);border-bottom:1px solid #dbe2e8}
|
|
20
|
+
.xml-profile-controls button{border:1px solid #94a3b8;border-radius:6px;min-height:36px;padding:4px 10px;background:transparent;color:inherit;cursor:pointer}
|
|
21
|
+
.xml-profile-controls button[aria-pressed="true"]{background:#0f766e;color:white;border-color:#0f766e}
|
|
22
|
+
.xml-profile-source,.xml-profile-rendered{grid-row:2;grid-column:1;min-height:0;height:100%;width:100%;border:0}
|
|
23
|
+
.xml-profile-source{position:relative;z-index:1;overflow:auto;background:var(--file-viewer-panel,#fff)}.xml-profile-rendered{background:white}
|
|
24
|
+
.xml-profile-diagnostics{grid-row:3;font-size:12px;max-height:140px;overflow:auto;padding:4px 10px;white-space:pre-wrap;overflow-wrap:anywhere}
|
|
25
|
+
.xml-profile-viewer [hidden]{display:none!important}
|
|
26
|
+
@media(max-width:600px){.xml-profile-controls button{min-height:44px}}
|
|
27
|
+
`;
|
|
28
|
+
const controls = doc.createElement('div');
|
|
29
|
+
controls.className = 'xml-profile-controls';
|
|
30
|
+
controls.hidden = true;
|
|
31
|
+
const sourceButton = doc.createElement('button');
|
|
32
|
+
sourceButton.type = 'button';
|
|
33
|
+
sourceButton.dataset.xmlView = 'source';
|
|
34
|
+
sourceButton.textContent = (_d = (_c = options.labels) === null || _c === void 0 ? void 0 : _c.viewSource) !== null && _d !== void 0 ? _d : 'View Source';
|
|
35
|
+
const renderedButton = doc.createElement('button');
|
|
36
|
+
renderedButton.type = 'button';
|
|
37
|
+
renderedButton.dataset.xmlView = 'rendered';
|
|
38
|
+
renderedButton.textContent = (_f = (_e = options.labels) === null || _e === void 0 ? void 0 : _e.viewRendered) !== null && _f !== void 0 ? _f : 'View Rendered';
|
|
39
|
+
controls.append(renderedButton, sourceButton);
|
|
40
|
+
const sourceTarget = doc.createElement('div');
|
|
41
|
+
sourceTarget.className = 'xml-profile-source';
|
|
42
|
+
const details = doc.createElement('details');
|
|
43
|
+
details.className = 'xml-profile-diagnostics';
|
|
44
|
+
details.hidden = true;
|
|
45
|
+
const summary = doc.createElement('summary');
|
|
46
|
+
summary.textContent = (_h = (_g = options.labels) === null || _g === void 0 ? void 0 : _g.diagnostics) !== null && _h !== void 0 ? _h : 'XML profile diagnostics';
|
|
47
|
+
const diagnosticText = doc.createElement('div');
|
|
48
|
+
details.append(summary, diagnosticText);
|
|
49
|
+
root.append(controls, sourceTarget, details);
|
|
50
|
+
target.replaceChildren(style, root);
|
|
51
|
+
let disposed = false;
|
|
52
|
+
let sourceInstance;
|
|
53
|
+
let frame;
|
|
54
|
+
let profileId;
|
|
55
|
+
let view = 'source';
|
|
56
|
+
const setView = (next) => {
|
|
57
|
+
if (disposed || !frame || !profileId)
|
|
58
|
+
return;
|
|
59
|
+
view = next;
|
|
60
|
+
root.dataset.xmlView = next;
|
|
61
|
+
// Keep the sandbox's layout alive while the opaque source layer covers it.
|
|
62
|
+
// Hiding an out-of-process iframe can leave a blank compositor surface on return.
|
|
63
|
+
frame.inert = next !== 'rendered';
|
|
64
|
+
frame.setAttribute('aria-hidden', String(next !== 'rendered'));
|
|
65
|
+
frame.style.pointerEvents = next === 'rendered' ? '' : 'none';
|
|
66
|
+
sourceTarget.hidden = next !== 'source';
|
|
67
|
+
sourceButton.setAttribute('aria-pressed', String(next === 'source'));
|
|
68
|
+
renderedButton.setAttribute('aria-pressed', String(next === 'rendered'));
|
|
69
|
+
};
|
|
70
|
+
const showSource = () => setView('source');
|
|
71
|
+
const showRendered = () => setView('rendered');
|
|
72
|
+
sourceButton.addEventListener('click', showSource);
|
|
73
|
+
renderedButton.addEventListener('click', showRendered);
|
|
74
|
+
const unmount = () => {
|
|
75
|
+
var _a;
|
|
76
|
+
if (disposed)
|
|
77
|
+
return;
|
|
78
|
+
disposed = true;
|
|
79
|
+
controller.abort(new XmlProfileError('cancelled', 'XML profile processing was cancelled.'));
|
|
80
|
+
(_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', unmount);
|
|
81
|
+
sourceButton.removeEventListener('click', showSource);
|
|
82
|
+
renderedButton.removeEventListener('click', showRendered);
|
|
83
|
+
void disposeFileViewerRendered(sourceInstance);
|
|
84
|
+
sourceInstance = undefined;
|
|
85
|
+
frame === null || frame === void 0 ? void 0 : frame.removeAttribute('srcdoc');
|
|
86
|
+
frame === null || frame === void 0 ? void 0 : frame.remove();
|
|
87
|
+
frame = undefined;
|
|
88
|
+
// Only remove this render's DOM; a later render may already own the target.
|
|
89
|
+
root.remove();
|
|
90
|
+
root.replaceChildren();
|
|
91
|
+
style.remove();
|
|
92
|
+
};
|
|
93
|
+
(_j = context === null || context === void 0 ? void 0 : context.signal) === null || _j === void 0 ? void 0 : _j.addEventListener('abort', unmount, { once: true });
|
|
94
|
+
if ((_k = context === null || context === void 0 ? void 0 : context.signal) === null || _k === void 0 ? void 0 : _k.aborted)
|
|
95
|
+
unmount();
|
|
96
|
+
const instance = {
|
|
97
|
+
$el: target, unmount,
|
|
98
|
+
xml: {
|
|
99
|
+
get diagnostics() { return diagnostics.map(item => ({ ...item })); },
|
|
100
|
+
get profileId() { return profileId; },
|
|
101
|
+
get view() { return view; },
|
|
102
|
+
setView
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const emit = (diagnostic) => {
|
|
106
|
+
var _a, _b, _c;
|
|
107
|
+
if (disposed)
|
|
108
|
+
return;
|
|
109
|
+
const safe = { ...diagnostic, message: diagnostic.message.slice(0, 4096) };
|
|
110
|
+
diagnostics.push(safe);
|
|
111
|
+
diagnosticText.textContent = diagnostics.map(item => `${item.code}${item.profileId ? ` (${item.profileId})` : ''}: ${item.message}`).join('\n');
|
|
112
|
+
details.hidden = false;
|
|
113
|
+
try {
|
|
114
|
+
(_a = options.onDiagnostic) === null || _a === void 0 ? void 0 : _a.call(options, { ...safe });
|
|
115
|
+
}
|
|
116
|
+
catch { /* Host callbacks do not control preview. */ }
|
|
117
|
+
try {
|
|
118
|
+
(_c = (_b = context === null || context === void 0 ? void 0 : context.options) === null || _b === void 0 ? void 0 : _b.onDiagnostic) === null || _c === void 0 ? void 0 : _c.call(_b, {
|
|
119
|
+
code: `xml-profile-${safe.code}`,
|
|
120
|
+
level: ['profile-selected', 'root-mismatch', 'no-match'].includes(safe.code) ? 'info' : 'warning',
|
|
121
|
+
message: safe.message, detail: { profileId: safe.profileId }
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch { /* Host callbacks do not control preview. */ }
|
|
125
|
+
};
|
|
126
|
+
let timeout;
|
|
127
|
+
try {
|
|
128
|
+
checkXmlAbort(signal);
|
|
129
|
+
// Pretty printing would change the displayed source. Pass the exact input
|
|
130
|
+
// bytes to the existing renderer, with only that display option disabled.
|
|
131
|
+
sourceInstance = await renderCode(buffer, sourceTarget, 'xml', {
|
|
132
|
+
...context, signal,
|
|
133
|
+
options: { ...context === null || context === void 0 ? void 0 : context.options, text: { ...(_l = context === null || context === void 0 ? void 0 : context.options) === null || _l === void 0 ? void 0 : _l.text, prettyPrint: false } }
|
|
134
|
+
});
|
|
135
|
+
if (disposed) {
|
|
136
|
+
void disposeFileViewerRendered(sourceInstance);
|
|
137
|
+
return instance;
|
|
138
|
+
}
|
|
139
|
+
const milliseconds = (_m = options.timeoutMs) !== null && _m !== void 0 ? _m : 15000;
|
|
140
|
+
if (!Number.isFinite(milliseconds) || milliseconds < 1)
|
|
141
|
+
throw new XmlProfileError('invalid-manifest', 'XML timeout must be a positive number.');
|
|
142
|
+
timeout = setTimeout(() => controller.abort(new XmlProfileError('timeout', 'XML profile processing timed out.')), Math.min(milliseconds, 60000));
|
|
143
|
+
const source = decodeFileViewerTextBuffer(buffer, (_p = (_o = context === null || context === void 0 ? void 0 : context.options) === null || _o === void 0 ? void 0 : _o.text) === null || _p === void 0 ? void 0 : _p.encoding).text;
|
|
144
|
+
const result = await applyXmlProfiles(source, buffer.byteLength, options, doc, signal, emit);
|
|
145
|
+
checkXmlAbort(signal);
|
|
146
|
+
if (result && !disposed) {
|
|
147
|
+
const html = createHtmlPreviewDocument(doc, result.html);
|
|
148
|
+
frame = doc.createElement('iframe');
|
|
149
|
+
frame.className = 'xml-profile-rendered';
|
|
150
|
+
frame.title = (_r = (_q = options.labels) === null || _q === void 0 ? void 0 : _q.viewRendered) !== null && _r !== void 0 ? _r : 'Rendered XML';
|
|
151
|
+
frame.setAttribute('sandbox', '');
|
|
152
|
+
frame.referrerPolicy = 'no-referrer';
|
|
153
|
+
frame.srcdoc = html;
|
|
154
|
+
root.insertBefore(frame, sourceTarget);
|
|
155
|
+
profileId = result.profileId;
|
|
156
|
+
root.dataset.xmlProfile = profileId;
|
|
157
|
+
controls.hidden = false;
|
|
158
|
+
setView((_s = options.initialView) !== null && _s !== void 0 ? _s : 'rendered');
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
if (!disposed) {
|
|
163
|
+
const failure = signal.aborted && signal.reason instanceof XmlProfileError ? signal.reason : error;
|
|
164
|
+
emit(failure instanceof XmlProfileError
|
|
165
|
+
? { code: failure.code, message: failure.message }
|
|
166
|
+
: { code: 'resource-error', message: failure instanceof Error ? failure.message : 'XML profiles could not be loaded.' });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
if (timeout !== undefined)
|
|
171
|
+
clearTimeout(timeout);
|
|
172
|
+
}
|
|
173
|
+
return instance;
|
|
174
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
interface XmlEngineResult {
|
|
2
|
+
valid?: boolean;
|
|
3
|
+
html?: string;
|
|
4
|
+
message?: string;
|
|
5
|
+
error?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function validateXmlWithWasm(source: string, wasm: Uint8Array, xml: string, schema: string, signal: AbortSignal): Promise<XmlEngineResult>;
|
|
8
|
+
export declare function transformXmlWithWasm(source: string, xml: string, stylesheet: string, maxOutputBytes: number, signal: AbortSignal): Promise<XmlEngineResult>;
|
|
9
|
+
export {};
|