Skip to content

Commit

Permalink
chore(innate): add css matching stub
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 5, 2023
1 parent bcd1311 commit 4fb42e5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions kayle_innate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kayle_innate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ serde-wasm-bindgen = "0.4"
js-sys = "0.3.64"
selectors = "0.25.0"
smallvec = "1.11.1"
ego-tree = "0.6.2"

[dependencies.cssparser]
version = "^0.31.0"
Expand Down
2 changes: 1 addition & 1 deletion kayle_innate/src/engine/audit/wcag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl WCAG3AA {
/// init the rules
pub fn audit(
// allow tree mutation until threads or setup the tree with initial elements.
tree: std::collections::BTreeMap<&str, Vec<ElementRef<'_>>>,
tree: &std::collections::BTreeMap<&str, Vec<ElementRef<'_>>>,
_css: cssparser::Parser<'_, '_>,
// todo: get configs like viewport
) -> Vec<Issue> {
Expand Down
43 changes: 40 additions & 3 deletions kayle_innate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use std::collections::BTreeMap;
use std::collections::HashSet;
use utils::{convert_abs_path, convert_base_path, domain_name, set_panic_hook};

#[cfg(feature = "accessibility")]
use selectors::matching::{MatchingContext, MatchingMode, QuirksMode};

#[cfg(feature = "accessibility")]
#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -231,6 +234,8 @@ pub fn parse_accessibility_tree(
/// audit a web page passing the html and css rules.
pub fn _audit_not_ready(html: &str, _css_rules: &str) -> Result<JsValue, JsValue> {
// majority of time is spent on initial parse_document.

use selectors::NthIndexCache;
let html = scraper::Html::parse_document(html);
let _tree = parse_accessibility_tree(&html);
// TODO: if the css rules are empty extract the css from the HTML
Expand All @@ -244,21 +249,53 @@ pub fn _audit_not_ready(html: &str, _css_rules: &str) -> Result<JsValue, JsValue

let mut rules = Vec::new();

// todo: parse the rules out correctly to nodes and add declarations
// todo: parse the rules out correctly to nodes and add declarations
for result in css_rules_parser {
match result {
Ok(crate::engine::styles::rules::CssRule::StyleRule { selectors, block }) => {
for selector in selectors.0 {
rules.push((selector, block.clone()));
}
}
_ => ()
_ => (),
};
}

console_log!("CSS RULES: {:?}", rules);

let _audit = engine::audit::wcag::WCAG3AA::audit(_tree, _css_parser);
let _audit = engine::audit::wcag::WCAG3AA::audit(&_tree, _css_parser);

// TODO: build struct that can keep lifetimes
// let mut nth_index_cache = NthIndexCache::from(Default::default());
// let mut match_context = MatchingContext::new(
// MatchingMode::Normal,
// None,
// &mut nth_index_cache,
// QuirksMode::NoQuirks,
// selectors::matching::NeedsSelectorFlags::No,
// selectors::matching::IgnoreNthChildForInvalidation::No,
// );

// for item in _tree {
// for node in item.1 {
// let id = node.id();

// // todo: use the css block style to get computations
// for &(ref selector, ref _block) in &rules {
// if selectors::matching::matches_selector(
// selector,
// 0,
// None,
// &node,
// &mut match_context,
// ) {
// console_log!("Style Match {:?}", id);
// // build all the styles for the element based on the match
// // into.push(_block)
// }
// }
// }
// }

// todo: map to JsValues instead of serde
Ok(serde_wasm_bindgen::to_value(&_audit)?)
Expand Down

0 comments on commit 4fb42e5

Please sign in to comment.