diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-28 19:55:53 +0530 |
|---|---|---|
| committer | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-02 19:12:46 +0530 |
| commit | cfa1892e17e3696da004c71a4942d5b12186b12c (patch) | |
| tree | 228b15fc7ada4193bdcabeaf913421a2958215ce /src/domains/fill-engine.ts | |
| parent | 254de089ee468795bac175d99e09ddaec4ac1469 (diff) | |
fix(domains): one fill engine, so browser_fill stops lying about checkboxes
browser_fill and browser_fill_form carried separate in-page implementations that
disagreed on the same element. The worst case was silent: browser_fill on a
checkbox fired the HTMLInputElement value setter, which writes the submit value
and leaves `checked` alone, then reported status ok — a no-op the agent had no
reason to doubt. Both tools now share one engine, with browser_fill's documented
<select> rejection kept as an explicit pre-check and per-tool focus behaviour kept
as a parameter.
Deliberate, user-approved behaviour changes:
- browser_fill on a DISABLED input now returns an error instead of success. The
old success was the bug; browser_fill_form already refused it.
- browser_fill on a checkbox/radio now toggles `checked` instead of the value.
- browser_fill_form on a contenteditable now uses execCommand("insertText"),
so it drives ProseMirror/Slack-style editors as browser_fill already did.
browser_fill keeps appending interactiveDiff and republishing refs; fill_form
still does not.
Report: docs/superpowers/reports/2026-07-26-bug-audit.md#f5
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/fill-engine.ts')
| -rw-r--r-- | src/domains/fill-engine.ts | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/domains/fill-engine.ts b/src/domains/fill-engine.ts new file mode 100644 index 0000000..32102fe --- /dev/null +++ b/src/domains/fill-engine.ts @@ -0,0 +1,95 @@ +/** + * The one in-page fill implementation, shared by browser_fill and + * browser_fill_form so the two can no longer disagree about the same element. + * + * `this` is the target element and `value` is the requested value. Two call-site + * differences are parameters rather than forks: + * rejectSelect — browser_fill refuses a <select> and points at + * browser_select_option; browser_fill_form fills it. + * focusFirst — browser_fill_form focuses the field first; browser_fill + * documents that it does not move focus. + * + * Returns a PageResult (see form.ts): { ok, reason?, kind?, value?, text?, + * checked?, changed?, options?, tag? }. + */ + +export type FillOptions = { + readonly rejectSelect: boolean; + readonly focusFirst: boolean; +}; + +const ENGINE = ` + const el = this; + if (!el || el.nodeType !== 1) return { ok: false, reason: "ref does not point to an element" }; + const tagName = el.tagName || ""; + const tag = tagName.toLowerCase(); + const type = (el.type || "").toLowerCase(); + + if (__rejectSelect && tag === "select") { + return { ok: false, reason: "element is a <select>", kind: "select", tag: tagName }; + } + if (el.disabled) return { ok: false, reason: "element is disabled", tag: tagName }; + + const fire = function (t) { el.dispatchEvent(new Event(t, { bubbles: true })); }; + if (__focusFirst) { try { el.focus(); } catch (e) {} } + + if (tag === "select") { + const want = String(value); + let matched = null; + for (let i = 0; i < el.options.length; i++) { + const o = el.options[i]; + if (o.value === want || o.label === want || o.text === want) { matched = o; break; } + } + if (!matched) { + const opts = []; + for (let i = 0; i < el.options.length; i++) opts.push({ value: el.options[i].value, text: el.options[i].text }); + return { ok: false, reason: "no matching option", kind: "select", options: opts, tag: tagName }; + } + el.value = matched.value; + fire("input"); fire("change"); + return { ok: true, kind: "select", value: el.value, text: matched.text, tag: tagName }; + } + + if (tag === "input" && (type === "checkbox" || type === "radio")) { + const want = value === true || value === "true" || value === "on" || value === 1; + let changed = false; + if (el.checked !== want) { + try { el.click(); } catch (e) {} + if (el.checked !== want) { el.checked = want; fire("input"); fire("change"); } + changed = true; + } + return { ok: true, kind: type, checked: el.checked, changed: changed, tag: tagName }; + } + + if (el.isContentEditable) { + try { el.focus(); } catch (e) {} + let done = false; + try { + const sel = window.getSelection(); + if (sel) { sel.removeAllRanges(); const rng = document.createRange(); rng.selectNodeContents(el); sel.addRange(rng); } + done = document.execCommand("insertText", false, String(value)); + } catch (e) { done = false; } + if (!done) { + el.textContent = String(value); + el.dispatchEvent(new InputEvent("input", { bubbles: true })); + } + return { ok: true, kind: "contenteditable", value: el.textContent || "", tag: tagName }; + } + + if (tag === "input" || tag === "textarea") { + const proto = tag === "textarea" ? window.HTMLTextAreaElement.prototype : window.HTMLInputElement.prototype; + const desc = Object.getOwnPropertyDescriptor(proto, "value"); + if (desc && desc.set) desc.set.call(el, String(value)); + else el.value = String(value); + fire("input"); fire("change"); + return { ok: true, kind: el.type || tag, value: el.value, tag: tagName }; + } + + return { ok: false, reason: "element is not a fillable field (tag=" + tag + ")", tag: tagName }; +`; + +export const fillBody = (opts: FillOptions): string => + `const __rejectSelect = ${opts.rejectSelect}; const __focusFirst = ${opts.focusFirst};\n${ENGINE}`; + +export const fillDeclaration = (opts: FillOptions): string => + `function (value) { ${fillBody(opts)} }`; |
