Update the FormData constructor to allow providing a submitter (#35066)

Signed-off-by: Shane Handley <shanehandley@fastmail.com>
This commit is contained in:
shanehandley 2025-01-24 04:20:44 +11:00 committed by GitHub
parent f5f5a3f79e
commit cca600e909
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 16 deletions

View file

@ -20,7 +20,12 @@ use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::blob::Blob;
use crate::dom::file::File;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlformelement::{FormDatum, FormDatumValue, HTMLFormElement};
use crate::dom::htmlbuttonelement::HTMLButtonElement;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::{
FormDatum, FormDatumValue, FormSubmitterElement, HTMLFormElement,
};
use crate::dom::htmlinputelement::HTMLInputElement;
use crate::script_runtime::CanGc;
#[dom_struct]
@ -70,20 +75,60 @@ impl FormData {
impl FormDataMethods<crate::DomTypeHolder> for FormData {
// https://xhr.spec.whatwg.org/#dom-formdata
fn Constructor(
fn Constructor<'a>(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
form: Option<&HTMLFormElement>,
form: Option<&'a HTMLFormElement>,
submitter: Option<&'a HTMLElement>,
) -> Fallible<DomRoot<FormData>> {
// Helper to validate the submitter
fn validate_submitter<'b>(
submitter: &'b HTMLElement,
form: &'b HTMLFormElement,
) -> Result<FormSubmitterElement<'b>, Error> {
let submit_button = submitter
.downcast::<HTMLButtonElement>()
.map(FormSubmitterElement::Button)
.or_else(|| {
submitter
.downcast::<HTMLInputElement>()
.map(FormSubmitterElement::Input)
})
.ok_or(Error::Type(
"submitter is not a form submitter element".to_string(),
))?;
// Step 1.1.1. If submitter is not a submit button, then throw a TypeError.
if !submit_button.is_submit_button() {
return Err(Error::Type("submitter is not a submit button".to_string()));
}
// Step 1.1.2. If submitters form owner is not form, then throw a "NotFoundError"
// DOMException.
if !matches!(submit_button.form_owner(), Some(owner) if *owner == *form) {
return Err(Error::NotFound);
}
Ok(submit_button)
}
// Step 1. If form is given, then:
if let Some(opt_form) = form {
return match opt_form.get_form_dataset(None, None, can_gc) {
// Step 1.1. If submitter is non-null, then:
let submitter_element = submitter
.map(|s| validate_submitter(s, opt_form))
.transpose()?;
// Step 1.2. Let list be the result of constructing the entry list for form and submitter.
return match opt_form.get_form_dataset(submitter_element, None, can_gc) {
Some(form_datums) => Ok(FormData::new_with_proto(
Some(form_datums),
global,
proto,
can_gc,
)),
// Step 1.3. If list is null, then throw an "InvalidStateError" DOMException.
None => Err(Error::InvalidState),
};
}

View file

@ -1475,7 +1475,7 @@ impl FormSubmitterElement<'_> {
}
// https://html.spec.whatwg.org/multipage/#concept-submit-button
fn is_submit_button(&self) -> bool {
pub(crate) fn is_submit_button(&self) -> bool {
match *self {
// https://html.spec.whatwg.org/multipage/#image-button-state-(type=image)
// https://html.spec.whatwg.org/multipage/#submit-button-state-(type=submit)
@ -1487,7 +1487,7 @@ impl FormSubmitterElement<'_> {
}
// https://html.spec.whatwg.org/multipage/#form-owner
fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
pub(crate) fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
match *self {
FormSubmitterElement::Button(button_el) => button_el.form_owner(),
FormSubmitterElement::Input(input_el) => input_el.form_owner(),

View file

@ -10,7 +10,7 @@ typedef (File or USVString) FormDataEntryValue;
[Exposed=(Window,Worker)]
interface FormData {
[Throws] constructor(optional HTMLFormElement form);
[Throws] constructor(optional HTMLFormElement form, optional HTMLElement? submitter = null);
undefined append(USVString name, USVString value);
undefined append(USVString name, Blob value, optional USVString filename);
undefined delete(USVString name);