Skip to content

Commit

Permalink
Explicitly construct URLSearchParams with string pairs
Browse files Browse the repository at this point in the history
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]: microsoft/TypeScript#30584
  • Loading branch information
sstephenson committed Jan 29, 2021
1 parent 385b01a commit 6b903e6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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() {
Expand Down

0 comments on commit 6b903e6

Please sign in to comment.