The preview showed nothing but the bare word `forbidden` in DSH Desktop. The cause is not this plugin's routes but the host, one layer above them: `DesktopWebServer` wraps EVERY route with `permits()`, and in the default isolated/扩展 mode `ordinaryBrowserEnabled` is false, so a request is served only when it carries the Electron renderer capability (`x-dsh-desktop-renderer`). That header is attached solely to MAIN-frame and SAME-ORIGIN-subframe traffic (`requestComesFromRendererOrigin`), and a frame sandboxed WITHOUT allow-same-origin is an opaque origin at request time — so the preview document request could never be served, while every other sidebar route (all main-frame) worked fine. The fix keeps the sandbox boundary exactly where it was and moves the delivery: the document is fetched by the MAIN frame — which the host does trust — and handed to the frame through `srcdoc`. New shared component HtmlPreviewFrame owns that path plus the sandbox tokens for BOTH surfaces (the editor viewer and the changes tab's op render preview, which had no escape hatch at all), so the two can no longer drift; a failed fetch falls back to handing the frame the route URL, leaving the route's own answer visible instead of a blank pane. `withPreviewBase` injects `<base href="<the file's route directory>">`: a srcdoc document has no URL of its own, so without it `./style.css` would resolve against the GUI page. A document that declares its own base is left alone. Two consequences, both deliberate: - sandboxed: the frame stays opaque, so a self-contained document previews completely while a multi-file one shows its markup with the relative assets missing (those requests are opaque-origin too — inlining them is a separate job); - unlocked (the warned per-surface unlock, or the setting): the srcdoc frame inherits the GUI origin — which is what that escape hatch already meant — and there relative references resolve and load like any other same-origin request, so multi-file previews work in that mode. Verified: typecheck clean; build clean; 1364 passed / 9 skipped (130 files, +13: the pure base-rewrite spec, the delivery spec, and the updated sandbox-contract assertions, which now pin srcdoc + no route src instead of the old route-src shape).
52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
/**
|
||
* HTML preview vocabulary tests: the srcdoc delivery path needs the previewed
|
||
* document's relative references to resolve back into its own
|
||
* `/sidebar/html/…` route (a srcdoc document has no URL of its own — the
|
||
* browser would otherwise resolve `./style.css` against the GUI page).
|
||
*
|
||
* Pure module, plain node environment.
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import { previewBaseOf, withPreviewBase } from '../src/client/html-preview.ts'
|
||
|
||
const URL_ = '/sidebar/html/s1/p/a/index.html'
|
||
|
||
describe('previewBaseOf', () => {
|
||
it('keeps everything up to the last slash', () => {
|
||
expect(previewBaseOf(URL_)).toBe('/sidebar/html/s1/p/a/')
|
||
})
|
||
|
||
it('keeps the UNC double slash intact (the route encodes it that way)', () => {
|
||
expect(previewBaseOf('/sidebar/html/s1//server/share/a.html')).toBe('/sidebar/html/s1//server/share/')
|
||
})
|
||
|
||
it('returns the input unchanged when it has no slash', () => {
|
||
expect(previewBaseOf('s1')).toBe('s1')
|
||
})
|
||
})
|
||
|
||
describe('withPreviewBase', () => {
|
||
it('injects the base as the first child of <head>, ahead of the document’s own elements', () => {
|
||
const out = withPreviewBase('<html><head><title>t</title></head><body></body></html>', URL_)
|
||
expect(out).toBe('<html><head><base href="/sidebar/html/s1/p/a/"><title>t</title></head><body></body></html>')
|
||
})
|
||
|
||
it('falls back to <html> when the document declares no head', () => {
|
||
const out = withPreviewBase('<html><body>x</body></html>', URL_)
|
||
expect(out).toBe('<html><base href="/sidebar/html/s1/p/a/"><body>x</body></html>')
|
||
})
|
||
|
||
it('prepends for a headless fragment', () => {
|
||
expect(withPreviewBase('<h1>x</h1>', URL_)).toBe('<base href="/sidebar/html/s1/p/a/"><h1>x</h1>')
|
||
})
|
||
|
||
it('leaves a document that declares its own base alone (the author owns it)', () => {
|
||
const doc = '<head><base href="https://example.com/"></head>'
|
||
expect(withPreviewBase(doc, URL_)).toBe(doc)
|
||
})
|
||
|
||
it('quotes the injected href', () => {
|
||
expect(withPreviewBase('<head></head>', '/x/"y/')).toBe('<head><base href="/x/"y/"></head>')
|
||
})
|
||
})
|