-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(parser): add fork parser scraper * chore(scraper): add style parsing scraper * chore(styles): add parsed styles * chore(innate): add parse inline css if sheet empty * chore(audit): add auditor base
- Loading branch information
Showing
14 changed files
with
993 additions
and
391 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,90 @@ | ||
pub mod css_cache; | ||
pub mod errors; | ||
pub mod rules; | ||
|
||
use crate::console_log; | ||
use cssparser::{Parser, ParserInput}; | ||
use markup5ever::local_name; | ||
use markup5ever::namespace_url; | ||
use markup5ever::ns; | ||
use scraper_forky::selector::Simple; | ||
use scraper_forky::{ElementRef, Html}; | ||
use selectors::matching::MatchingContext; | ||
use std::sync::Arc; | ||
use victor_tree::style::cascade::USER_AGENT_STYLESHEET; | ||
use victor_tree::style::declaration_block::DeclarationBlock; | ||
use victor_tree::style::values::{Direction, WritingMode}; | ||
use victor_tree::style::{ComputedValues, StyleSet}; | ||
|
||
/// get the style for an element | ||
pub fn style_for_element<'a>( | ||
author: &StyleSet, | ||
_document: &Html, | ||
node: ElementRef<'a>, | ||
parent_style: Option<&ComputedValues>, | ||
match_context: &mut MatchingContext<'_, Simple>, | ||
) -> Arc<ComputedValues> { | ||
use smallvec::SmallVec; | ||
let style_attr_block; | ||
let mut matching = victor_tree::style::cascade::MatchingDeclarations { | ||
ua: SmallVec::new(), | ||
author: SmallVec::new(), | ||
}; | ||
|
||
// let mut nth_index_cache = selectors::NthIndexCache::from(Default::default()); | ||
// let mut match_context = selectors::matching::MatchingContext::new( | ||
// selectors::matching::MatchingMode::Normal, | ||
// None, | ||
// Some(&mut nth_index_cache), | ||
// selectors::matching::QuirksMode::NoQuirks, | ||
// // selectors::matching::NeedsSelectorFlags::No, | ||
// // selectors::matching::IgnoreNthChildForInvalidation::No, | ||
// ); | ||
|
||
for &(ref selector, ref block) in &USER_AGENT_STYLESHEET.rules { | ||
if selectors::matching::matches_selector( | ||
selector, | ||
0, | ||
None, | ||
&node, | ||
match_context, | ||
&mut |_, _| {}, | ||
) { | ||
matching.ua.push(block) | ||
} | ||
} | ||
|
||
// push author style sheet | ||
for &(ref selector, ref block) in &author.rules { | ||
if selectors::matching::matches_selector( | ||
selector, | ||
0, | ||
None, | ||
&node, | ||
match_context, | ||
&mut |_, _| {}, | ||
) { | ||
matching.author.push(block) | ||
} | ||
} | ||
|
||
if let ns!(html) | ns!(svg) | ns!(mathml) = node.value().name.ns { | ||
if let Some(style_attr) = node.value().attr(&local_name!("style")) { | ||
let mut input = ParserInput::new(style_attr); | ||
let mut parser = Parser::new(&mut input); | ||
style_attr_block = DeclarationBlock::parse(&mut parser); | ||
matching.author.push(&style_attr_block); | ||
} | ||
} | ||
|
||
let styles = ComputedValues::new(parent_style, Some(&matching)); | ||
|
||
console_log!( | ||
"{:?}", | ||
styles | ||
.box_size() | ||
.size_to_physical((WritingMode::SidewaysLr, Direction::Ltr)) | ||
); | ||
|
||
styles | ||
} |
Oops, something went wrong.