Skip to content

Commit

Permalink
chore(selector): add class handling uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 13, 2023
1 parent 4158806 commit 2ed0129
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::engine::rules::techniques::Techniques;
use crate::engine::rules::wcag_base::{Guideline, IssueType, Principle};
use crate::ElementRef;
use accessibility_scraper::Selector;
use ego_tree::NodeRef;
use selectors::Element;
use slotmap::DefaultKey;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -42,14 +43,34 @@ 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()
selector = "#".to_string() + ele.attr("id").unwrap_or_default()
}
// TODO: if id is not found recursively get all elements until complete if class does not match

if selector.is_empty() && ele.has_attribute("class") {
// TODO: check to see if class is a unique selector before setting by using the root tree
selector = ele.value().name().to_string() + &ele.local_name().to_string();
let node_selector = ele.value().name().to_string() + &ele.local_name().to_string();
let only_selector = match ele.tree().root().first_child() {
Some(child) => {
match ElementRef::wrap(child) {
Some(element) => {
match Selector::parse(node_selector.as_str()) {
Ok(s) => {
let e = element.select(&s);
e.count() == 1
}
_ => false
}
}
_ => false
}
}
_ => false
};
if only_selector {
selector = node_selector;
}
}

// TODO: if id is not found recursively get all elements until complete if class does not match
if selector.is_empty() {
selector = ele.value().name().to_string();
}
Expand Down

0 comments on commit 2ed0129

Please sign in to comment.