-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
319 additions
and
271 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use scraper_forky::ElementRef; | ||
use scraper_forky::Html; | ||
use victor_tree::style::StyleSet; | ||
|
||
use super::tree::parse_accessibility_tree; | ||
|
||
/// the intro to an audit | ||
pub struct Auditor<'a> { | ||
/// the html document | ||
pub document: &'a Html, | ||
/// the tree to map to nodes | ||
pub tree: std::collections::BTreeMap<&'a str, Vec<ElementRef<'a>>>, | ||
/// styles for the audit | ||
pub author: StyleSet, | ||
// /// the matching context for css selectors | ||
pub match_context: selectors::matching::MatchingContext<'a, scraper_forky::selector::Simple>, | ||
} | ||
|
||
impl<'a> Auditor<'a> { | ||
pub fn new( | ||
document: &'a Html, | ||
css_rules: &str, | ||
match_context: selectors::matching::MatchingContext<'a, scraper_forky::selector::Simple>, | ||
) -> Auditor<'a> { | ||
use crate::{console_log, now}; | ||
let t = now(); | ||
let tree = parse_accessibility_tree(&document); | ||
console_log!("Tree Build Time {:?}", now() - t); | ||
let tt = now(); | ||
|
||
// TODO: make stylesheet building optional and only on first requirement | ||
let author = { | ||
let mut author = victor_tree::style::StyleSetBuilder::new(); | ||
if !css_rules.is_empty() { | ||
author.add_stylesheet(css_rules); | ||
} else { | ||
use markup5ever::local_name; | ||
match tree.get("style") { | ||
Some(styles) => { | ||
for node in styles { | ||
// https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block | ||
if let Some(type_attr) = node.attr(&local_name!("type")) { | ||
if !type_attr.eq_ignore_ascii_case("text/css") { | ||
continue; | ||
} | ||
author.add_stylesheet(&node.inner_html()) | ||
} | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
author.finish() | ||
}; | ||
|
||
console_log!("StyleSheets Build Time {:?}", now() - tt); | ||
|
||
Auditor { | ||
document, | ||
tree, | ||
author, | ||
match_context, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
/// the auditor | ||
pub mod auditor; | ||
/// the node tree | ||
pub mod tree; | ||
/// WCAG audit | ||
pub mod wcag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use scraper_forky::ElementRef; | ||
use std::collections::BTreeMap; | ||
|
||
/// try to fix all possible issues using a spec against the tree. | ||
pub fn parse_accessibility_tree( | ||
html: &scraper_forky::Html, | ||
// todo: return the nodes with a tuple of the layout node and the element node | ||
) -> std::collections::BTreeMap<&str, Vec<ElementRef<'_>>> { | ||
// use taffy::prelude::*; | ||
// // todo: use optional variable for clips or layout creation | ||
// let mut taffy = Taffy::new(); | ||
|
||
// let header_node = taffy | ||
// .new_leaf(Style { | ||
// size: Size { | ||
// width: points(800.0), | ||
// height: points(100.0), | ||
// }, | ||
// ..Default::default() | ||
// }) | ||
// .unwrap(); | ||
|
||
// let body_node = taffy | ||
// .new_leaf(Style { | ||
// size: Size { | ||
// width: points(800.0), | ||
// height: auto(), | ||
// }, | ||
// flex_grow: 1.0, | ||
// ..Default::default() | ||
// }) | ||
// .unwrap(); | ||
|
||
// let root_node = taffy | ||
// .new_with_children( | ||
// Style { | ||
// flex_direction: FlexDirection::Column, | ||
// size: Size { | ||
// width: points(800.0), | ||
// height: points(600.0), | ||
// }, | ||
// ..Default::default() | ||
// }, | ||
// &[header_node, body_node], | ||
// ) | ||
// .unwrap(); | ||
|
||
// // Call compute_layout on the root of your tree to run the layout algorithm | ||
// taffy.compute_layout(root_node, Size::MAX_CONTENT).unwrap(); | ||
// console_log!("Header Layout {:?}", taffy.layout(header_node).unwrap()); | ||
// We can get the x,y, and height, width of the element on proper tree insert | ||
|
||
// parse doc will start from html downwards | ||
// accessibility tree for ordered element mappings | ||
let mut accessibility_tree: BTreeMap<&str, Vec<ElementRef<'_>>> = | ||
BTreeMap::from([("title".into(), Default::default())]); | ||
|
||
for node in html.tree.nodes() { | ||
match scraper_forky::element_ref::ElementRef::wrap(node) { | ||
Some(element) => { | ||
accessibility_tree | ||
.entry(element.value().name()) | ||
.and_modify(|n| n.push(element)) | ||
.or_insert(Vec::from([element])); | ||
} | ||
_ => (), | ||
}; | ||
} | ||
|
||
// console_log!("Getting tree links {:?}", accessibility_tree.get("a")); | ||
// console_log!("Tree {:?}", accessibility_tree); | ||
|
||
accessibility_tree | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// build matching context | ||
pub fn build_matching_context<'a>( | ||
nth_index_cache: &'a mut selectors::NthIndexCache, | ||
) -> selectors::matching::MatchingContext<'a, scraper_forky::selector::Simple> { | ||
selectors::matching::MatchingContext::new( | ||
selectors::matching::MatchingMode::Normal, | ||
None, | ||
Some(nth_index_cache), | ||
selectors::matching::QuirksMode::NoQuirks, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.