Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/chat-ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -675,23 +675,38 @@ body {
padding: 12px 16px 16px;
}

.composer input {
/* A textarea, because `type=text` deleted the newlines out of a paste
(issue 686). `min-width: 0` and `box-sizing` for the same reason
`.prompt-field` exists: a replaced field that cannot shrink to the space it
is given paints past it (issue 641).
`field-sizing` lets it follow its content up to `max-height` and then scroll;
where the engine has not got it the field simply stays at its one row, which
is what the input did, so the fallback is the shape shipped today. The clamp
is the point either way: the Chat surface is 420 by 560 points (`main.rs`),
and every point this field takes comes off the log above it. */
.composer textarea {
flex: 1;
min-width: 0;
box-sizing: border-box;
field-sizing: content;
max-height: calc(1.5em * 5 + 22px);
overflow-y: auto;
resize: none;
padding: 10px 13px;
border: 1px solid var(--chat-line);
border-radius: var(--chat-radius-control);
background: var(--chat-fill);
color: var(--chat-ink);
font: inherit;
font-size: 13px;
line-height: 1.5;
}

.composer input::placeholder {
.composer textarea::placeholder {
color: var(--chat-ink-4);
}

.composer input:disabled {
.composer textarea:disabled {
background: var(--chat-fill-dim);
color: var(--chat-ink-4);
}
Expand All @@ -714,7 +729,7 @@ body {
cursor: default;
}

.composer input:focus-visible,
.composer textarea:focus-visible,
.composer button:focus-visible {
outline: 2px solid var(--chat-accent);
outline-offset: 2px;
Expand Down
12 changes: 5 additions & 7 deletions src/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,11 @@ <h2>Your own layer</h2>
`chat_send`, which would leave the log showing what was never sent.
The `.take()` there stays the trust boundary's guard. -->
<form class="composer" id="composer">
<input
type="text"
id="line"
maxlength="16000"
placeholder="Nothing can answer yet"
autocomplete="off"
/>
<!-- A textarea, not an input: `type=text` value sanitization deletes every
LF and CR from the value. It rests at one row because the Chat surface
is 420 by 560 points (`main.rs`) and the log above it has no height to
spare. -->
<textarea id="line" rows="1" maxlength="16000" placeholder="Nothing can answer yet" autocomplete="off"></textarea>
<button type="submit" id="send">Send</button>
</form>

Expand Down
15 changes: 14 additions & 1 deletion src/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ if (settingsBtn) {

// Which tab is showing. The conversation and the prompt behind it are the two
// things this window holds, and they do not fit one above the other at 420
// points (ADR-0012).
// points (`main.rs`).
function showTab(name) {
const prompt = name === "prompt";
log.hidden = prompt;
Expand Down Expand Up @@ -450,6 +450,19 @@ function showWho(opening) {
}
}

// A textarea does not submit its form on Enter. Enter still sends, because that
// is the whole muscle memory of this window; Shift+Enter types the newline.
// `isComposing` is the IME's Enter — it accepts a candidate, and sending there
// would cut the word in half.
line.addEventListener("keydown", (event) => {
if (event.key !== "Enter" || event.shiftKey || event.isComposing) {
return;
}
// Otherwise the newline lands in the field as well as sending the turn.
event.preventDefault();
composer.requestSubmit();
});

composer.addEventListener("submit", (event) => {
event.preventDefault();
const text = line.value.trim();
Expand Down
102 changes: 102 additions & 0 deletions tests/chat-composer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Run with `node --test tests/`.
//
// The composer is the only way a user gets text into a turn, and for most of
// V1 it was an `<input type="text">`. HTML's value sanitization for that type
// deletes every LF and CR rather than folding them to spaces, so a pasted
// function reached the Harness as `}function next(` — every line boundary
// gone, and nothing on screen saying so (#686). The element is the bug, so
// the element is what this file pins.

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";

const html = readFileSync(new URL("../src/chat.html", import.meta.url), "utf8");
const js = readFileSync(new URL("../src/chat.js", import.meta.url), "utf8");
const css = readFileSync(new URL("../src/chat-ui.css", import.meta.url), "utf8");

function composerForm() {
const match = html.match(/<form class="composer"[^>]*>([\s\S]*?)<\/form>/);
assert.ok(match, "the composer form is missing");
return match[1];
}

test("the composer holds a value that can carry newlines", () => {
const form = composerForm();

assert.match(
form,
/<textarea\b[^>]*\bid="line"/,
"a `type=text` input strips LF and CR out of its value; only a textarea keeps them",
);
assert.doesNotMatch(
form,
/<input\b[^>]*\bid="line"/,
"the sanitizing input is gone, not merely shadowed by a textarea beside it",
);
});

// An `<input>` in a form submits on Enter for free, which is why this window
// shipped with no keydown listener at all. A textarea does not, so the send
// key is now something the Shell has to say out loud.
function keydownHandler() {
const at = js.indexOf('line.addEventListener("keydown"');
assert.notEqual(at, -1, "the composer field has no keydown listener, so Enter no longer sends");
const end = js.indexOf("\n});", at);
assert.notEqual(end, -1, "the keydown listener is not closed the way this parse expects");
return js.slice(at, end);
}

test("Enter sends and Shift+Enter keeps the newline the textarea can now hold", () => {
const handler = keydownHandler();

assert.match(handler, /event\.key\s*[!=]==\s*"Enter"/, "Enter is the send key");
assert.match(
handler,
/event\.shiftKey/,
"Shift+Enter has to fall through to the textarea; it is the only way to type a newline",
);
assert.match(handler, /event\.preventDefault\(\)/, "a sending Enter must not also insert its newline");
});

test("the send key reuses the submit path rather than opening a second one", () => {
assert.match(
keydownHandler(),
/composer\.requestSubmit\(\)/,
"requestSubmit goes through the submit listener, so the guards there still run",
);
assert.equal(
js.split('invoke("chat_send"').length - 1,
1,
"one place a turn is actually sent, so the empty-line and attach guards cannot be bypassed",
);
});

// The Chat surface is 420 by 560 points (`main.rs`) and the composer sits under
// the log, so any height the field takes is height the conversation loses. It
// rests at the single line it has always been and a paste scrolls inside it.
function ruleBlock(selector) {
const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const match = css.match(new RegExp(`${escaped}\\s*\\{([^}]+)\\}`));
assert.ok(match, `${selector} has no rule of its own`);
return match[1];
}

test("the composer field rests at one row and scrolls a paste inside itself", () => {
assert.match(composerForm(), /<textarea\b[^>]*\brows="1"/, "one row at rest, as the input was");

const field = ruleBlock(".composer textarea");
assert.match(field, /max-height:/, "a paste cannot push the log off the window");
assert.match(field, /overflow-y:\s*auto/, "it scrolls inside the field instead");
assert.match(field, /resize:\s*none/, "and there is no drag handle to take the log's room by hand");
assert.match(field, /box-sizing:\s*border-box/, "padding sits inside the width flex hands it");
assert.match(field, /min-width:\s*0/, "#641: a replaced field that cannot shrink paints past its track");
});

test("no rule is still addressed to the input the composer no longer has", () => {
assert.doesNotMatch(
css,
/\.composer input\b/,
"these stopped applying the moment the element changed",
);
});
26 changes: 21 additions & 5 deletions tests/chat-prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,35 @@ function promptSection(source) {
return match[1];
}

// Every event this window listens for, in the order it wires them.
const LISTENED = [...js.matchAll(/addEventListener\(\s*"([a-z-]+)"/g)].map(([, name]) => name);
// Every event this window listens for, as `target.event`, in the order it
// wires them.
const LISTENED = [...js.matchAll(/(\w+)\.addEventListener\(\s*"([a-z-]+)"/g)].map(
([, target, name]) => `${target}.${name}`,
);

test("nothing in the Chat surface saves on a keystroke", () => {
const typing = LISTENED.filter((name) =>
["input", "keyup", "keydown", "keypress", "change"].includes(name),
assert.ok(LISTENED.length >= 4, "the listener parse stopped matching — this cannot pass vacuously");

const typing = LISTENED.filter((wiring) =>
["input", "keyup", "keydown", "keypress", "change"].includes(wiring.split(".")[1]),
);

// The composer's send key is the one listener that may fire mid-typing: the
// field became a textarea (#686), which does not submit its form on Enter,
// so Enter had to be wired by hand. It sends a turn; it does not save.
assert.deepEqual(
typing,
[],
["line.keydown"],
`these fire while the user is still typing, and saving reopens the session: ${typing.join(", ")}`,
);

const at = js.indexOf('line.addEventListener("keydown"');
const end = js.indexOf("\n});", at);
assert.doesNotMatch(
js.slice(at, end),
/invoke\(/,
"the send key reaches the Shell only through the submit listener, never the save",
);
});

test("the save is asked for twice before the conversation is spent", () => {
Expand Down