Skip to content

Commit

Permalink
chore(engine): add multi technique setup
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 16, 2023
1 parent 2bac9a8 commit 68d0bc5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 32 deletions.
2 changes: 1 addition & 1 deletion accessibility-rs/src/engine/audit/wcag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl WCAGAAA {
"WCAGAAA",
rule.principle.as_str(),
rule.guideline.as_str(),
rule.rule_id.as_str(),
&rule.rule_id.into_str(),
]
.join("."),
rule.issue_type.as_str(),
Expand Down
40 changes: 38 additions & 2 deletions accessibility-rs/src/engine/rules/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,47 @@ impl Validation {
}
}

#[derive(Debug, Clone)]
/// techniques for a rule
pub enum Technique {
/// a single technique
Single(Techniques),
/// multiple techniques
Multi(Vec<Techniques>),
}

impl Technique {
/// technique(s) into string
pub fn into_str(&self) -> String {
match self {
Technique::Multi(tech) => tech
.iter()
.map(|x| x.as_str())
.collect::<Vec<_>>()
.join(",")
.into(),
Technique::Single(tech) => tech.as_str().into(),
}
}
}

impl From<Techniques> for Technique {
fn from(t: Techniques) -> Self {
Technique::Single(t)
}
}

impl From<Vec<Techniques>> for Technique {
fn from(t: Vec<Techniques>) -> Self {
Technique::Multi(t)
}
}

