chat: a focused feel keeps its focus across a refresh
Final review M1 (Astra 5). With a taste card's feel checkbox
focused, the refresh's focus restore called setSelectionRange on the
checkbox, which throws InvalidStateError and aborted the rest of the
refresh (the refresh's catch retried it); and every feel shares one
name, so focus could land on the first feel.
The focus identity now includes a checkbox's (or radio's) value, and
text selection is restored only on a control that has one.
Test (RED first, browser):
go test -tags browser ./internal/equipo/ -run TestBrowserTasteCardDrafts -count=1
(new case: the second feel focused during a refresh; the page
records any throw from setSelectionRange and unhandled rejections)
RED: "focus after a refresh with the second feel focused: \"Calm\""
GREEN: ok; also -run 'TestBrowser.*Card|TestBrowserDirection'
Mutations (each restored):
setSelectionRange on any control -> "script errors:
InvalidStateError: ... ('checkbox')
does not support selection." FAIL
focus matched by key only -> focus lands on "Calm" FAIL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+16
−4
internal/equipo/static/chat-stream.js+5 −3internal/equipo/taste_card_browser_test.go+11 −1
diff --git a/internal/equipo/static/chat-stream.js b/internal/equipo/static/chat-stream.js| index 9140a3d5..d79847aa 100644 |
| --- a/internal/equipo/static/chat-stream.js |
| +++ b/internal/equipo/static/chat-stream.js |
| @@ -836,7 +836,9 @@ |
| // A newly committed reply settles into its draft's element; a turn |
| // may already have earlier messages on the page. |
| var focused=document.activeElement, focusedCard=focused && focused.closest("[data-card]"); |
| - var cardFocus=focusedCard && /^(TEXTAREA|INPUT)$/.test(focused.tagName) ? {id:focusedCard.dataset.card,name:focused.name,key:draftKey(focused),start:focused.selectionStart,end:focused.selectionEnd} : null; |
| + // A ticked box (a taste card's feels) is its key and its value: |
| + // every feel shares one name. Only a text field has a selection. |
| + var cardFocus=focusedCard && /^(TEXTAREA|INPUT)$/.test(focused.tagName) ? {id:focusedCard.dataset.card,name:focused.name,key:draftKey(focused),value:/^(checkbox|radio)$/.test(focused.type)?focused.value:null,start:focused.selectionStart,end:focused.selectionEnd} : null; |
| var keep = {}, shown = {}, opened = {}; |
| feed.querySelectorAll("[equipo-thinking]:not([data-optimistic])").forEach(function (el) { keep[el.dataset.turn] = el; }); |
| // A receipt the person opened with ⋯ stays open. |
| @@ -913,8 +915,8 @@ |
| arrange(items.concat(pending)); |
| if (window.equipoPulse) window.equipoPulse(feed); |
| if(cardFocus) { |
| - var target=cardFocus.key===cardFocus.name ? feed.querySelector('[data-card="'+CSS.escape(cardFocus.id)+'"] [name="'+CSS.escape(cardFocus.name)+'"]') : Array.prototype.find.call(feed.querySelectorAll('[data-card="'+CSS.escape(cardFocus.id)+'"] textarea, [data-card="'+CSS.escape(cardFocus.id)+'"] input:not([type=hidden])'), function (el) { return draftKey(el)===cardFocus.key; }); |
| - if(target) {target.focus({preventScroll:true});if(target.setSelectionRange)target.setSelectionRange(cardFocus.start,cardFocus.end);} |
| + var target=cardFocus.key===cardFocus.name && cardFocus.value===null ? feed.querySelector('[data-card="'+CSS.escape(cardFocus.id)+'"] [name="'+CSS.escape(cardFocus.name)+'"]') : Array.prototype.find.call(feed.querySelectorAll('[data-card="'+CSS.escape(cardFocus.id)+'"] textarea, [data-card="'+CSS.escape(cardFocus.id)+'"] input:not([type=hidden])'), function (el) { return draftKey(el)===cardFocus.key && (cardFocus.value===null || el.value===cardFocus.value); }); |
| + if(target) {target.focus({preventScroll:true});if(cardFocus.start!==null && typeof target.selectionStart==="number")target.setSelectionRange(cardFocus.start,cardFocus.end);} |
| } |
| local.forEach(function (el) { place(el); }); |
| Object.keys(drafts).forEach(function (turn) { |
diff --git a/internal/equipo/taste_card_browser_test.go b/internal/equipo/taste_card_browser_test.go| index 7123aac9..53cd833f 100644 |
| --- a/internal/equipo/taste_card_browser_test.go |
| +++ b/internal/equipo/taste_card_browser_test.go |
| @@ -32,7 +32,7 @@ func TestBrowserTasteCardDrafts(t *testing.T) { |
| cursor.Store(1) |
| page := func() string { |
| return fmt.Sprintf(`<!doctype html><html lang="en"><head><meta charset="utf-8"><link rel="stylesheet" href="%s"><link rel="stylesheet" href="%s"><link rel="stylesheet" href="%s">`+ |
| - `<script>window.errors = []; addEventListener("error", e => errors.push(String(e.message)));</script><script defer src="%s"></script><script defer src="%s"></script></head><body>`+ |
| + `<script>window.errors = []; addEventListener("error", e => errors.push(String(e.message))); addEventListener("unhandledrejection", e => errors.push(String(e.reason))); const ssr = HTMLInputElement.prototype.setSelectionRange; HTMLInputElement.prototype.setSelectionRange = function () { try { return ssr.apply(this, arguments); } catch (e) { errors.push(String(e)); throw e; } };</script><script defer src="%s"></script><script defer src="%s"></script></head><body>`+ |
| `<div equipo-conversation data-chat-stream data-cursor="%d" data-card-busy="Busy"><div equipo-scroll><ol equipo-feed>%s<li data-card="c2"><form action="/elsewhere"><textarea name="note"></textarea></form></li></ol></div><form id="composer"><div id="chat-composer"></div><textarea name="text"></textarea></form></div></body></html>`, |
| assets.Path("static/tokens.css"), assets.Path("static/theme.css"), assets.Path("static/equipo.css"), assets.Path("static/stardust.js"), assets.Path("static/chat-stream.js"), cursor.Load(), card.String()) |
| } |
| @@ -113,6 +113,16 @@ func TestBrowserTasteCardDrafts(t *testing.T) { |
| if got := state(); got != "false false " { |
| t.Fatalf("after unticking and clearing: %q", got) |
| } |
| + // Final review M1: the second feel focused during a refresh keeps |
| + // the focus (each feel is its own field) and the refresh finishes |
| + // without an error (a checkbox has no text selection to restore: the |
| + // page records a throw from setSelectionRange, which the refresh's |
| + // own catch would otherwise swallow, retrying). |
| + eval(`(` + feel("Bold") + `.focus(), "")`) |
| + refresh() |
| + if got := eval(`document.activeElement === ` + feel("Bold") + ` ? "bold" : String(document.activeElement && (document.activeElement.value || document.activeElement.tagName))`); got != "bold" { |
| + t.Fatalf("focus after a refresh with the second feel focused: %q", got) |
| + } |
| if got := eval(`errors.join(";")`); got != "" { |
| t.Errorf("script errors: %s", got) |
| } |