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") + } +}