From 3e2d9282f62ada3a70c30496486b9ecf0ae80f96 Mon Sep 17 00:00:00 2001 From: Sara Vieira Date: Mon, 19 Dec 2022 19:27:48 +0000 Subject: [PATCH] add support for aria --- macros/src/config.rs | 28 +++++++++++++++++++++++++++- macros/src/declare.rs | 11 +++++++++++ macros/src/html.rs | 26 ++++++++++++++++++++++++++ typed-html/src/elements.rs | 14 ++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/macros/src/config.rs b/macros/src/config.rs index 29d88a3..068e498 100644 --- a/macros/src/config.rs +++ b/macros/src/config.rs @@ -33,7 +33,33 @@ pub fn global_attrs(span: Span) -> StringyMap { insert("tabindex", "isize"); insert("title", "String"); - // FIXME ARIA and XML attrs missing + // ARIA + insert("aria_autocomplete", "String"); + insert("aria_checked", "crate::types::Bool"); + insert("aria_disabled", "crate::types::Bool"); + insert("aria_errormessage", "String"); + insert("aria_expanded", "crate::types::Bool"); + insert("aria_haspopup", "crate::types::Bool"); + insert("aria_hidden", "crate::types::Bool"); + insert("aria_invalid", "crate::types::Bool"); + insert("aria_label", "String"); + insert("aria_modal", "crate::types::Bool"); + insert("aria_multiline", "crate::types::Bool"); + insert("aria_multiselectable", "crate::types::Bool"); + insert("aria_orientation", "String"); // TODO Only supports some values https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-orientation + insert("aria_placeholder", "String"); + insert("aria_pressed", "crate::types::Bool"); + insert("aria_readonly", "crate::types::Bool"); + insert("aria_required", "crate::types::Bool"); + insert("aria_selected", "crate::types::Bool"); + insert("aria_placeholder", "String"); + insert("aria_sort", "String"); // TODO only supports some values + insert("aria_valuemax", "isize"); + insert("aria_valuemin", "isize"); + insert("aria_valuenow", "isize"); + insert("aria_valuetext", "String"); + + // FIXME XML attrs missing } attrs } diff --git a/macros/src/declare.rs b/macros/src/declare.rs index 13c212b..adcbf7a 100644 --- a/macros/src/declare.rs +++ b/macros/src/declare.rs @@ -102,6 +102,7 @@ impl Declare { pub struct #elem_name where T: crate::OutputType + Send { pub attrs: #attr_type_name, pub data_attributes: Vec<(&'static str, String)>, + pub aria_attributes: Vec<(&'static str, String)>, pub events: T::Events, #body } @@ -127,6 +128,7 @@ impl Declare { attrs: #attr_type_name { #attrs }, )); body.extend(quote!(data_attributes: Vec::new(),)); + body.extend(quote!(aria_attributes: Vec::new(),)); for (child_name, _, _) in self.req_children() { body.extend(quote!( #child_name, )); @@ -175,6 +177,7 @@ impl Declare { let mut attributes = Vec::new(); #push_attrs attributes.extend(self.data_attributes.clone()); + attributes.extend(self.aria_attributes.clone()); let mut children = Vec::new(); #req_children @@ -240,6 +243,10 @@ impl Declare { for (key, value) in &self.data_attributes { out.push((key, value.to_string())); } + + for (key, value) in &self.aria_attributes { + out.push((key, value.to_string())); + } out } } @@ -353,6 +360,10 @@ impl Declare { write!(f, " data-{}=\"{}\"", key, crate::escape_html_attribute(value.to_string()))?; } + for (key, value) in &self.aria_attributes { + write!(f, " aria-{}=\"{}\"", key, + crate::escape_html_attribute(value.to_string()))?; + } write!(f, "{}", self.events)?; #print_children } diff --git a/macros/src/html.rs b/macros/src/html.rs index 26986af..4fd914c 100644 --- a/macros/src/html.rs +++ b/macros/src/html.rs @@ -97,6 +97,21 @@ fn extract_event_handlers( events } +fn extract_aria_attributes( + attrs: &mut StringyMap, +) -> StringyMap { + let mut data = StringyMap::new(); + let keys: Vec = attrs.keys().cloned().collect(); + for key in keys { + let key_name = key.to_string(); + if let Some(key_name) = key_name.strip_prefix("aria_") { + let value = attrs.remove(&key).unwrap(); + data.insert(key_name.to_string(), value); + } + } + data +} + fn process_value(value: &TokenTree) -> TokenStream { match value { TokenTree::Group(g) if g.delimiter() == Delimiter::Bracket => { @@ -146,6 +161,7 @@ impl Element { } let events = extract_event_handlers(&mut self.attributes); let data_attrs = extract_data_attrs(&mut self.attributes); + let aria_attrs = extract_aria_attributes(&mut self.attributes); let attrs = self.attributes.iter().map(|(key, value)| { ( key.to_string(), @@ -217,6 +233,16 @@ impl Element { element.data_attributes.push((#key, #value.into())); )); } + + for (key, value) in aria_attrs + .iter() + .map(|(k, v)| (TokenTree::from(Literal::string(k)), v.clone())) + { + body.extend(quote!( + element.aria_attributes.push((#key, #value.into())); + )); + } + body.extend(opt_children); for (key, value) in events.iter() { diff --git a/typed-html/src/elements.rs b/typed-html/src/elements.rs index f8377df..5312da1 100644 --- a/typed-html/src/elements.rs +++ b/typed-html/src/elements.rs @@ -474,3 +474,17 @@ fn test_meta_tags() { frag.to_string() ); } + +#[test] +fn test_aria() { + use crate as axohtml; + use crate::{dom::DOMTree, html}; + + let frag: DOMTree = html!(
+ ); + + assert_eq!( + "
", + frag.to_string() + ); +}