From c75ae2f2e74c9b0e2e381b401b12a9eefe1d21a4 Mon Sep 17 00:00:00 2001 From: Wynne Pirini Date: Tue, 24 Nov 2026 11:35:07 +1300 Subject: [PATCH] feat(embed): prefill and lock the booker's name/email from attributes (#255) The embed widget builds its own form, so a host page that already knows who is booking had no way to pass that in. Two optional attributes now do: When present, the matching field is pre-filled and read-only. Both are optional and independent; absent or empty leaves the field editable exactly as before. `readonly`, not `disabled`: the value still submits and the field stays focusable for screen readers, where disabled would drop it from the tab order. This is a convenience only, and the code says so. The booking endpoint still accepts whatever name/email a request sends, so a read-only input is not proof of who booked. Verified with the reporter on #255 that the convenience case is the whole ask; verified identity would need a server-signed token and is not this. Verified in a real browser with two widgets on one page, one with the attributes and one without, form rendered in each: - with attributes: name "John Doe" readonly, email "john@doe.com" readonly - without: both empty and editable, placeholders unchanged - both still required and submitting through the same code path Test: TestEmbedJS_identityPrefill pins that the attributes are still read and the fields still use readonly rather than disabled. The widget has no browser harness in this repo, so that is the practical guard; the visual check above is manual. --- internal/handler/embed.js | 22 ++++++++++++++++++++-- internal/handler/embed_handler_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/internal/handler/embed.js b/internal/handler/embed.js index a9cceccb..33d258e5 100644 --- a/internal/handler/embed.js +++ b/internal/handler/embed.js @@ -13,6 +13,14 @@ * * * + * + * Optional identity prefill (#255): when the host page already knows the booker, pass + * name/email and the form opens with those fields filled and read-only. + * + * + * + * Read-only is a convenience, not a guarantee: the booking endpoint still accepts + * whatever a request sends, so this does not prove who booked. */ (function () { 'use strict'; @@ -186,6 +194,9 @@ if (this._mounted) return; this._mounted = true; this.slug = this.getAttribute('slug'); + // Optional identity prefill (#255). Trimmed; empty means "leave the field editable". + this.prefillName = (this.getAttribute('name') || '').trim(); + this.prefillEmail = (this.getAttribute('email') || '').trim(); this.root = this.attachShadow({ mode: 'open' }); var cssLink = el('link', { rel: 'stylesheet', href: BASE + '/booking.css' }); // .clamp styling arrives with the stylesheet, so re-measure the description @@ -597,8 +608,15 @@ var form = el('form', { novalidate: 'novalidate' }); var hp = el('input', { type: 'text', name: 'hp_extra', tabindex: '-1', autocomplete: 'off' }); form.appendChild(el('div', { 'aria-hidden': 'true', style: 'position:absolute;left:-5000px;height:0;width:0;overflow:hidden;' }, [hp])); - var name = el('input', { type: 'text', required: 'required', autocomplete: 'name', placeholder: t(this.i18n, 'name_placeholder') }); - var email = el('input', { type: 'email', required: 'required', autocomplete: 'email', placeholder: t(this.i18n, 'email_placeholder') }); + // Pre-filled and locked when the host page supplied them. `readonly`, not + // `disabled`: the value still submits and the field stays focusable for screen + // readers, which disabled would remove from the tab order. + var nameAttrs = { type: 'text', required: 'required', autocomplete: 'name', placeholder: t(this.i18n, 'name_placeholder') }; + if (self.prefillName) { nameAttrs.value = self.prefillName; nameAttrs.readonly = 'readonly'; } + var emailAttrs = { type: 'email', required: 'required', autocomplete: 'email', placeholder: t(this.i18n, 'email_placeholder') }; + if (self.prefillEmail) { emailAttrs.value = self.prefillEmail; emailAttrs.readonly = 'readonly'; } + var name = el('input', nameAttrs); + var email = el('input', emailAttrs); form.appendChild(el('div', { class: 'field' }, [el('label', { text: t(this.i18n, 'name_label') }), name])); form.appendChild(el('div', { class: 'field' }, [el('label', { text: t(this.i18n, 'email_label') }), email])); var phone = el('input', { type: 'tel', maxlength: '40', autocomplete: 'tel', id: 'phone-call-number' }); diff --git a/internal/handler/embed_handler_test.go b/internal/handler/embed_handler_test.go index a0608fe7..ca626070 100644 --- a/internal/handler/embed_handler_test.go +++ b/internal/handler/embed_handler_test.go @@ -96,3 +96,28 @@ func TestBookingCSS_controlsInheritFontFamily(t *testing.T) { t.Error("booking.css no longer makes form controls inherit font-family; buttons fall back to the UA font inside the embed widget") } } + +// #255: the host page can pass name/email so the booking form arrives pre-filled and +// read-only. This is a source-level guard: the widget is a web component with no test +// harness here, so what is pinned is that the attributes are still read and still +// render as `readonly` (not `disabled`, which would drop the field from the tab order). +func TestEmbedJS_identityPrefill(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + h.EmbedJS(rec, httptest.NewRequest(http.MethodGet, "/embed.js", nil)) + body := rec.Body.String() + + for _, want := range []string{ + `this.getAttribute('name')`, + `this.getAttribute('email')`, + `nameAttrs.readonly = 'readonly'`, + `emailAttrs.readonly = 'readonly'`, + } { + if !strings.Contains(body, want) { + t.Errorf("embed.js no longer contains %q - identity prefill may have regressed", want) + } + } + if strings.Contains(body, `disabled = 'disabled'`) { + t.Error("identity fields use disabled, which removes them from the tab order; use readonly") + } +}