Skip to content

Commit

Permalink
chore(innate): add accessibility tree building
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Sep 29, 2023
1 parent d3aa4a3 commit afd16e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
49 changes: 25 additions & 24 deletions kayle_innate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate lazy_static;
mod utils;
use case_insensitive_string::CaseInsensitiveString;
use std::collections::HashSet;
use utils::{convert_abs_path, convert_base_path, set_panic_hook, domain_name};
use utils::{convert_abs_path, convert_base_path, domain_name, set_panic_hook};
use wasm_bindgen::prelude::*;

#[cfg(feature = "wee_alloc")]
Expand Down Expand Up @@ -130,6 +130,7 @@ pub fn get_document_links(res: &str, domain: &str) -> Box<[JsValue]> {
/// try to fix all possible issues using a spec against the tree.
pub fn parse_accessibility_tree(html: &str) {
set_panic_hook();
use std::collections::BTreeMap;

#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -158,50 +159,50 @@ pub fn parse_accessibility_tree(html: &str) {
// The chrome browser we can set to ignore all assets and fetch them here but, it would be re-doing the wheel.
// If we can send the Stylesheets from node to rust this could leverage the sheets attached since we just need the node references.

let mut n = 0;
let t = now();
let mut n = 0;
let mut accessibility_tree: BTreeMap<Option<&str>, Vec<_>> = BTreeMap::new();
let d = select::document::Document::from(html);

// measure select parsing doc 1:1 around 34ms - gets slower when using methods possibly due to clones
while let Some(node) = select::document::Document::from(html).nth(n) {
while let Some(node) = d.nth(n) {
let element_name = node.name();
console_log!("{:?}", element_name);
// console_log!("{:?}", element_name);
accessibility_tree
.entry(element_name)
.and_modify(|n| n.push(node))
.or_insert(Vec::from([node]));
n += 1;
}

console_log!("Select Parser duration {:?}ms", now() - t);
// console_log!("Tree {:?}", accessibility_tree);

let t = now();

// parse doc will start from html downwards
let h = scraper::Html::parse_document(html);
let mut hh = h.tree.nodes();
// accessibility tree for ordered element mappings
let mut accessibility_tree: BTreeMap<&str, Vec<_>> = BTreeMap::new();

// measure select parsing doc 1:1 around 10ms
while let Some(node) = hh.next() {
if let Some(element) = node.value().as_element() {
let element_name = element.name();
console_log!("{:?}", element_name);
// console_log!("{:?}", element_name);
accessibility_tree
.entry(element_name)
.and_modify(|n| n.push(element))
.or_insert(Vec::from([element]));
}
}
// "html"
// "head"
// "title"
// "meta"
// "link"
// "style"
// "body"
// "header"
// "nav"
// "a"
// "a"
// "main"
// "h1"
// "p"
// "input"
// "footer"
// "ul"
// "li"

console_log!("Scraper Parser: duration {:?}ms", now() - t);
console_log!(
"Getting tree links {:?}",
accessibility_tree.get(&"a")
);
// console_log!("Tree {:?}", accessibility_tree);
}

#[wasm_bindgen]
Expand Down
26 changes: 17 additions & 9 deletions kayle_innate/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn _get_document_links() {
<html>
<body>
<main>
<a href="/";>Home</a>
<a href="/about";>About</a>
<a href="/">Home</a>
<a href="/about">About</a>
</main>
</body>
</html>
Expand All @@ -38,10 +38,10 @@ fn _get_document_links() {
<html>
<body>
<main>
<a href="/";>Home</a>
<a href="/about";>About</a>
<a href="/magic/";>Magic</a>
<a href="https://www.meta.com";>Meta</a>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/magic/">Magic</a>
<a href="https://www.meta.com">Meta</a>
</main>
</body>
</html>
Expand Down Expand Up @@ -85,18 +85,26 @@ fn _parse_accessibility_tree() {
<body>
<header>
<nav>
<a href="/";>Home</a>
<a href="/about";>About</a>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main>
<h1>Some nice content</h1>
<p>Content ipsum</p>
<input type="text" placeholder="Phone number"></input>
<input type="text" placeholder="Phone number">
<h2>Main header</h2>
<p>Content ipsum</p>
<h2>Second Main Header</h2>
<article>
<h3>Articles about Access</h3>
<p>Learning accessibility with content ipsum</p>
</article>
</main>
<footer>
<ul style="background: green;">
<li>Access</li>
<a href="/">Company</a>
</ul>
</footer>
</body>
Expand Down

0 comments on commit afd16e3

Please sign in to comment.