Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(forms): distinguish button Auto state in the core

ldm0b4aa3aaea6e2
diff --git a/moli-dom/src/forms/button_type.rs b/moli-dom/src/forms/button_type.rs
new file mode 100644
index 000000000..d7288254a
--- /dev/null
+++ b/moli-dom/src/forms/button_type.rs
@@ -0,0 +1,67 @@
+/// The state selected by an HTML `<button>` element's `type` attribute.
+///
+/// Missing and invalid values select the Auto state. Whether Auto makes the
+/// element a submit button depends on the element's command attributes and
+/// parent node, so callers must not treat it as an alias for Submit.
+#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
+pub enum ButtonTypeState {
+ #[default]
+ Auto,
+ Submit,
+ Reset,
+ Button,
+}
+
+impl ButtonTypeState {
+ pub fn from_attribute_value(value: Option<&str>) -> Self {
+ match value {
+ Some(value) if value.eq_ignore_ascii_case("submit") => Self::Submit,
+ Some(value) if value.eq_ignore_ascii_case("reset") => Self::Reset,
+ Some(value) if value.eq_ignore_ascii_case("button") => Self::Button,
+ _ => Self::Auto,
+ }
+ }
+
+ pub const fn reflected_keyword(self, is_submit_button: bool) -> &'static str {
+ match self {
+ Self::Auto if is_submit_button => "submit",
+ Self::Auto | Self::Button => "button",
+ Self::Submit => "submit",
+ Self::Reset => "reset",
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::ButtonTypeState;
+
+ #[test]
+ fn keywords_are_ascii_case_insensitive_without_whitespace_trimming() {
+ assert_eq!(
+ ButtonTypeState::from_attribute_value(Some("SuBmIt")),
+ ButtonTypeState::Submit
+ );
+ assert_eq!(
+ ButtonTypeState::from_attribute_value(Some("RESET")),
+ ButtonTypeState::Reset
+ );
+ assert_eq!(
+ ButtonTypeState::from_attribute_value(Some("button")),
+ ButtonTypeState::Button
+ );
+ for value in [None, Some(""), Some("auto"), Some(" submit ")] {
+ assert_eq!(
+ ButtonTypeState::from_attribute_value(value),
+ ButtonTypeState::Auto
+ );
+ }
+ }
+
+ #[test]
+ fn auto_reflection_uses_the_derived_submit_button_state() {
+ assert_eq!(ButtonTypeState::Auto.reflected_keyword(true), "submit");
+ assert_eq!(ButtonTypeState::Auto.reflected_keyword(false), "button");
+ assert_eq!(ButtonTypeState::Submit.reflected_keyword(false), "submit");
+ }
+}
diff --git a/moli-dom/src/forms/mod.rs b/moli-dom/src/forms/mod.rs
index 37f0af1f3..500fe7a8d 100644
--- a/moli-dom/src/forms/mod.rs
+++ b/moli-dom/src/forms/mod.rs
@@ -1,8 +1,10 @@
+mod button_type;
mod input_type;
mod numeric;
mod text;
mod validity;
+pub use button_type::ButtonTypeState;
pub(crate) use input_type::{
InputValueSanitizationContext, sanitize_input_value_for_type_with_context,
};