Differentiate between missing/invalid value in make_enumerated_getter! (#34412)

* Create spec-compliant version of create_enumerated_getter

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Use new make_enumerated_getter! macro everywhere

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Remove old make_enumerated_getter macro

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Return lowercased value from make_enumerated_getter macro

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Update WPT expectations

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2024-11-28 01:54:03 +01:00 committed by GitHub
parent 612492b372
commit 9168375b33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 137 additions and 36 deletions

View file

@ -105,8 +105,14 @@ impl HTMLButtonElementMethods<crate::DomTypeHolder> for HTMLButtonElement {
self.form_owner()
}
// https://html.spec.whatwg.org/multipage/#dom-button-type
make_enumerated_getter!(Type, "type", "submit", "reset" | "button");
// <https://html.spec.whatwg.org/multipage/#dom-button-type>
make_enumerated_getter!(
Type,
"type",
"submit" | "reset" | "button",
missing => "submit",
invalid => "submit"
);
// https://html.spec.whatwg.org/multipage/#dom-button-type
make_setter!(SetType, "type");
@ -121,15 +127,21 @@ impl HTMLButtonElementMethods<crate::DomTypeHolder> for HTMLButtonElement {
make_enumerated_getter!(
FormEnctype,
"formenctype",
"application/x-www-form-urlencoded",
"text/plain" | "multipart/form-data"
"application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain",
invalid => "application/x-www-form-urlencoded"
);
// https://html.spec.whatwg.org/multipage/#dom-fs-formenctype
make_setter!(SetFormEnctype, "formenctype");
// https://html.spec.whatwg.org/multipage/#dom-fs-formmethod
make_enumerated_getter!(FormMethod, "formmethod", "get", "post" | "dialog");
make_enumerated_getter!(
FormMethod,
"formmethod",
"get" | "post" | "dialog",
missing => "get",
invalid => "get"
);
// https://html.spec.whatwg.org/multipage/#dom-fs-formmethod
make_setter!(SetFormMethod, "formmethod");

View file

@ -160,7 +160,14 @@ impl HTMLElementMethods<crate::DomTypeHolder> for HTMLElement {
make_setter!(SetLang, "lang");
// https://html.spec.whatwg.org/multipage/#the-dir-attribute
make_enumerated_getter!(Dir, "dir", "", "ltr" | "rtl" | "auto");
make_enumerated_getter!(
Dir,
"dir",
"ltr" | "rtl" | "auto",
missing => "",
invalid => ""
);
// https://html.spec.whatwg.org/multipage/#the-dir-attribute
make_setter!(SetDir, "dir");

View file

@ -217,7 +217,13 @@ impl HTMLFormElementMethods<crate::DomTypeHolder> for HTMLFormElement {
make_setter!(SetAction, "action");
// https://html.spec.whatwg.org/multipage/#dom-form-autocomplete
make_enumerated_getter!(Autocomplete, "autocomplete", "on", "off");
make_enumerated_getter!(
Autocomplete,
"autocomplete",
"on" | "off",
missing => "on",
invalid => "on"
);
// https://html.spec.whatwg.org/multipage/#dom-form-autocomplete
make_setter!(SetAutocomplete, "autocomplete");
@ -226,8 +232,9 @@ impl HTMLFormElementMethods<crate::DomTypeHolder> for HTMLFormElement {
make_enumerated_getter!(
Enctype,
"enctype",
"application/x-www-form-urlencoded",
"text/plain" | "multipart/form-data"
"application/x-www-form-urlencoded" | "text/plain" | "multipart/form-data",
missing => "application/x-www-form-urlencoded",
invalid => "application/x-www-form-urlencoded"
);
// https://html.spec.whatwg.org/multipage/#dom-fs-enctype
@ -244,7 +251,13 @@ impl HTMLFormElementMethods<crate::DomTypeHolder> for HTMLFormElement {
}
// https://html.spec.whatwg.org/multipage/#dom-fs-method
make_enumerated_getter!(Method, "method", "get", "post" | "dialog");
make_enumerated_getter!(
Method,
"method",
"get" | "post" | "dialog",
missing => "get",
invalid => "get"
);
// https://html.spec.whatwg.org/multipage/#dom-fs-method
make_setter!(SetMethod, "method");

View file

@ -1399,21 +1399,28 @@ impl HTMLInputElementMethods<crate::DomTypeHolder> for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#dom-input-formaction
make_setter!(SetFormAction, "formaction");
// https://html.spec.whatwg.org/multipage/#dom-input-formenctype
// https://html.spec.whatwg.org/multipage/#dom-fs-formenctype
make_enumerated_getter!(
FormEnctype,
"formenctype",
"application/x-www-form-urlencoded",
"text/plain" | "multipart/form-data"
"application/x-www-form-urlencoded" | "text/plain" | "multipart/form-data",
missing => "",
invalid => "application/x-www-form-urlencoded"
);
// https://html.spec.whatwg.org/multipage/#dom-input-formenctype
make_setter!(SetFormEnctype, "formenctype");
// https://html.spec.whatwg.org/multipage/#dom-input-formmethod
make_enumerated_getter!(FormMethod, "formmethod", "get", "post" | "dialog");
// https://html.spec.whatwg.org/multipage/#dom-fs-formmethod
make_enumerated_getter!(
FormMethod,
"formmethod",
"get" | "post" | "dialog",
missing => "get",
invalid => "get"
);
// https://html.spec.whatwg.org/multipage/#dom-input-formmethod
// https://html.spec.whatwg.org/multipage/#dom-fs-formmethod
make_setter!(SetFormMethod, "formmethod");
// https://html.spec.whatwg.org/multipage/#dom-input-formtarget

View file

@ -2159,8 +2159,15 @@ impl HTMLMediaElementMethods<crate::DomTypeHolder> for HTMLMediaElement {
}
// https://html.spec.whatwg.org/multipage/#attr-media-preload
// Missing value default is user-agent defined.
make_enumerated_getter!(Preload, "preload", "", "none" | "metadata" | "auto");
// Missing/Invalid values are user-agent defined.
make_enumerated_getter!(
Preload,
"preload",
"none" | "metadata" | "auto",
missing => "auto",
invalid => "auto"
);
// https://html.spec.whatwg.org/multipage/#attr-media-preload
make_setter!(SetPreload, "preload");

View file

@ -158,22 +158,86 @@ macro_rules! make_labels_getter(
);
);
#[macro_export]
/// Implements the `To determine the state of an attribute` steps from
/// <https://html.spec.whatwg.org/multipage/#keywords-and-enumerated-attributes>
macro_rules! make_enumerated_getter(
( $attr:ident, $htmlname:tt, $default:expr, $($choices:pat_param)|+) => (
($attr:ident,
$htmlname:tt,
$($choices:literal)|+,
missing => $missing:literal,
invalid => $invalid:literal
) => (
fn $attr(&self) -> DOMString {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
let element = self.upcast::<Element>();
let mut val = element.get_string_attribute(&html5ever::local_name!($htmlname));
val.make_ascii_lowercase();
// https://html.spec.whatwg.org/multipage/#attr-fs-method
match &*val {
$($choices)|+ => val,
_ => DOMString::from($default)
use $crate::dom::bindings::codegen::Bindings::AttrBinding::Attr_Binding::AttrMethods;
let attr_or_none = self.upcast::<Element>()
.get_attribute(&html5ever::ns!(), &html5ever::local_name!($htmlname));
match attr_or_none {
// Step 1. If the attribute is not specified:
None => {
// Step 1.1. If the attribute has a missing value default state defined, then return that
// missing value default state.
// Step 1.2 Otherwise, return no state.
return DOMString::from($missing);
},
Some(attr) => {
// Step 2. If the attribute's value is an ASCII case-insensitive match for one of the keywords
// defined for the attribute, then return the state represented by that keyword.
let value: DOMString = attr.Value().to_ascii_lowercase().into();
$(
if value.str() == $choices {
return value;
}
)+
// Step 3. If the attribute has an invalid value default state defined, then return that invalid
// value default state.
// Step 4. Return no state.
return DOMString::from($invalid);
}
}
}
);
($attr:ident,
$htmlname:tt,
$($choices:literal)|+,
) => (
make_enumerated_getter!(
$attr,
$htmlname,
$($choices)|+,
missing => "",
invalid => ""
);
);
($attr:ident,
$htmlname:tt,
$($choices:literal)|+,
invalid => $invalid:literal
) => (
make_enumerated_getter!(
$attr,
$htmlname,
$($choices)|+,
missing => "",
invalid => $invalid
);
);
($attr:ident,
$htmlname:tt,
$($choices:literal)|+,
missing => $missing:literal,
) => (
make_enumerated_getter!(
$attr,
$htmlname,
$($choices)|+,
missing => $missing,
invalid => ""
);
);
);
// concat_idents! doesn't work for function name positions, so

View file

@ -248,9 +248,6 @@
[input.autocomplete: IDL set to object "test-valueOf"]
expected: FAIL
[input.formEnctype: IDL get with DOM attribute unset]
expected: FAIL
[input.formMethod: IDL get with DOM attribute unset]
expected: FAIL

View file

@ -1130,9 +1130,6 @@
[input.autocomplete: IDL set to object "test-valueOf"]
expected: FAIL
[input.formEnctype: IDL get with DOM attribute unset]
expected: FAIL
[input.formMethod: IDL get with DOM attribute unset]
expected: FAIL
@ -1604,9 +1601,6 @@
[button.tabIndex: IDL set to -2147483648]
expected: FAIL
[button.formEnctype: IDL get with DOM attribute unset]
expected: FAIL
[button.formMethod: IDL get with DOM attribute unset]
expected: FAIL