From 95e2725a0b94fadcfc96a4142af08bd3b49565c8 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Sun, 17 Mar 2024 19:51:49 -0400 Subject: [PATCH] chore(validation): fix primary test for H44 [#4] --- Cargo.lock | 2 +- README.md | 2 +- accessibility-rs/Cargo.toml | 2 +- .../src/engine/rules/wcag_rule_map.rs | 59 +++++++++++++++---- accessibility-rs/src/lib.rs | 6 +- accessibility-rs/tests/integration_test.rs | 4 +- accessibility-rs/tests/unit/area.rs | 3 +- accessibility-rs/tests/unit/mod.rs | 2 +- 8 files changed, 56 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd6e459..f0ba6ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "accessibility-rs" -version = "0.0.48" +version = "0.0.49" dependencies = [ "accessibility-scraper", "accessibility-tree", diff --git a/README.md b/README.md index ee93578..a3f1e60 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The Rust web accessibility engine. ```toml [dependencies] -accessibility-rs = "^0.0.48" +accessibility-rs = "^0.0.49" ``` ```rs diff --git a/accessibility-rs/Cargo.toml b/accessibility-rs/Cargo.toml index cd9f6d3..2e2ac8a 100644 --- a/accessibility-rs/Cargo.toml +++ b/accessibility-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "accessibility-rs" -version = "0.0.48" +version = "0.0.49" authors = ["The A11yWatch Project Developers", "Jeff Mendez "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/accessibility-rs/src/engine/rules/wcag_rule_map.rs b/accessibility-rs/src/engine/rules/wcag_rule_map.rs index dcf21ab..c4fa854 100644 --- a/accessibility-rs/src/engine/rules/wcag_rule_map.rs +++ b/accessibility-rs/src/engine/rules/wcag_rule_map.rs @@ -420,24 +420,57 @@ lazy_static! { let mut elements = Vec::new(); for ele in nodes { - match ele.0.attr("for") { - Some(s) => { - let selector = unsafe { Selector::parse(&("#".to_string() + &s)).unwrap_unchecked() }; - let root_tree = ele.0.tree().root(); - - match ElementRef::new(root_tree) { - t => { - let e = t.select(&selector); + let has_valid_aria_label = ele.0.attr("aria-label").map_or(false, |s| !s.trim().is_empty()); + let mut has_valid_text_match = false; + + if !has_valid_aria_label && ele.0.text().next().is_some() { + for child in ele.0.children() { + match ElementRef::wrap(child) { + Some(child_element) => { + let name = child_element.value().name(); + + if vec!["textareas", "select"].contains(&name) { + has_valid_text_match = true; + } else if name == "input" { + match child_element.attr("type") { + Some(s) => { + if vec!["text", "file", "password"].contains(&s) { + has_valid_text_match = true; + } + } + _ => () + } + } - if e.count() == 0 { - valid = false; - elements.push(get_unique_selector(&ele.0)) + if has_valid_text_match { + break; } } + _ => () } } - _ => () } + + if !has_valid_aria_label && !has_valid_text_match { + match ele.0.attr("for") { + Some(s) => { + let selector = unsafe { Selector::parse(&("#".to_string() + &s)).unwrap_unchecked() }; + let root_tree = ele.0.tree().root(); + + match ElementRef::new(root_tree) { + t => { + let e = t.select(&selector); + + if e.count() == 0 { + valid = false; + elements.push(get_unique_selector(&ele.0)) + } + } + } + } + _ => () + } + } } Validation::new(valid, "NonExistent", elements, Default::default()).into() @@ -541,7 +574,7 @@ lazy_static! { elements.push(get_unique_selector(&ele)); } } - + Validation::new(valid, "ImageMapAreaNoAlt", elements, Default::default()).into() }) ])), diff --git a/accessibility-rs/src/lib.rs b/accessibility-rs/src/lib.rs index 8e51d91..e1d6fc4 100644 --- a/accessibility-rs/src/lib.rs +++ b/accessibility-rs/src/lib.rs @@ -3,13 +3,13 @@ //! Audit html to see how it complies with WCAG //! standards. //! -//! Accessibility-RS is a web accessibility +//! accessibility-rs is a web accessibility //! engine that can replicate websites without //! a browser to get complex accessibility reports. //! -//! # How to use Accessibility-RS +//! # How to use accessibility-rs //! -//! There are a couple of ways to use Accessibility-RS: +//! There are a couple of ways to use accessibility-rs: //! //! - **Audit** perform an audit against an html page. //! - [`audit`] is used to audit a web page for issues. diff --git a/accessibility-rs/tests/integration_test.rs b/accessibility-rs/tests/integration_test.rs index a4edbbc..d4b70f7 100644 --- a/accessibility-rs/tests/integration_test.rs +++ b/accessibility-rs/tests/integration_test.rs @@ -9,7 +9,7 @@ fn _audit() { let report = accessibility_rs::audit(AuditConfig::new( mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, - true, + false, "en", )); println!("{:?}", report) @@ -20,7 +20,7 @@ fn _audit_large() { let report = accessibility_rs::audit(AuditConfig::new( mock::MOCK_HTML_LARGE_PAGE, &mock::MOCK_CSS_RULES_LARGE, - true, + false, "en", )); println!("{:?}", report) diff --git a/accessibility-rs/tests/unit/area.rs b/accessibility-rs/tests/unit/area.rs index 620be55..435ddd7 100644 --- a/accessibility-rs/tests/unit/area.rs +++ b/accessibility-rs/tests/unit/area.rs @@ -1,7 +1,6 @@ //! Test for anchors. -use accessibility_rs::{engine::audit, AuditConfig}; -use maud::html; +use accessibility_rs::AuditConfig; #[test] fn _audit_missing_alt_area() { diff --git a/accessibility-rs/tests/unit/mod.rs b/accessibility-rs/tests/unit/mod.rs index c94d3fb..7541196 100644 --- a/accessibility-rs/tests/unit/mod.rs +++ b/accessibility-rs/tests/unit/mod.rs @@ -1,5 +1,6 @@ pub mod anchor; pub mod applet; +pub mod area; pub mod contrast; pub mod fieldset; pub mod heading; @@ -8,4 +9,3 @@ pub mod img; pub mod input; pub mod label; pub mod meta; -pub mod area; \ No newline at end of file