Skip to content

Commit

Permalink
chore(rule): add html lang validation
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 9, 2023
1 parent c548af9 commit bb22577
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
61 changes: 47 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ List of techniques we want to have and whether we have it handled or not for WCA
| -------------------------------------------------- | -------------------------- | -------- |
| [H25](https://www.w3.org/TR/WCAG20-TECHS/H25.html) | empty titles ||
| [H32](https://www.w3.org/TR/WCAG20-TECHS/H32.html) | missing form submit button ||
| [H57](https://www.w3.org/TR/WCAG20-TECHS/H57.html) | html contains valid lang ||

### WCAGAA

Expand Down
4 changes: 3 additions & 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.5"
version = "0.0.6"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -26,6 +26,8 @@ accessibility-tree = { version = "0.0.1", path = "../accessibility-tree/victor"
markup5ever = "0.11.0"
cssparser = { workspace = true }
slotmap = "1.0.6"
strum = "0.25"
strum_macros = "0.25"

[dev-dependencies]
wasm-bindgen-test = "0.3.37"
Expand Down
4 changes: 4 additions & 0 deletions accessibility-rs/src/engine/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub struct Clip {
pub struct RunnerExtras {
/// the url to get more information on the issue
pub help_url: &'static str,
/// a detailed description of the issue
pub description: &'static str,
/// the impact level criteria
pub impact: &'static str,
}

/// issue details
Expand Down
12 changes: 7 additions & 5 deletions accessibility-rs/src/engine/rules/ids.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
use std::vec;
use strum_macros::IntoStaticStr;

#[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug)]
#[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug, IntoStaticStr)]
/// techniques for WCAG <https://www.w3.org/TR/WCAG20-TECHS/>
pub enum Techniques {
/// <https://www.w3.org/TR/WCAG20-TECHS/H25.html>
H25,
/// <https://www.w3.org/TR/WCAG20-TECHS/H32.html>
H32,
/// <https://www.w3.org/TR/WCAG20-TECHS/H57>
H57
}

impl Techniques {
/// get rule id to string
pub fn as_str(&self) -> &'static str {
match self {
Techniques::H25 => "H25",
Techniques::H32 => "H32",
}
// todo: make macro
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::H32 => vec!["H32.2"],
Techniques::H57 => vec!["H57.2", "H57.3.Lang", "H57.3.XmlLang"],
}
}
}
3 changes: 3 additions & 0 deletions accessibility-rs/src/engine/rules/wcag_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl Principle {
pub enum Guideline {
/// Provide ways to help users navigate, find content, and determine where they are.
Navigable,
/// Make text content readable and understandable.
Readable,
/// Make Web pages appear and operate in predictable ways.
Predictable,
}
Expand All @@ -55,6 +57,7 @@ impl Guideline {
pub fn as_str(&self) -> &'static str {
match self {
Guideline::Navigable => "Guideline2_4",
Guideline::Readable => "Guideline3_1",
Guideline::Predictable => "Guideline3_2",
}
}
Expand Down
9 changes: 9 additions & 0 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ lazy_static! {
/// a list of rules that should be applied for WCAG1
pub static ref RULES_A: BTreeMap<&'static str, Vec<Rule>> =
vec![
("html", Vec::from([
Rule::new(Techniques::H57, Criteria::Error, Principle::Understandable, Guideline::Readable, |_rule, nodes| {
(!nodes[0].0.attr("lang").unwrap_or_default().is_empty(), "2", Default::default())
}),
Rule::new(Techniques::H57, Criteria::Error, Principle::Understandable, Guideline::Readable, |_rule, nodes| {
let lang = nodes[0].0.attr("lang").unwrap_or_default();
(lang.chars().all(|x| x.is_alphanumeric()) && !lang.contains("_"), "3.Lang", Default::default())
}),
])),
// empty titles
("title", Vec::from([
Rule::new(Techniques::H25, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, nodes| {
Expand Down
5 changes: 4 additions & 1 deletion accessibility-rs/src/i18n/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ lazy_static! {
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.", "", "")),
(Techniques::H25.pairs()[1], Messages::new(&"The title element in the head section should be non-empty.", "", "")),
(Techniques::H32.pairs()[0], Messages::new(&r###"Form does not contain a submit button (input type="submit", input type="image", or button type="submit")."###, "", ""))
(Techniques::H32.pairs()[0], Messages::new(&r###"Form does not contain a submit button (input type="submit", input type="image", or button type="submit")."###, "", "")),
(Techniques::H57.pairs()[0], Messages::new(&"The html element should have a lang or xml:lang attribute which describes the language of the document.", "", "")),
(Techniques::H57.pairs()[1], Messages::new(&"The language specified in the lang attribute of the document element does not appear to be well-formed.", "", "")),
(Techniques::H57.pairs()[2], Messages::new(&"The language specified in the xml:lang attribute of the document element does not appear to be well-formed.", "", "")),
])
};
}

0 comments on commit bb22577

Please sign in to comment.