From 6b48b50996bd760b0d07615d5d4336fa0ec6169f Mon Sep 17 00:00:00 2001 From: j-mendez Date: Sat, 14 Oct 2023 09:54:49 -0400 Subject: [PATCH] chore(headings): add element selectors --- Cargo.lock | 2 +- README.md | 2 +- accessibility-rs/Cargo.toml | 2 +- accessibility-rs/src/engine/audit/wcag.rs | 1 + accessibility-rs/src/engine/rules/rule.rs | 2 +- .../src/engine/rules/techniques.rs | 24 ++----------------- .../src/engine/rules/utils/nodes.rs | 23 ++++++++++++++---- .../src/engine/rules/wcag_rule_map.rs | 20 ++++++++-------- accessibility-rs/tests/unit/heading.rs | 2 +- accessibility-rs/tests/unit/img.rs | 4 +--- 10 files changed, 37 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b052897..372baa6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "accessibility-rs" -version = "0.0.22" +version = "0.0.24" dependencies = [ "accessibility-scraper", "accessibility-tree", diff --git a/README.md b/README.md index a45e997..5393f0b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ accessibility-rs = "^0.0.16" ```rs use accessibility_rs::{audit, AuditConfig}; -// pass in raw html and css if coming from a headless browser +// pass in raw html and optional css let audit = accessibility_rs::audit(&AuditConfig::new(&html, &css, false, "en")); ``` diff --git a/accessibility-rs/Cargo.toml b/accessibility-rs/Cargo.toml index 1c612a8..e8d05b8 100644 --- a/accessibility-rs/Cargo.toml +++ b/accessibility-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "accessibility-rs" -version = "0.0.22" +version = "0.0.24" authors = ["The A11yWatch Project Developers", "Jeff Mendez "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/accessibility-rs/src/engine/audit/wcag.rs b/accessibility-rs/src/engine/audit/wcag.rs index b357f09..b9b0ba0 100644 --- a/accessibility-rs/src/engine/audit/wcag.rs +++ b/accessibility-rs/src/engine/audit/wcag.rs @@ -15,6 +15,7 @@ impl WCAG3AA { auditor: &Auditor<'_>, ) -> Vec { let mut issues: Vec = Vec::new(); + // TODO: push rules found to MAP that are different across nodes to combine the selectors // go through nodes and map to validation rules for node in &auditor.tree { diff --git a/accessibility-rs/src/engine/rules/rule.rs b/accessibility-rs/src/engine/rules/rule.rs index 92b7645..14e8adf 100644 --- a/accessibility-rs/src/engine/rules/rule.rs +++ b/accessibility-rs/src/engine/rules/rule.rs @@ -5,7 +5,7 @@ use crate::engine::rules::wcag_base::{Guideline, IssueType, Principle}; use crate::ElementRef; /// the validation response -#[derive(Default)] +#[derive(Default, Debug)] pub struct Validation { /// is valid pub valid: bool, diff --git a/accessibility-rs/src/engine/rules/techniques.rs b/accessibility-rs/src/engine/rules/techniques.rs index 0a9d20d..774989e 100644 --- a/accessibility-rs/src/engine/rules/techniques.rs +++ b/accessibility-rs/src/engine/rules/techniques.rs @@ -1,4 +1,3 @@ -use std::vec; use strum_macros::IntoStaticStr; #[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug, IntoStaticStr)] @@ -28,6 +27,8 @@ pub enum Techniques { F41, /// F47, + /// + F77 } impl Techniques { @@ -35,25 +36,4 @@ impl Techniques { pub fn as_str(&self) -> &'static str { self.into() } - /// get pairs for a rule - pub fn pairs(&self) -> Vec<&'static str> { - match self { - Techniques::H25 => vec!["H25.1.NoTitleEl", "H25.1.EmptyTitle"], - Techniques::H30 => vec!["H30.2"], - Techniques::H32 => vec!["H32.2"], - Techniques::H36 => vec!["H36"], - Techniques::H37 => vec!["H37"], - Techniques::H42 => vec!["H42.2"], - Techniques::H57 => vec!["H57.2", "H57.3.Lang", "H57.3.XmlLang"], - Techniques::H64 => vec!["H64.1", "H64.2"], - Techniques::H91 => vec![ - "H91.A.NoContent", - "H91.[NodeName].Name", - "H91.[NodeName].Value", - ], - Techniques::F40 => vec!["F40.2"], - Techniques::F41 => vec!["F41.2"], - Techniques::F47 => vec!["F47"], - } - } } diff --git a/accessibility-rs/src/engine/rules/utils/nodes.rs b/accessibility-rs/src/engine/rules/utils/nodes.rs index dbc65e7..0223f7a 100644 --- a/accessibility-rs/src/engine/rules/utils/nodes.rs +++ b/accessibility-rs/src/engine/rules/utils/nodes.rs @@ -1,6 +1,6 @@ use crate::engine::rules::rule::Validation; use crate::ElementRef; -use accessibility_scraper::Selector; +use accessibility_scraper::{node, Selector}; use selectors::Element; use slotmap::DefaultKey; @@ -25,13 +25,26 @@ pub fn has_alt(ele: ElementRef<'_>) -> bool { } /// elements empty -pub fn is_empty(nodes: &ElementNodes) -> bool { - let mut empty = false; +pub fn is_empty(nodes: &ElementNodes) -> (bool, Vec) { + let mut valid = true; + let mut elements = Vec::new(); + for ele in nodes { let ele = ele.0; - empty = ele.inner_html().trim().is_empty(); + let empty = ele.inner_html().trim().is_empty(); + if empty { + valid = false; + elements.push(get_unique_selector(&ele)) + } } - empty + + (valid, elements) +} + +/// elements empty with validation +pub fn validate_empty_nodes(nodes: &ElementNodes, id: &'static str) -> Validation { + let (valid, elements) = is_empty(&nodes); + Validation::new(valid, id, elements, "") } /// check if the selector only exist for one element diff --git a/accessibility-rs/src/engine/rules/wcag_rule_map.rs b/accessibility-rs/src/engine/rules/wcag_rule_map.rs index 7e79c60..aae9f8b 100644 --- a/accessibility-rs/src/engine/rules/wcag_rule_map.rs +++ b/accessibility-rs/src/engine/rules/wcag_rule_map.rs @@ -1,7 +1,7 @@ use crate::engine::rules::rule::{Rule, Validation}; use crate::engine::rules::techniques::Techniques; use crate::engine::rules::utils::nodes::{ - get_unique_selector, has_alt, is_empty, validate_missing_attr, + get_unique_selector, has_alt, validate_empty_nodes, validate_missing_attr, }; use crate::engine::rules::wcag_base::{Guideline, IssueType, Principle}; use accessibility_scraper::Selector; @@ -10,7 +10,7 @@ use std::collections::BTreeMap; // todo: validate each element and add a shape that can prevent repitiion lazy_static! { - /// a list of rules that should be applied for WCAG1 + /// a list of rules that should be applied for WCAG1 A-AAA pub static ref RULES_A: BTreeMap<&'static str, Vec> = vec![ ("html", Vec::from([ @@ -210,39 +210,39 @@ lazy_static! { valid = alt; } - Validation::new(valid, Techniques::H37.pairs()[0], elements, "") + Validation::new(valid, "", elements, "") }), ])), ("h1", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), ])), ("h2", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), ])), ("h3", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), ])), ("h4", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), ])), ("h5", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), ])), ("h6", Vec::from([ Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |_rule, nodes| { - Validation::new_issue(!is_empty(nodes), Techniques::H42.pairs()[0]) + validate_empty_nodes(nodes, "2") }), - ])) + ])), ] .into_iter() .collect(); diff --git a/accessibility-rs/tests/unit/heading.rs b/accessibility-rs/tests/unit/heading.rs index 2b355e1..a848bfa 100644 --- a/accessibility-rs/tests/unit/heading.rs +++ b/accessibility-rs/tests/unit/heading.rs @@ -64,7 +64,7 @@ fn _audit_headings_empty() {

The orange is a hybrid of ancient cultivated origin...

Banana is the common name for herbaceous plants ...

-

+

A vegetable is an edible plant or part of a plant other than a sweet fruit ...

diff --git a/accessibility-rs/tests/unit/img.rs b/accessibility-rs/tests/unit/img.rs index 599ccf7..5d65201 100644 --- a/accessibility-rs/tests/unit/img.rs +++ b/accessibility-rs/tests/unit/img.rs @@ -1,5 +1,4 @@ -//! Test for anchors. - +//! Test for img elements. use accessibility_rs::AuditConfig; #[test] @@ -46,7 +45,6 @@ fn _audit_form_submit_img_missing_alt() { let mut valid = true; for x in &audit { - println!("{:?}", x); if x.code == "WCAGAAA.Principle1.Guideline1_1.H36" { valid = false; break;