From 2644363849783b5e0b0c1430eae6e49aaa232d50 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Tue, 10 Oct 2023 17:08:06 -0400 Subject: [PATCH] chore(conf): add audit config --- Cargo.lock | 2 +- README.md | 3 +- accessibility-rs/Cargo.toml | 2 +- accessibility-rs/src/i18n/locales.rs | 7 +- accessibility-rs/src/lib.rs | 46 ++++++- accessibility-rs/tests/integration_test.rs | 3 +- accessibility-rs/tests/unit/meta.rs | 142 ++++++++++----------- 7 files changed, 120 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5627293..de90ce5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "accessibility-rs" -version = "0.0.7" +version = "0.0.8" dependencies = [ "accessibility-scraper", "accessibility-tree", diff --git a/README.md b/README.md index 709f80d..a3e8c08 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,7 @@ The Rust web accessibility engine. ```rs -// pass in html and css rules prior. If css rules are not passed in internal extraction is performed. -let audit = accessibility_rs::audit(&html, &css_rules, false); +let audit = accessibility_rs::audit(&AuditConfig::new(&html, &css, false, "en")); ``` ## Features diff --git a/accessibility-rs/Cargo.toml b/accessibility-rs/Cargo.toml index d21e52e..416e52b 100644 --- a/accessibility-rs/Cargo.toml +++ b/accessibility-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "accessibility-rs" -version = "0.0.7" +version = "0.0.8" authors = ["The A11yWatch Project Developers", "Jeff Mendez "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/accessibility-rs/src/i18n/locales.rs b/accessibility-rs/src/i18n/locales.rs index 4aaa123..4ca4172 100644 --- a/accessibility-rs/src/i18n/locales.rs +++ b/accessibility-rs/src/i18n/locales.rs @@ -67,8 +67,7 @@ impl Langs { /// the context of the issue impl Messages { /// create a new message - pub fn new(en: M, es: M, de: M, ja: M) -> Messages - { + pub fn new(en: M, es: M, de: M, ja: M) -> Messages { Messages { en, es, @@ -107,8 +106,8 @@ lazy_static! { pub static ref LOCALES: BTreeMap<&'static str, Messages> = { BTreeMap::from([ (Techniques::H25.pairs()[0], Messages::new( - "A title should be provided for the document, using a non-empty title element in the head section.", - "Se debe proporcionar un título para el documento, utilizando un elemento de título no vacío en la sección head.", + "A title should be provided for the document, using a non-empty title element in the head section.", + "Se debe proporcionar un título para el documento, utilizando un elemento de título no vacío en la sección head.", "", "head セクションの空でない title 要素を使って、文書にタイトルをつけるべきです。" )), diff --git a/accessibility-rs/src/lib.rs b/accessibility-rs/src/lib.rs index 3ffd766..5fe1628 100644 --- a/accessibility-rs/src/lib.rs +++ b/accessibility-rs/src/lib.rs @@ -12,15 +12,53 @@ pub use crate::engine::audit::auditor::Auditor; pub use crate::engine::issue::Issue; pub use accessibility_scraper::ElementRef; +/// configs for the audit +#[derive(Default)] +pub struct AuditConfig { + /// the html source code + pub html: &'static str, + /// the css rules to apply + pub css: &'static str, + /// extract bounding box of elements + pub bounding_box: bool, + /// the locale of the audit translations + pub locale: &'static str, +} + +impl AuditConfig { + /// a new audit configuration + pub fn new( + html: &'static str, + css: &'static str, + bounding_box: bool, + locale: &'static str, + ) -> Self { + AuditConfig { + html, + css, + bounding_box, + locale, + } + } + + /// basic audit + pub fn basic(html: &'static str) -> Self { + AuditConfig { + html, + ..Default::default() + } + } +} + /// audit a web page passing the html and css rules. -pub fn audit(html: &str, css_rules: &str, bounding_box: bool) -> Vec { - let document = accessibility_scraper::Html::parse_document(html); +pub fn audit(config: &AuditConfig) -> Vec { + let document = accessibility_scraper::Html::parse_document(config.html); let mut nth_index_cache = selectors::NthIndexCache::from(Default::default()); let auditor = Auditor::new( &document, - &css_rules, + &config.css, engine::styles::css_cache::build_matching_context(&mut nth_index_cache), - bounding_box, + config.bounding_box, ); engine::audit::wcag::WCAG3AA::audit(&auditor) } diff --git a/accessibility-rs/tests/integration_test.rs b/accessibility-rs/tests/integration_test.rs index bd63733..41acd9f 100644 --- a/accessibility-rs/tests/integration_test.rs +++ b/accessibility-rs/tests/integration_test.rs @@ -1,9 +1,10 @@ //! Test suite for handling accessibility in Rust. mod mocks; +use accessibility_rs::AuditConfig; use mocks::mock; #[test] fn _audit() { - let _ = accessibility_rs::audit(mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, false); + let _ = accessibility_rs::audit(&AuditConfig::new(mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, false, "en")); } diff --git a/accessibility-rs/tests/unit/meta.rs b/accessibility-rs/tests/unit/meta.rs index 8e3c63a..6f2a722 100644 --- a/accessibility-rs/tests/unit/meta.rs +++ b/accessibility-rs/tests/unit/meta.rs @@ -1,11 +1,13 @@ //! Test for meta data. +use accessibility_rs::AuditConfig; + use crate::mocks::mock; #[test] /// missing title element fn _audit_missing_headers() { - let audit = accessibility_rs::audit(mock::MOCK_WEBSITE_HTML, &"", false); + let audit = accessibility_rs::audit(&AuditConfig::basic(mock::MOCK_WEBSITE_HTML)); let mut valid = true; for x in &audit { @@ -21,25 +23,23 @@ fn _audit_missing_headers() { #[test] /// meta refresh redirect fn _audit_meta_refresh() { - let audit = accessibility_rs::audit( + let audit = accessibility_rs::audit(&AuditConfig::basic( r###" - - Do not use this! - - - -

- If your browser supports Refresh, you'll be - transported to our - new site - in 5 seconds, otherwise, select the link manually. -

- - "###, - &"", - false, - ); + + Do not use this! + + + +

+ If your browser supports Refresh, you'll be + transported to our + new site + in 5 seconds, otherwise, select the link manually. +

+ + "###, + )); let mut valid = true; for x in &audit { @@ -51,18 +51,16 @@ fn _audit_meta_refresh() { assert_eq!(valid, false); - let audit = accessibility_rs::audit( + let audit = accessibility_rs::audit(&AuditConfig::basic( r###" - - HTML Techniques for WCAG 2.0 - - - - - "###, - &"", - false, - ); + + HTML Techniques for WCAG 2.0 + + + + +"###, + )); let mut valid = true; for x in &audit { @@ -78,18 +76,16 @@ fn _audit_meta_refresh() { #[test] /// no blink elements fn _audit_blink_found() { - let audit = accessibility_rs::audit( + let audit = accessibility_rs::audit(&AuditConfig::basic( r###" - - Do not use this! - - -

My Great Product Sale! $44,995!

- - "###, - &"", - false, - ); + + Do not use this! + + +

My Great Product Sale! $44,995!

+ +"###, + )); let mut valid = true; for x in &audit { @@ -105,8 +101,8 @@ fn _audit_blink_found() { #[test] /// iframe missing title fn _iframe_missing_title() { - let audit = accessibility_rs::audit( - r###" + let audit = accessibility_rs::audit(&AuditConfig { + html: r###" A simple frameset document @@ -121,9 +117,8 @@ fn _iframe_missing_title() { "###, - &"", - false, - ); + ..Default::default() + }); let mut valid = true; for x in &audit { @@ -134,26 +129,27 @@ fn _iframe_missing_title() { } assert_eq!(valid, true); - - let audit = accessibility_rs::audit( + let config = AuditConfig::new( r###" - - A simple frameset document - - - - - - <body> - <a href="lib.html" title="Library link">Select to - go to the electronic library</a> - </body> - - - "###, + + A simple frameset document + + + + + + <body> + <a href="lib.html" title="Library link">Select to + go to the electronic library</a> + </body> + + + "###, &"", false, + "en", ); + let audit = accessibility_rs::audit(&config); let mut valid = true; for x in &audit { @@ -164,20 +160,22 @@ fn _iframe_missing_title() { } assert_eq!(valid, false); - - let audit = accessibility_rs::audit( + let config = AuditConfig::new( r###" - - A document using iframe - - - "###, + + A document using iframe + + + "###, &"", false, + "en", ); + + let audit = accessibility_rs::audit(&config); let mut valid = true; for x in &audit {