Skip to content

Commit

Permalink
perf(audit): remove extra tree clone
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 21, 2024
1 parent 65760e8 commit 96ceb1f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion accessibility-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-rs"
version = "0.0.51"
version = "0.0.52"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
24 changes: 13 additions & 11 deletions accessibility-rs/src/engine/audit/auditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ pub struct Auditor<'a> {
pub tree: std::collections::BTreeMap<&'a str, Vec<(ElementRef<'a>, Option<taffy::NodeId>)>>,
/// styles for the audit
pub author: StyleSet,
/// the matching context for css selectors
pub match_context:
selectors::matching::MatchingContext<'a, accessibility_scraper::selector::Simple>,
/// layout handling
pub taffy: Option<TaffyTree>,
/// language to get results in
Expand All @@ -34,7 +31,10 @@ impl<'a> Auditor<'a> {
>,
bounds: bool,
locale: &'a str,
) -> Auditor<'a> {
) -> (
Auditor<'a>,
selectors::matching::MatchingContext<'a, accessibility_scraper::selector::Simple>,
) {
// TODO: make stylesheet building optional and only on first requirement
let author = {
let mut author = accessibility_tree::style::StyleSetBuilder::new();
Expand Down Expand Up @@ -63,13 +63,15 @@ impl<'a> Auditor<'a> {
parse_accessibility_tree(&document, &author, match_context)
};

Auditor {
document,
tree,
author,
(
Auditor {
document,
tree,
author,
taffy,
locale,
},
match_context,
taffy,
locale,
}
)
}
}
12 changes: 9 additions & 3 deletions accessibility-rs/src/engine/audit/wcag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ pub struct WCAGAAA;
/// wcag rules to test for
impl WCAGAAA {
/// audit html against WCAGAAA standards
pub fn audit(mut auditor: &mut Auditor<'_>) -> Vec<Issue> {
pub fn audit(
auditor: Auditor<'_>,
mut match_context: &mut selectors::matching::MatchingContext<
'_,
accessibility_scraper::selector::Simple,
>,
) -> Vec<Issue> {
let mut issues: Vec<Issue> = Vec::new();

for node in auditor.tree.clone().iter() {
for node in auditor.tree.iter() {
match RULES_A.get(&*node.0) {
Some(rules) => {
for rule in rules {
match (rule.validate)(&node.1, &mut auditor) {
match (rule.validate)(&node.1, &auditor, &mut match_context) {
RuleValidation::Single(validation) => {
push_issue(validation, rule, &node.0, &auditor.locale, &mut issues)
}
Expand Down
7 changes: 5 additions & 2 deletions accessibility-rs/src/engine/rules/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ impl From<Vec<Validation>> for RuleValidation {
}
}

type ValidateFn =
fn(&Vec<(ElementRef<'_>, Option<taffy::NodeId>)>, &mut crate::Auditor<'_>) -> RuleValidation;
type ValidateFn = fn(
&Vec<(ElementRef<'_>, Option<taffy::NodeId>)>,
&crate::Auditor<'_>,
&mut selectors::matching::MatchingContext<'_, accessibility_scraper::selector::Simple>,
) -> RuleValidation;

/// the rule validation method that should be performed.
#[derive(Debug)]
Expand Down
74 changes: 37 additions & 37 deletions accessibility-rs/src/engine/rules/wcag_rule_map.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions accessibility-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ impl<'a> AuditConfig<'a> {
pub fn audit(config: AuditConfig) -> Vec<Issue> {
let document = accessibility_scraper::Html::parse_document(config.html);
let mut nth_index_cache = selectors::NthIndexCache::from(Default::default());
let mut auditor = Auditor::new(
let (auditor, mut match_context) = Auditor::new(
&document,
&config.css,
engine::styles::css_cache::build_matching_context(&mut nth_index_cache),
config.bounding_box,
config.locale,
);
engine::audit::wcag::WCAGAAA::audit(&mut auditor)
engine::audit::wcag::WCAGAAA::audit(auditor, &mut match_context)
}

0 comments on commit 96ceb1f

Please sign in to comment.