{"record":{"id":"cfb614aab933d430","repo":"DioxusLabs/dioxus","slug":"should-have-access-to-the-document","errorCode":null,"errorMessage":"should have access to the Document","messagePattern":"should have access to the Document","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/web/src/events/mod.rs","lineNumber":286,"sourceCode":"}\n\n// todo: some of these events are being casted to the wrong event type.\n// We need tests that simulate clicks/etc and make sure every event type works.\npub(crate) fn virtual_event_from_websys_event(\n    event: web_sys::Event,\n    target: Element,\n) -> PlatformEventData {\n    PlatformEventData::new(Box::new(GenericWebSysEvent {\n        raw: event,\n        element: target,\n    }))\n}\n\npub(crate) fn load_document() -> Document {\n    web_sys::window()\n        .expect(\"should have access to the Window\")\n        .document()\n        .expect(\"should have access to the Document\")\n}\n","sourceCodeStart":268,"sourceCodeEnd":288,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/web/src/events/mod.rs#L268-L288","documentation":"Once the Window is available, `load_document` calls `.document().expect(\"should have access to the Document\")`. The HTML spec permits a Window whose document is null (detached browsing contexts), and stubbed test environments may provide a window without a document; in those cases this second expect panics. If the Window itself were missing, the sibling error at line 284 fires first.","triggerScenarios":"A window global exists but its document is null: JS test harnesses mocking `window` without `document`, event code running during document teardown/unload races, or exotic embedding contexts that provide a bare Window.","commonSituations":"jsdom-style mocks in tooling that intercept wasm-bindgen globals; pages being torn down while deferred event code still executes.","solutions":["Run the renderer in a real browser page rather than a mocked window global","Defer renderer/event work until DOMContentLoaded so a document certainly exists","Fix test environment stubs to provide both `window` and `document`","Cancel pending event work on teardown so it cannot race document detach"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn has_document() -> bool {\n    web_sys::window().and_then(|w| w.document()).is_some()\n}","tryCatchPattern":null,"preventionTips":["Run the renderer in a real browser page, not a mocked window global","Defer renderer/event initialization until DOMContentLoaded","If you stub browser globals in tests, provide both window and document","Cancel deferred event work during teardown so it cannot race document detach"],"tags":["dioxus-web","wasm","document","environment","panic"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}