Skip to content

Commit

Permalink
chore(headings): add element selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 14, 2023
1 parent 15b60c6 commit 6b48b50
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 45 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.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
```

Expand Down
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.22"
version = "0.0.24"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions accessibility-rs/src/engine/audit/wcag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl WCAG3AA {
auditor: &Auditor<'_>,
) -> Vec<Issue> {
let mut issues: Vec<Issue> = 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 {
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/src/engine/rules/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 2 additions & 22 deletions accessibility-rs/src/engine/rules/techniques.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::vec;
use strum_macros::IntoStaticStr;

#[derive(PartialOrd, Ord, std::cmp::Eq, PartialEq, Hash, Debug, IntoStaticStr)]
Expand Down Expand Up @@ -28,32 +27,13 @@ pub enum Techniques {
F41,
/// <https://www.w3.org/TR/WCAG20-TECHS/F47>
F47,
/// <https://www.w3.org/TR/WCAG20-TECHS/F77>
F77
}

impl Techniques {
/// get rule id to string
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"],
}
}
}
23 changes: 18 additions & 5 deletions accessibility-rs/src/engine/rules/utils/nodes.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String>) {
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
Expand Down
20 changes: 10 additions & 10 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Rule>> =
vec![
("html", Vec::from([
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/tests/unit/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn _audit_headings_empty() {
<p>The orange is a hybrid of ancient cultivated origin...</p>
<h3> </h3>
<p>Banana is the common name for herbaceous plants ...</p>
<h2> </h2>
<h2 id="empty-h2"> </h2>
<p>A vegetable is an edible plant or part of a plant other than a
sweet fruit ...</p>
<h3> </h3>
Expand Down
4 changes: 1 addition & 3 deletions accessibility-rs/tests/unit/img.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Test for anchors.
//! Test for img elements.
use accessibility_rs::AuditConfig;

#[test]
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6b48b50

Please sign in to comment.