From 12e227dfe5cff258c3a4e30ddcc6062c8c1190a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Sun, 13 Oct 2024 09:57:10 +0200 Subject: [PATCH] Add dom-accessibility-api --- Cargo.lock | 11 +++++++++++ packages/dom/Cargo.toml | 1 + packages/dom/src/config.rs | 1 - packages/dom/src/queries/role.rs | 23 ++++++++++++----------- packages/dom/src/types/config.rs | 15 --------------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d008fd..81925ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "dom-accessibility-api" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deb1a0f49725723d1372a15e358412e2a5e23e76b7d20c8773aa218f4149251d" +dependencies = [ + "regex", + "web-sys", +] + [[package]] name = "downcast" version = "0.11.0" @@ -273,6 +283,7 @@ name = "testing-library-dom" version = "0.0.1" dependencies = [ "aria-query", + "dom-accessibility-api", "indoc", "log", "mockall", diff --git a/packages/dom/Cargo.toml b/packages/dom/Cargo.toml index 84c78ff..6c7e1b5 100644 --- a/packages/dom/Cargo.toml +++ b/packages/dom/Cargo.toml @@ -11,6 +11,7 @@ version.workspace = true [dependencies] aria-query = "0.0.3" +dom-accessibility-api = "0.0.2" log.workspace = true paste = "1.0.15" pretty-format = { path = "../pretty-format", version = "0.0.1" } diff --git a/packages/dom/src/config.rs b/packages/dom/src/config.rs index 977f5d5..3ac855a 100644 --- a/packages/dom/src/config.rs +++ b/packages/dom/src/config.rs @@ -9,7 +9,6 @@ use crate::{ static CONFIG: LazyLock>> = LazyLock::new(|| { Arc::new(Mutex::new(Config { test_id_attribute: "data-testid".into(), - computed_style_supports_pseudo_elements: false, default_hidden: false, default_ignore: "script, style".into(), show_original_stack_trace: false, diff --git a/packages/dom/src/queries/role.rs b/packages/dom/src/queries/role.rs index 7d474f6..1dd0a45 100644 --- a/packages/dom/src/queries/role.rs +++ b/packages/dom/src/queries/role.rs @@ -1,7 +1,10 @@ use std::collections::HashSet; use aria_query::{AriaProperty, AriaRole, ROLES, ROLE_ELEMENTS}; -use web_sys::{Element, HtmlElement}; +use dom_accessibility_api::{ + compute_accessible_description, compute_accessible_name, ComputeTextAlternativeOptions, +}; +use web_sys::HtmlElement; use crate::{ build_queries, @@ -18,14 +21,6 @@ use crate::{ util::node_list_to_vec, }; -fn compute_accessibe_name(_element: &Element) -> String { - todo!("port dom-accessibility-api and import compute_accessibe_name") -} - -fn compute_accessibe_description(_element: &Element) -> String { - todo!("port dom-accessibility-api and import compute_accessibe_description") -} - pub fn _query_all_by_role>( container: &HtmlElement, role: M, @@ -261,7 +256,10 @@ pub fn _query_all_by_role>( let normalizer = |text| text; matches( - Some(compute_accessibe_name(element)), + Some(compute_accessible_name( + element, + ComputeTextAlternativeOptions::default(), + )), Some(element), name, &normalizer, @@ -276,7 +274,10 @@ pub fn _query_all_by_role>( let normalizer = |text| text; matches( - Some(compute_accessibe_description(element)), + Some(compute_accessible_description( + element, + ComputeTextAlternativeOptions::default(), + )), Some(element), description, &normalizer, diff --git a/packages/dom/src/types/config.rs b/packages/dom/src/types/config.rs index 9e9a29b..3a4daba 100644 --- a/packages/dom/src/types/config.rs +++ b/packages/dom/src/types/config.rs @@ -10,7 +10,6 @@ pub type GetElementErrorFn = dyn Fn(Option, Element) -> QueryError + Sen pub struct Config { pub test_id_attribute: String, // TODO - pub computed_style_supports_pseudo_elements: bool, /// Default value for the `hidden` option in `by_role` queries. pub default_hidden: bool, /// Default value for the `ignore` option in `by_text` queries. @@ -28,11 +27,6 @@ impl Config { if let Some(test_id_attribute) = other.test_id_attribute { self.test_id_attribute = test_id_attribute; } - if let Some(computed_style_supports_pseudo_elements) = - other.computed_style_supports_pseudo_elements - { - self.computed_style_supports_pseudo_elements = computed_style_supports_pseudo_elements; - } if let Some(default_hidden) = other.default_hidden { self.default_hidden = default_hidden; } @@ -55,7 +49,6 @@ impl Config { pub struct PartialConfig { pub test_id_attribute: Option, // TODO - pub computed_style_supports_pseudo_elements: Option, /// Default value for the `hidden` option in `by_role` queries. pub default_hidden: Option, /// Default value for the `ignore` option in `by_text` queries. @@ -74,11 +67,6 @@ impl PartialConfig { self } - pub fn computed_style_supports_pseudo_elements(mut self, value: bool) -> Self { - self.computed_style_supports_pseudo_elements = Some(value); - self - } - pub fn default_hidden(mut self, value: bool) -> Self { self.default_hidden = Some(value); self @@ -109,9 +97,6 @@ impl From<&Config> for PartialConfig { fn from(value: &Config) -> Self { Self { test_id_attribute: Some(value.test_id_attribute.clone()), - computed_style_supports_pseudo_elements: Some( - value.computed_style_supports_pseudo_elements, - ), default_hidden: Some(value.default_hidden), default_ignore: Some(value.default_ignore.clone()), show_original_stack_trace: Some(value.show_original_stack_trace),