Skip to content

Fix %jigsaw cross-origin errors and restore button functionality#375

Merged
blink1073 merged 3 commits intoCalysto:mainfrom
blink1073:fix-170
Mar 14, 2026
Merged

Fix %jigsaw cross-origin errors and restore button functionality#375
blink1073 merged 3 commits intoCalysto:mainfrom
blink1073:fix-170

Conversation

@blink1073
Copy link
Copy Markdown
Contributor

@blink1073 blink1073 commented Mar 14, 2026

Closes #176, #199, #170, #166, #165

Summary

  • Replace all window.parent.* direct-access calls in the downloaded iframe HTML with postMessage, fixing cross-origin/sandboxed frame errors in modern browsers
  • Restore Run, Generate Python Code, and Clear Cell Output button behaviour for both Notebook 6 and Notebook 7
  • Embed previously saved workspace XML in the HTML at magic run time so Blockly state is restored when the cell is re-executed (no XHR needed)
  • Show run output and generated code in the jigsaw cell's own output area rather than a separate cell
  • Fix Blockly code generator indentation from 2 spaces to 4 spaces
  • Create subdirectory for workspace path when it includes a path component (already fixed in Fix %jigsaw failing to save files when workspace path includes a subdirectory #374, now included here for completeness)

Technical approach

The old implementation relied on window.parent.Blockly, window.parent.IPython, and window.parent.document.* — all blocked by modern browser security for cross-origin or null-origin sandboxed frames.

The new implementation:

  1. _fix_cross_origin() rewrites the downloaded Blockly HTML to remove all window.parent.* accesses, inject saved workspace XML as a JS variable, and replace button onclick handlers with local postMessage helpers
  2. Parent-side listener (injected via Javascript()) receives execute/insert/clear messages from the iframe and handles them using the appropriate notebook API — the classic IPython.notebook OutputArea API for Notebook 6, or DOM manipulation + Jupyter REST/WebSocket for Notebook 7
  3. Cell caching stores the owning cell object at magic run time (while element is in scope) so the message handler can find it later without frameElement, which is null for cross-origin frames

Test plan

  • Run just test tests/magics/test_jigsaw_magic.py — both tests pass
  • Manually tested in Notebook 6: Run executes code and shows output in jigsaw cell, Generate shows code block, Clear removes both
  • Manually tested in Notebook 7: same behaviour via WebSocket execution and DOM output
  • Manually tested in JupyterLab: same behaviour
  • Verify workspace XML is restored after re-running the %jigsaw cell

 Calysto#176, Calysto#199, Calysto#170, Calysto#166, Calysto#165)

Replace the old same-origin window.parent.* architecture with postMessage
so the jigsaw iframe works in modern browsers with cross-origin/sandboxed
frames.  Restore Run, Generate, and Clear button behaviour for both
Notebook 6 and Notebook 7.

Key changes:
- Remove all window.parent.* direct-access calls from the downloaded
  iframe HTML (_fix_cross_origin), replacing button handlers with local
  postMessage helpers and workspace registration with inline XML
  initialisation (no XHR required).
- Embed previously saved workspace XML in the HTML at magic run time so
  the Blockly state is restored when the cell is re-executed.
- Add a parent-side message listener (postMessage) that handles execute,
  insert, and clear actions; registered once, updated on every %jigsaw run.
- Support Notebook 6 (IPython.notebook classic API) and Notebook 7
  (DOM + Jupyter REST/WebSocket fallback) in a single code path.
- All output (run results and generated code) is shown in the jigsaw
  cell's own output area rather than a separate cell.
- Fix Blockly indentation: set INDENT = '    ' (4 spaces) for all
  generators.
- Create subdirectories for workspace paths that include a subdirectory
  component.
@blink1073 blink1073 marked this pull request as draft March 14, 2026 21:09
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 14, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.82%. Comparing base (f42dd6e) to head (96ce86e).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
metakernel/magics/jigsaw_magic.py 75.00% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #375      +/-   ##
==========================================
- Coverage   90.96%   90.82%   -0.14%     
==========================================
  Files          51       51              
  Lines        2921     2943      +22     
  Branches      409      410       +1     
==========================================
+ Hits         2657     2673      +16     
- Misses        185      190       +5     
- Partials       79       80       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blink1073 blink1073 marked this pull request as ready for review March 14, 2026 22:03
@blink1073 blink1073 merged commit f33283c into Calysto:main Mar 14, 2026
25 of 27 checks passed
@blink1073 blink1073 deleted the fix-170 branch March 14, 2026 22:04
blink1073 added a commit to blink1073/calysto.github.io that referenced this pull request Mar 15, 2026
These pages are intended to replace the shims added to metakernel in
Calysto/metakernel#375
blink1073 added a commit to blink1073/metakernel that referenced this pull request Mar 15, 2026
 Calysto#196 with Calysto#375)

Adds docs/jupyterhub.md covering kernel installation, Apache/nginx reverse-proxy
config, and an explanation of the Jigsaw CORS fix. Adds two unit tests that
demonstrate the window.parent cross-origin issue and verify _fix_cross_origin
eliminates every direct property access in favour of postMessage and embedded XML.
blink1073 added a commit that referenced this pull request Mar 15, 2026
#196) (#386)

* Add JupyterHub deployment docs and cross-origin regression tests (closes #196 with #375)

Adds docs/jupyterhub.md covering kernel installation, Apache/nginx reverse-proxy
config, and an explanation of the Jigsaw CORS fix. Adds two unit tests that
demonstrate the window.parent cross-origin issue and verify _fix_cross_origin
eliminates every direct property access in favour of postMessage and embedded XML.

* Update troubleshooting version reference to >= 1.0
@blink1073 blink1073 mentioned this pull request Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

%jigsaw not showing block in workspace

1 participant