pub const KEYS_JS: &str = "// Document key listeners. Run: `cargo test -p zdc-runtime`\n//\n// Its own module rather than four lines in `dom.js`, for the reason\n// `list.js`, `markup.js` and `foreign.js` are their own modules: a program\n// that writes no `on key` must not download this (spec \u{a7}16.3.1). It imports\n// `signal.js` and nothing else \u{2014} it needs a listener and a focus question,\n// not a node to render into, so a program whose only DOM work is a shortcut\n// does not link the renderer.\n//\n// # What this file is actually for\n//\n// `document.addEventListener(\'keydown\', \u{2026})` is a strictly larger capability\n// than a listener on one element: it receives keystrokes aimed at *every*\n// element on the page, including a field the program never declared. A\n// password manager\'s iframe, a `Prose` block holding markup somebody else\n// wrote, a third-party embed \u{2014} the program did not put those characters on\n// the screen and has no business reading them.\n//\n// So the capability is narrowed here, where it is created, rather than\n// labelled after the fact:\n//\n// 1. **The program named its key.** `onKey` compares and returns; a key\n// the program did not name reaches nothing. The compiler emits the\n// literal, so the set of observable keys is written in the source.\n// 2. **No editable element had focus.** `isEditable` is what makes (1)\n// safe for a printable key \u{2014} `on key \"r\"` cannot see the `r` in a\n// password, because while that password field has focus this listener\n// stands down.\n//\n// Together: a handler learns only that the key it named itself was pressed\n// while nobody was typing into anything. There is no payload, so there is\n// nothing else to learn, and there is no argument about what to label.\n//\n// # The listener is removed\n//\n// `dom.js`\'s `on` never detaches, and it is right not to: the node it is\n// attached to is what gets removed. A document listener has no such node.\n// One left behind is a leak *and* a correctness bug \u{2014} it keeps firing into\n// a graph whose signals nothing renders any more. `onCleanup` is registered\n// against whatever `owned` scope is open, which is the branch closure\n// `ifInto`, `whenInto` and `eachInto` already build and already dispose.\n\nimport { batch, onCleanup } from \'./signal.js\';\n\n/**\n * Whether a keystroke aimed at `target` is somebody typing into something.\n *\n * Conservative by construction: the question asked is \"could this element\n * receive text\", and anything unrecognised is treated as if it could not,\n * only because an unrecognised element cannot receive text either \u{2014} every\n * text-receiving element in HTML is one of these three cases.\n *\n * `type` is not consulted. A `<input type=\"checkbox\">` receives no\n * characters, so suppressing there costs a shortcut and protects nothing \u{2014}\n * but reading `type` to allow it would be a list of the input types that\n * are safe, and that list fails open the day a new one is added.\n */\nfunction isEditable(target) {\n if (!target || typeof target !== \'object\') return false;\n if (target.isContentEditable) return true;\n const tag = typeof target.tagName === \'string\' ? target.tagName.toLowerCase() : \'\';\n return tag === \'input\' || tag === \'textarea\' || tag === \'select\';\n}\n\n/**\n * Run `handler` when `key` is pressed and nobody is typing.\n *\n * `key` is compared against `KeyboardEvent.key` exactly, which is why the\n * compiler checks the literal against a closed table: `\"Esc\"` is a listener\n * that never fires, and a browser reports that as silence.\n *\n * Batched and contained for the reasons `dom.js`\'s `on` is: several writes\n * in one handler repaint once, and a throwing handler must not take the\n * page\'s other listeners with it.\n */\nexport function onKey(key, handler) {\n const listener = (event) => {\n if (event.key !== key) return;\n if (isEditable(event.target)) return;\n try {\n // No argument, and that is not an oversight. The grammar has no\n // binder, so nothing emitted can read one \u{2014} passing the event anyway\n // would leave a live channel one lowering change away from being\n // reachable. The event does not leave this function.\n batch(() => handler());\n } catch (failure) {\n reportError(failure);\n }\n };\n document.addEventListener(\'keydown\', listener);\n onCleanup(() => document.removeEventListener(\'keydown\', listener));\n}\n";Expand description
Document key listeners: on key "Escape".
Its own module for the reason the modules above are: a program that
writes no on key must not download it (§16.3.1).
It imports signal.js and nothing else — it needs a listener and a
focus question rather than a node to render into.