/// the rule validation method that should be performed.
#[derive(Debug)]
pub struct Rule {
/// the message id of the rule to point to the locale
pub rule_id: Techniques,
pub rule_id: Technique,
/// the type of rule
pub issue_type: IssueType,
/// validate a test returns (valid, rule, selectors)
Expand All @@ -57,7 +93,7 @@ pub struct Rule {
impl Rule {
/// a new rule type
pub fn new(
rule_id: Techniques,
rule_id: Technique,
issue_type: IssueType,
principle: Principle,
guideline: Guideline,
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/src/engine/rules/techniques.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum_macros::IntoStaticStr;

#[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug, 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/H25>
Expand Down
46 changes: 23 additions & 23 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ lazy_static! {
pub static ref RULES_A: BTreeMap<&'static str, Vec<Rule>> =
vec![
("html", Vec::from([
Rule::new(Techniques::H57, IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
Rule::new(Techniques::H57.into(), IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
let n = nodes[0].0;
Validation::new_issue(!n.attr("lang").unwrap_or_default().is_empty() || !n.attr("xml:lang").unwrap_or_default().is_empty(), "2")
}),
Rule::new(Techniques::H57, IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
Rule::new(Techniques::H57.into(), IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
let lang = nodes[0].0.attr("lang").unwrap_or_default();
let alphabetic = lang.chars().all(|x| x.is_alphabetic());
// <https://www.rfc-editor.org/rfc/bcp/bcp47.txt>
Expand All @@ -32,7 +32,7 @@ lazy_static! {
alphabetic && lang.len() < 12
}, "3.Lang")
}),
Rule::new(Techniques::H57, IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
Rule::new(Techniques::H57.into(), IssueType::Error, Principle::Understandable, Guideline::Readable, "1", |nodes, _lang| {
let lang = nodes[0].0.attr("xml:lang").unwrap_or_default();
let alphabetic = lang.chars().all(|x| x == '_' || x.is_alphabetic());
// <https://www.rfc-editor.org/rfc/bcp/bcp47.txt>
Expand All @@ -44,7 +44,7 @@ lazy_static! {
alphabetic && lang.len() < 12
}, "3.XmlLang")
}),
Rule::new(Techniques::F77, IssueType::Error, Principle::Robust, Guideline::Compatible, "1", |nodes, lang| {
Rule::new(Techniques::F77.into(), IssueType::Error, Principle::Robust, Guideline::Compatible, "1", |nodes, lang| {
let mut id_map: HashMap<&str, u8> = HashMap::new();
let mut valid = true;

Expand Down Expand Up @@ -84,7 +84,7 @@ lazy_static! {
}),
])),
("meta", Vec::from([
Rule::new(Techniques::F40, IssueType::Error, Principle::Operable, Guideline::EnoughTime, "1", |nodes, _lang| {
Rule::new(Techniques::F40.into(), IssueType::Error, Principle::Operable, Guideline::EnoughTime, "1", |nodes, _lang| {
let mut valid = true;

for node in nodes {
Expand All @@ -100,7 +100,7 @@ lazy_static! {

Validation::new_issue(valid, "2")
}),
Rule::new(Techniques::F41, IssueType::Error, Principle::Understandable, Guideline::EnoughTime, "1", |nodes, _lang| {
Rule::new(Techniques::F41.into(), IssueType::Error, Principle::Understandable, Guideline::EnoughTime, "1", |nodes, _lang| {
let mut valid = true;

for node in nodes {
Expand All @@ -118,30 +118,30 @@ lazy_static! {
}),
])),
("title", Vec::from([
Rule::new(Techniques::H25, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Rule::new(Techniques::H25.into(), IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Validation::new_issue(!nodes.is_empty(), "1.NoTitleEl")
}),
Rule::new(Techniques::H25, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Rule::new(Techniques::H25.into(), IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Validation::new_issue(nodes.is_empty() || nodes[0].0.html().is_empty(), "1.EmptyTitle")
}),
])),
("blink", Vec::from([
Rule::new(Techniques::F47, IssueType::Error, Principle::Operable, Guideline::EnoughTime, "2", |nodes, _lang| {
Rule::new(Techniques::F47.into(), IssueType::Error, Principle::Operable, Guideline::EnoughTime, "2", |nodes, _lang| {
Validation::new_issue(nodes.is_empty(), "")
}),
])),
("iframe", Vec::from([
Rule::new(Techniques::H64, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Rule::new(Techniques::H64.into(), IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
validate_missing_attr(nodes, "title", "1")
}),
])),
("frame", Vec::from([
Rule::new(Techniques::H64, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
Rule::new(Techniques::H64.into(), IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |nodes, _lang| {
validate_missing_attr(nodes, "title", "1")
}),
])),
("form", Vec::from([
Rule::new(Techniques::H32, IssueType::Error, Principle::Operable, Guideline::Predictable, "2", |nodes, _lang| {
Rule::new(Techniques::H32.into(), IssueType::Error, Principle::Operable, Guideline::Predictable, "2", |nodes, _lang| {
let mut valid = false;
let mut elements = Vec::new();
let selector = unsafe { Selector::parse("button[type=submit]").unwrap_unchecked() };
Expand All @@ -161,7 +161,7 @@ lazy_static! {

Validation::new(valid, "2", elements, Default::default())
}),
Rule::new(Techniques::H36, IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
Rule::new(Techniques::H36.into(), IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
let mut valid = false;
let mut elements = Vec::new();
let selector = unsafe { Selector::parse("input[type=image][name=submit]").unwrap_unchecked() };
Expand All @@ -183,7 +183,7 @@ lazy_static! {
}),
])),
("a", Vec::from([
Rule::new(Techniques::H30, IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
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() };
let mut elements = Vec::new();
Expand All @@ -203,7 +203,7 @@ lazy_static! {

Validation::new(valid, "2", elements, Default::default())
}),
Rule::new(Techniques::H91, IssueType::Error, Principle::Robust, Guideline::Compatible, "2", |nodes, _lang| {
Rule::new(Techniques::H91.into(), IssueType::Error, Principle::Robust, Guideline::Compatible, "2", |nodes, _lang| {
let mut valid = true;
let mut elements = Vec::new();

Expand All @@ -222,7 +222,7 @@ lazy_static! {
}
Validation::new(valid, "A.NoContent", elements, Default::default())
}),
Rule::new(Techniques::H91, IssueType::Error, Principle::Robust, Guideline::Compatible, "2", |nodes, _lang| {
Rule::new(Techniques::H91.into(), IssueType::Error, Principle::Robust, Guideline::Compatible, "2", |nodes, _lang| {
let mut valid = true;
let mut elements = Vec::new();
for ele in nodes {
Expand All @@ -237,7 +237,7 @@ lazy_static! {
}),
])),
("img", Vec::from([
Rule::new(Techniques::H37, IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
Rule::new(Techniques::H37.into(), IssueType::Error, Principle::Perceivable, Guideline::TextAlternatives, "1", |nodes, _lang| {
let mut valid = true;
let mut elements = Vec::new();

Expand All @@ -254,32 +254,32 @@ lazy_static! {
}),
])),
("h1", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
("h2", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
("h3", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
("h4", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
("h5", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
("h6", Vec::from([
Rule::new(Techniques::H42, IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
Rule::new(Techniques::H42.into(), IssueType::Error, Principle::Perceivable, Guideline::Adaptable, "1", |nodes, _lang| {
validate_empty_nodes(nodes, "2")
}),
])),
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/src/i18n/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn get_message_i18n_str_raw(
pub fn get_message_i18n_str(rule: &Rule, section: &str) -> String {
get_message_i18n_str_raw(
&rule.guideline,
rule.rule_id.as_str(),
&rule.rule_id.into_str(),
rule.success_criteria,
section,
)
Expand Down
7 changes: 3 additions & 4 deletions accessibility-rs/tests/unit/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn _audit_duplicate_element_id() {
assert_eq!(valid, false)
}


#[test]
/// duplicate html elements with locale "ja"
fn _audit_duplicate_element_id_ja() {
Expand All @@ -41,9 +40,9 @@ fn _audit_duplicate_element_id_ja() {
<div id="dog"></div>
</body>
</html>"###,
"",
false,
"ja"
"",
false,
"ja",
));
let mut valid = true;

Expand Down

0 comments on commit 68d0bc5

Please sign in to comment.