From 6b903e62de74e31047e5c296dbff68f812c6021e Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Fri, 29 Jan 2021 15:12:31 -0600 Subject: [PATCH] Explicitly construct URLSearchParams with string pairs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Although Chrome, Firefox, and Safari all seem to implement support for `new URLSearchParams(formData)`, the [spec][1] doesn’t mention it, the IDL and [TypeScript][2] don’t have typings for it, and searching suggests it’s a recent addition. [1]: https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams [2]: https://github.com/microsoft/TypeScript/issues/30584 --- src/core/drive/form_submission.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/drive/form_submission.ts b/src/core/drive/form_submission.ts index 01e6bfe06..c5dd5d742 100644 --- a/src/core/drive/form_submission.ts +++ b/src/core/drive/form_submission.ts @@ -52,8 +52,8 @@ export class FormSubmission { constructor(delegate: FormSubmissionDelegate, formElement: HTMLFormElement, submitter?: HTMLElement, mustRedirect = false) { this.delegate = delegate this.formElement = formElement - this.formData = buildFormData(formElement, submitter) this.submitter = submitter + this.formData = buildFormData(formElement, submitter) this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body) this.mustRedirect = mustRedirect } @@ -73,7 +73,7 @@ export class FormSubmission { get body() { if (this.enctype == FormEnctype.urlEncoded || this.method == FetchMethod.get) { - return new URLSearchParams(this.formData as any) + return new URLSearchParams(this.stringFormData) } else { return this.formData } @@ -83,6 +83,12 @@ export class FormSubmission { return formEnctypeFromString(this.submitter?.getAttribute("formenctype") || this.formElement.enctype) } + get stringFormData() { + return [ ...this.formData ].reduce((entries, [ name, value ]) => { + return entries.concat(typeof value == "string" ? [[ name, value ]] : []) + }, [] as [string, string][]) + } + // The submission process async start() {