diff --git a/accessibility-rs/src/engine/rules/wcag_rule_map.rs b/accessibility-rs/src/engine/rules/wcag_rule_map.rs index 803d7a1..5e00d60 100644 --- a/accessibility-rs/src/engine/rules/wcag_rule_map.rs +++ b/accessibility-rs/src/engine/rules/wcag_rule_map.rs @@ -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; @@ -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(); }