Skip to content

Commit

Permalink
feat(rules): add duplicate ID [F77]
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 14, 2023
1 parent 6b48b50 commit 447b0db
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
5 changes: 4 additions & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ List of [WCAG2.1 techniques](https://www.w3.org/TR/WCAG21/) and whether or not w
| [F40](https://www.w3.org/TR/WCAG20-TECHS/F40.html) | meta redirect used with a time limit | A-AAA | error | 2 ||
| [F41](https://www.w3.org/TR/WCAG20-TECHS/F41.html) | meta refresh used to reload the page | A-AAA | error | 2 ||
| [F47](https://www.w3.org/TR/WCAG20-TECHS/F47.html) | blink element used for attention | A-AAA | error | ||
| [F77](https://www.w3.org/TR/WCAG20-TECHS/F77.html) | duplicate ID found | A-AAA | error | | ✔️ |

Errors that can be to be tested with automation `13/70`.
Errors that can be to be tested with automation `14/70`.

Key: ✅ = Complete, ✔️ = Complete with a bit of missing details.
2 changes: 1 addition & 1 deletion accessibility-rs/src/engine/rules/techniques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum Techniques {
/// <https://www.w3.org/TR/WCAG20-TECHS/F47>
F47,
/// <https://www.w3.org/TR/WCAG20-TECHS/F77>
F77
F77,
}

impl Techniques {
Expand Down
43 changes: 42 additions & 1 deletion accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use crate::engine::rules::utils::nodes::{
get_unique_selector, has_alt, validate_empty_nodes, validate_missing_attr,
};
use crate::engine::rules::wcag_base::{Guideline, IssueType, Principle};
use accessibility_scraper::Selector;
use accessibility_scraper::{ElementRef, Selector};
use selectors::Element;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::ops::Add;

// todo: validate each element and add a shape that can prevent repitiion
lazy_static! {
Expand Down Expand Up @@ -42,6 +44,45 @@ lazy_static! {
alphabetic && lang.len() < 12
}, "3.XmlLang")
}),
Rule::new(Techniques::F77, IssueType::Error, Principle::Robust, Guideline::Compatible, "1", |_rule, nodes| {
let mut id_map: HashMap<&str, u8> = HashMap::new();
let mut valid = true;

for item in nodes {
let ele = item.0;
let tree = ele.tree();
for e in tree.nodes() {
match ElementRef::wrap(e) {
Some(element) => {
match element.value().id() {
Some(s) => {
if id_map.contains_key(s) {
let u = id_map.get(s);
match u {
Some(u) => {
valid = false;
id_map.insert(s, u.add(1));
}
_ => ()
}
} else {
id_map.insert(s, 1);
}
}
_ => ()
}
}
_ => (),
}
}
}

let duplicate_ids = id_map.into_iter().filter_map(|(id, size)| if size >= 1 { Some(id.to_string()) } else { None }).collect();

// let message = t!(&crate::i18n::locales::get_message_i18n_str(_rule, ""));

Validation::new(valid, "", duplicate_ids, "")
}),
])),
("meta", Vec::from([
Rule::new(Techniques::F40, IssueType::Error, Principle::Operable, Guideline::EnoughTime, "1", |_rule, nodes| {
Expand Down
11 changes: 9 additions & 2 deletions accessibility-rs/src/i18n/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Langs {
}

/// get message config type
pub fn get_message_i18n(rule: &Rule, section: &str, lang: &str) -> String {
pub fn get_message_i18n_str(rule: &Rule, section: &str) -> String {
// todo: add criteria handling fix
let base = [rule.guideline.as_index(), rule.success_criteria].join("_") + "_";
let message = if section.is_empty() {
Expand All @@ -74,5 +74,12 @@ pub fn get_message_i18n(rule: &Rule, section: &str, lang: &str) -> String {
};
let message = [base.as_str(), message.as_str()].join("").to_string();

t!(&message, lang = lang)
message
}

/// get message config type
pub fn get_message_i18n(rule: &Rule, section: &str, lang: &str) -> String {
let message = get_message_i18n_str(rule, section);

t!(&message, locale = lang)
}
29 changes: 29 additions & 0 deletions accessibility-rs/tests/unit/html.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Test generic html elements
use accessibility_rs::AuditConfig;

#[test]
/// duplicate html elements
fn _audit_duplicate_element_id() {
let audit = accessibility_rs::audit(AuditConfig::basic(
r###"<html lang="en">
<head>
<title>Duplicate ID: Do not Use.</title>
</head>
<body>
<div id="dog"></div>
<div id="dog"></div>
</body>
</html>"###,
));
let mut valid = true;

for x in &audit {
println!("{:?}", x);
if x.code == "WCAGAAA.Principle4.Guideline4_1.F77" {
valid = false;
break;
}
}

assert_eq!(valid, false)
}
1 change: 1 addition & 0 deletions accessibility-rs/tests/unit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod anchor;
pub mod heading;
pub mod html;
pub mod img;
pub mod meta;

0 comments on commit 447b0db

Please sign in to comment.