From 4fe8c08f09af7f99ac788a9350bee058451986a9 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Tue, 3 Oct 2023 11:20:17 -0400 Subject: [PATCH] chore(innate): add empty title test --- kayle/tests/innate-playwright.spec.ts | 1 - kayle/tests/innate.ts | 1 - kayle_innate/src/engine/rules/wcag.rs | 18 ++++++++++-------- kayle_innate/src/lib.rs | 16 +++++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/kayle/tests/innate-playwright.spec.ts b/kayle/tests/innate-playwright.spec.ts index c06ed98..ca90797 100644 --- a/kayle/tests/innate-playwright.spec.ts +++ b/kayle/tests/innate-playwright.spec.ts @@ -21,7 +21,6 @@ test("kayle_innate, fast_htmlcs, fast_axecore, and ace audit drakeMock profiling }); const mock = html.replace("Drake Industries | Custom, Durable, High-Quality Labels, Asset Tags and Custom Server Bezels", "") const startTime = performance.now(); - // 8 - after building end engine optimized most likely will be at 12 ms const audit = await _audit_not_ready(mock, css); const nextTime = performance.now() - startTime; console.log("Rust/WASM TIME ", nextTime); diff --git a/kayle/tests/innate.ts b/kayle/tests/innate.ts index 7dc0ebf..379f052 100644 --- a/kayle/tests/innate.ts +++ b/kayle/tests/innate.ts @@ -20,7 +20,6 @@ import { _audit_not_ready } from "kayle_innate"; }); const mock = html.replace("Drake Industries | Custom, Durable, High-Quality Labels, Asset Tags and Custom Server Bezels", "") const startTime = performance.now(); - // 8 - after building end engine optimized most likely will be at 12 ms const audit = await _audit_not_ready(mock, css); const nextTime = performance.now() - startTime; console.log("Rust/WASM TIME ", nextTime); diff --git a/kayle_innate/src/engine/rules/wcag.rs b/kayle_innate/src/engine/rules/wcag.rs index b1206e7..ff76c2a 100644 --- a/kayle_innate/src/engine/rules/wcag.rs +++ b/kayle_innate/src/engine/rules/wcag.rs @@ -66,7 +66,7 @@ struct Rule { /// validate a test pub validate: fn( &String, - &Vec, + &Vec, css: &cssparser::Parser<'_, '_>, ) -> (bool, &'static str), /// the principle type @@ -84,7 +84,7 @@ impl Rule { guideline: Guideline, validate: fn( &String, - &Vec, + &Vec, &cssparser::Parser<'_, '_>, ) -> (bool, &'static str), ) -> Rule { @@ -101,13 +101,14 @@ impl Rule { lazy_static! { /// a list of rules that should be applied for WCAG1 static ref RULES_A: BTreeMap<&'static str, Vec> = + // empty titles vec![("title", Vec::from([ Rule::new(RuleID::H25, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, elements, _css_parser| { (!elements.is_empty(), "1.NoTitleEl") }), - // Rule::new(RuleID::H25, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, elements, _css_parser| { - // (!elements.is_empty(), "1.EmptyTitle") - // }), + Rule::new(RuleID::H25, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, elements, _css_parser| { + (elements.is_empty() || elements[0].as_text().unwrap_or(&scraper::node::Text { text: "".into() }).text.is_empty(), "1.EmptyTitle") + }), ]))] .into_iter() .collect(); @@ -122,7 +123,7 @@ impl WCAG3AA { /// init the rules pub fn audit( // allow tree mutation until threads or setup the tree with initial elements. - mut tree: std::collections::BTreeMap>, + mut tree: std::collections::BTreeMap>, _css: cssparser::Parser<'_, '_>, // todo: get configs like viewport ) -> Vec { @@ -161,11 +162,12 @@ impl WCAG3AA { ); issues.push(issue); } - + console_log!( - "RULE {:?} {:?} Valid: {:?}", + "RULE {:?} {:?} {:?} Valid: {:?}", rule.rule_id, rule.criteria, + section, valid ); } diff --git a/kayle_innate/src/lib.rs b/kayle_innate/src/lib.rs index 8ea80e5..a6ce701 100644 --- a/kayle_innate/src/lib.rs +++ b/kayle_innate/src/lib.rs @@ -153,7 +153,9 @@ pub fn get_document_links(res: &str, domain: &str) -> Box<[JsValue]> { pub fn parse_accessibility_tree( html: &str, // todo: return the nodes with a tuple of the layout node and the element node -) -> std::collections::BTreeMap> { +) -> std::collections::BTreeMap> { + use scraper::Node; + console_log!("Starting accessibility tree parsing. This is incomplete and should not be used in production."); // use taffy::prelude::*; // // todo: use optional variable for clips or layout creation @@ -203,16 +205,16 @@ pub fn parse_accessibility_tree( // parse doc will start from html downwards let h = scraper::Html::parse_document(html); // accessibility tree for ordered element mappings - let mut accessibility_tree: BTreeMap> = BTreeMap::new(); - let mut hh = h.tree.nodes(); + let mut accessibility_tree: BTreeMap> = BTreeMap::new(); + let nodes = h.tree.into_iter(); - while let Some(node) = hh.next() { - if let Some(element) = node.value().as_element() { + for node in nodes { + if let Some(element) = node.as_element() { let element_name = element.name(); accessibility_tree .entry(element_name.to_string()) - .and_modify(|n| n.push(element.to_owned())) - .or_insert(Vec::from([element.to_owned()])); + .and_modify(|n| n.push(node.clone())) + .or_insert(Vec::from([node])); } }