fix(toc): expose tableOfContents on globalThis#188
Merged
JohnMcLear merged 2 commits intomasterfrom Apr 23, 2026
Merged
Conversation
Member
Author
|
/review |
…each it Top-level `const` in a <script> tag is script-scoped, not a global. `postAceInit.js` and `aceEditEvent.js` are loaded by Etherpad's plugin framework as CommonJS-style modules in their own scopes, so their bare `tableOfContents` identifier resolves via the scope chain up to globalThis — which never had the object attached. On Firefox the resulting `Uncaught (in promise) ReferenceError: tableOfContents is not defined` is swallowed silently as an unhandled promise rejection, so the pad appears to work. On Chromium it surfaces loudly and blocks pad initialization. Fix: assign to `globalThis.tableOfContents` alongside the `const` declaration. `globalThis` resolves to `window` in the browser and to `global` in Node.js, so the unit tests under `tests/` (which evaluate toc.js inside a Node vm.runInNewContext) keep working — `window` would have broken them.
…copes The toc.js fix alone was not enough. `aceEditEvent` fires inside ACE's inner iframe and `postAceInit` is loaded as a CommonJS plugin hook module with its own scope — in both cases `globalThis` is not shared with the <script> tag that loads toc.js, so the bare `tableOfContents` identifier still threw ReferenceError on Chromium. Both hook modules now look up the object via a small `getToc()` helper that tries globalThis, then window.top, then window — so it works regardless of which realm the hook happens to run in. If the lookup fails (e.g. toc.js has not executed yet), the hook silently no-ops and the next invocation picks it up. Verified live in Chromium after this change: no ReferenceError in the console, pad initializes cleanly, TOC toggle works. Firefox continues to work (it was only silent about the bug, not immune to it).
f4d651d to
950cd5f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
postAceInit.jsfails on pad load in Chromium with:The error blocks subsequent pad initialization. Firefox swallows it as an unhandled promise rejection, so the bug went undetected there — which is why it shipped without CI catching it.
Root cause
static/js/toc.jsdeclaresconst tableOfContents = {...}at the top level of a regular<script>tag. Top-levelconstis script-scoped, not a global — it's not attached towindow.postAceInit.jsandaceEditEvent.jsare loaded by Etherpad's plugin framework as CommonJS-style modules in their own scopes, so their baretableOfContentsidentifier resolves via the scope chain up toglobalThis— which never had the object attached.Fix
One-line change: assign to
globalThis.tableOfContentsalongside theconstdeclaration.globalThisresolves towindowin the browser and toglobalin Node.js, so the unit tests undertests/(which evaluate toc.js inside a Nodevm.runInNewContext) keep working — usingwindowinstead would have broken them.Test plan
npm testpasses locally (6/6)npm run lint— no new errors instatic/js/toc.js🤖 Generated with Claude Code