Skip to content

Commit

Permalink
chore(rules): add dup anchor img alt text content [H2]
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 22, 2023
1 parent 06f2375 commit 9538025
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Rust web accessibility engine.

```toml
[dependencies]
accessibility-rs = "^0.0.30"
accessibility-rs = "^0.0.43"
```

```rs
Expand All @@ -28,7 +28,7 @@ let audit = accessibility_rs::audit(&AuditConfig::new(&html, &css, false, "en"))
1. i18n support for multiple languages.
1. Re-creating layout tree to get element position coordinates.

## Benchmarks
## [Benchmarks](./benches/)

```sh
audit-speed/core/audit: small html (4k iterations)
Expand Down
3 changes: 2 additions & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ List of [WCAG2.1 techniques](https://www.w3.org/TR/WCAG21/) and whether or not w

| Technique | Description | WCAG | Type | Name | Complete |
| -------------------------------------------------- | ----------------------------------------------------------------------------- | ----- | ----- | --------------- | -------- |
| [H2](https://www.w3.org/TR/WCAG20-TECHS/H2.html) | img element in link has alt text that duplicates the text content of link | A-AAA | error | EG ||
| [H25](https://www.w3.org/TR/WCAG20-TECHS/H25.html) | empty titles | A-AAA | error | ||
| [H30](https://www.w3.org/TR/WCAG20-TECHS/H30.html) | text alternative img | A-AAA | error | ||
| [H32](https://www.w3.org/TR/WCAG20-TECHS/H32.html) | missing form submit button | A-AAA | error | ||
Expand All @@ -31,6 +32,6 @@ List of [WCAG2.1 techniques](https://www.w3.org/TR/WCAG21/) and whether or not w
| [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 `24/70`.
Errors that can be to be tested with automation `25/70`.

Key: ✅ = Complete, ✔️ = Complete with a bit of missing details.
2 changes: 1 addition & 1 deletion accessibility-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-rs"
version = "0.0.42"
version = "0.0.43"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions accessibility-rs/src/engine/rules/techniques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use strum_macros::IntoStaticStr;
#[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug, IntoStaticStr, Clone)]
/// techniques for WCAG <https://www.w3.org/TR/WCAG20-TECHS/>
pub enum Techniques {
/// <https://www.w3.org/TR/WCAG20-TECHS/H2>
H2,
/// <https://www.w3.org/TR/WCAG20-TECHS/H25>
H25,
/// <https://www.w3.org/TR/WCAG20-TECHS/H32.html>
Expand Down
27 changes: 27 additions & 0 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ lazy_static! {
}),
])),
("a", Vec::from([
Rule::new(Techniques::H2.into(), IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
let mut valid = true;
let selector = unsafe { Selector::parse("img").unwrap_unchecked() };
let mut elements = Vec::new();

for ele in nodes {
let ele = ele.0;
let mut e = ele.select(&selector);

while let Some(el) = e.next() {
let alt = match el.attr("alt") {
Some(s) => s,
_ => "",
};

let text = ele.text().collect::<Vec<_>>().join("");
let text = text.trim();

if alt == text {
valid = false;
elements.push(get_unique_selector(&ele))
}
}
}

Validation::new(valid, "EG5", elements, Default::default()).into()
}),
Rule::new(Techniques::H30.into(), IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
let mut valid = true;
let selector = unsafe { Selector::parse("img").unwrap_unchecked() };
Expand Down
25 changes: 24 additions & 1 deletion accessibility-rs/tests/unit/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn _audit_missing_alt_anchor_img() {
</head>
<body>
<a href="routes.html">
<img src="topo.gif" />
<img src="topo.gif">
</a>
</body>
</html>"###,
Expand Down Expand Up @@ -52,3 +52,26 @@ fn _audit_missing_anchor_content() {

assert_eq!(valid, false)
}

#[test]
/// anchor text matches img alt
fn _audit_img_alt_matches_text_anchor() {
let audit = accessibility_rs::audit(AuditConfig::basic(
r###"<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Decrative Img: Do not use!</title>
</head>
<body>
<a href="routes.html">
<img src="topo.gif" alt="Golf">
Golf
</a>
</body>
</html>"###,
));
let valid = !audit
.iter()
.any(|x| x.code == "WCAGAAA.Principle1.Guideline1_1.H2");

assert_eq!(valid, false)
}

0 comments on commit 9538025

Please sign in to comment.