Skip to content

Commit

Permalink
chore(selectors): add start of gathering element selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 13, 2023
1 parent 7e03204 commit b31a586
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 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 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.19"
version = "0.0.20"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions accessibility-rs/src/engine/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Issue {
/// the context of the issue or raw html
pub context: String,
/// the selector to identify the issue with css, xpath, or raw path
pub selectors: Vec<&'static str>,
pub selectors: Vec<String>,
/// the type of code for the issue
pub code: String,
/// the type of issue
Expand All @@ -56,7 +56,7 @@ impl Issue {
context: &str,
code: &str,
issue_type: &'static str,
selectors: Vec<&'static str>,
selectors: Vec<String>,
) -> Issue {
Issue {
message,
Expand Down
4 changes: 2 additions & 2 deletions accessibility-rs/src/engine/rules/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Validation {
/// the sub-technique
pub id: &'static str,
/// elements that match the issue
pub elements: Vec<&'static str>,
pub elements: Vec<String>,
/// the message of the error
pub message: &'static str,
}
Expand All @@ -22,7 +22,7 @@ impl Validation {
pub fn new(
valid: bool,
id: &'static str,
elements: Vec<&'static str>,
elements: Vec<String>,
message: &'static str,
) -> Self {
Self {
Expand Down
45 changes: 41 additions & 4 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::engine::rules::rule::{Rule, Validation};
use crate::engine::rules::techniques::Techniques;
use crate::engine::rules::wcag_base::{Guideline, IssueType, Principle};
use crate::ElementRef;
use accessibility_scraper::Selector;
use selectors::Element;
use slotmap::DefaultKey;
use std::collections::BTreeMap;
use crate::ElementRef;

type ElementNodes<'a> = Vec<(ElementRef<'a>, Option<DefaultKey>)>;

/// a valid alt attribute for image
fn has_alt(ele: ElementRef<'_>) -> bool {
Expand All @@ -26,7 +28,7 @@ fn has_alt(ele: ElementRef<'_>) -> bool {
}

/// elements empty
fn is_empty(nodes: &Vec<(ElementRef<'_>, Option<DefaultKey>)>) -> bool {
fn is_empty(nodes: &ElementNodes) -> bool {
let mut empty = false;
for ele in nodes {
let ele = ele.0;
Expand All @@ -35,6 +37,41 @@ fn is_empty(nodes: &Vec<(ElementRef<'_>, Option<DefaultKey>)>) -> bool {
empty
}

/// get the unique selector for an element
fn get_unique_selector(ele: &ElementRef<'_>) -> String {
let mut selector = String::new();

if ele.has_attribute("id") {
selector = ele.attr("id").unwrap_or_default().to_string()
}

if selector.is_empty() && ele.has_attribute("class") {
selector = ele.local_name().to_string();
}

if selector.is_empty() {
// TODO: get parent selector for node if any
selector = ele.value().name().to_string();
}

selector
}

/// validate missing title
fn validate_missing_title(nodes: &ElementNodes, id: &'static str) -> Validation {
let mut elements = Vec::new();
let mut valid = true;

nodes.iter().for_each(|e| {
if e.0.attr("title").unwrap_or_default().is_empty() {
valid = false;
elements.push(get_unique_selector(&e.0))
}
});

Validation::new(valid, id, elements, "")
}

// todo: validate each element and add a shape that can prevent repitiion
lazy_static! {
/// a list of rules that should be applied for WCAG1
Expand Down Expand Up @@ -119,12 +156,12 @@ lazy_static! {
])),
("iframe", Vec::from([
Rule::new(Techniques::H64, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |_rule, nodes| {
Validation::new_issue(nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "")
validate_missing_title(nodes, "1")
}),
])),
("frame", Vec::from([
Rule::new(Techniques::H64, IssueType::Error, Principle::Operable, Guideline::Navigable, "1", |_rule, nodes| {
Validation::new_issue(nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "")
validate_missing_title(nodes, "1")
}),
])),
("form", Vec::from([
Expand Down
4 changes: 3 additions & 1 deletion accessibility-rs/tests/unit/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ fn _iframe_missing_title() {
}
}

// this should be valid
assert_eq!(valid, true);

let config = AuditConfig::new(
r###"<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A simple frameset document</title>
</head>
<frameset cols="10%, 90%">
<frame src="nav.html" />
<frame src="doc.html" />
<frame src="doc.html" id="doc" />
<noframes>
<body>
<a href="lib.html" title="Library link">Select to
Expand Down

0 comments on commit b31a586

Please sign in to comment.