Merge pull request #11 from SaraVieira/aria

add support for aria
This commit is contained in:
ashley williams 2022-12-19 14:45:46 -06:00 committed by GitHub
commit b7a52d3e3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 1 deletions

View File

@ -33,7 +33,33 @@ pub fn global_attrs(span: Span) -> StringyMap<Ident, TokenStream> {
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
}

View File

@ -102,6 +102,7 @@ impl Declare {
pub struct #elem_name<T> 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
}

View File

@ -97,6 +97,21 @@ fn extract_event_handlers(
events
}
fn extract_aria_attributes(
attrs: &mut StringyMap<Ident, TokenTree>,
) -> StringyMap<String, TokenTree> {
let mut data = StringyMap::new();
let keys: Vec<Ident> = 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() {

View File

@ -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<String> = html!(<div aria_hidden="true" aria_label="hello" />
);
assert_eq!(
"<div aria-hidden=\"true\" aria-label=\"hello\"></div>",
frag.to_string()
);
}