From 00baa5b9bb4a4b30deaf96ccd7d4719d41953144 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 10 Oct 2024 11:59:10 +0100 Subject: [PATCH] refactor(aria): don't use macros --- crates/biome_aria/src/macros.rs | 12 ----- crates/biome_aria/src/roles.rs | 92 ++++++++++++++++++++++++++------- 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/crates/biome_aria/src/macros.rs b/crates/biome_aria/src/macros.rs index a238fd606e6e..e0c77eab4f8b 100644 --- a/crates/biome_aria/src/macros.rs +++ b/crates/biome_aria/src/macros.rs @@ -50,18 +50,6 @@ macro_rules! define_role { } impl AriaRoleDefinitionWithConcepts for $id { - fn concepts_by_element_name<'a>( - &self, - element_name: &str, - ) -> ElementsAndAttributes<'a> { - for (concept_name, _attributes) in Self::CONCEPTS { - if *concept_name == element_name { - return Some(Self::CONCEPTS.iter()); - } - } - None - } - fn concepts_by_role<'a>(&self) -> ElementsAndAttributes<'a> { Some(Self::CONCEPTS.iter()) } diff --git a/crates/biome_aria/src/roles.rs b/crates/biome_aria/src/roles.rs index a88435f20a9c..ef916b93dd37 100644 --- a/crates/biome_aria/src/roles.rs +++ b/crates/biome_aria/src/roles.rs @@ -68,12 +68,31 @@ pub trait AriaRoleDefinition: Debug { } } -define_role! { - /// https://www.w3.org/TR/wai-aria-1.1/#button - ButtonRole { - PROPS: [("aria-expanded", false), ("aria-expanded", false)], - ROLES: ["roletype", "widget", "command"], - CONCEPTS: &[("button", &[]), ("input", &[("type", "button")])], +#[derive(Debug)] +/// https://www.w3.org/TR/wai-aria-1.1/#switch +struct ButtonRole; + +impl ButtonRole { + const PROPS: &'static [(&'static str, bool)] = + &[("aria-expanded", false), ("aria-expanded", false)]; + const ROLES: &'static [&'static str] = &["roletype", "widget", "command"]; + const CONCEPTS: &'static [(&'static str, &'static [(&'static str, &'static str)])] = + &[("input", &[("type", "checkbox")])]; +} + +impl AriaRoleDefinition for ButtonRole { + fn properties(&self) -> Iter<(&str, bool)> { + Self::PROPS.iter() + } + + fn roles(&self) -> Iter<&str> { + Self::ROLES.iter() + } +} + +impl AriaRoleDefinitionWithConcepts for ButtonRole { + fn concepts_by_role<'a>(&self) -> ElementsAndAttributes<'a> { + Some(Self::CONCEPTS.iter()) } } @@ -93,20 +112,50 @@ define_role! { CONCEPTS: &[("input", &[("type", "radio")])], } } -define_role! { - /// https://www.w3.org/TR/wai-aria-1.1/#switch - SwitchRole { - PROPS: [("aria-checked", true)], - ROLES: ["checkbox", "widget"], + +#[derive(Debug)] +/// https://www.w3.org/TR/wai-aria-1.1/#switch +struct SwitchRole; + +impl SwitchRole { + const PROPS: &'static [(&'static str, bool)] = &[("aria-checked", true)]; + const ROLES: &'static [&'static str] = &["checkbox", "widget"]; +} + +impl AriaRoleDefinition for SwitchRole { + fn properties(&self) -> Iter<(&str, bool)> { + Self::PROPS.iter() + } + + fn roles(&self) -> Iter<&str> { + Self::ROLES.iter() } } -define_role! { - /// https://www.w3.org/TR/wai-aria-1.1/#option - OptionRole { - PROPS: [("aria-selected", true)], - ROLES: ["treeitem", "widget"], - CONCEPTS: &[("option", &[])], +#[derive(Debug)] +/// https://www.w3.org/TR/wai-aria-1.1/#option +struct OptionRole; + +impl OptionRole { + const PROPS: &'static [(&'static str, bool)] = &[("aria-selected", true)]; + const ROLES: &'static [&'static str] = &["treeitem", "widget"]; + const CONCEPTS: &'static [(&'static str, &'static [(&'static str, &'static str)])] = + &[("option", &[])]; +} + +impl AriaRoleDefinition for OptionRole { + fn properties(&self) -> Iter<(&str, bool)> { + Self::PROPS.iter() + } + + fn roles(&self) -> Iter<&str> { + Self::ROLES.iter() + } +} + +impl AriaRoleDefinitionWithConcepts for OptionRole { + fn concepts_by_role<'a>(&self) -> ElementsAndAttributes<'a> { + Some(Self::CONCEPTS.iter()) } } @@ -1362,7 +1411,14 @@ impl<'a> AriaRoles { type ElementsAndAttributes<'a> = Option>; pub trait AriaRoleDefinitionWithConcepts: AriaRoleDefinition { - fn concepts_by_element_name<'a>(&self, _element_name: &str) -> ElementsAndAttributes<'a> { + fn concepts_by_element_name<'a>(&self, element_name: &str) -> ElementsAndAttributes<'a> { + if let Some(iter) = self.concepts_by_role() { + for (concept_name, _attributes) in iter { + if *concept_name == element_name { + return self.concepts_by_role(); + } + } + } None }