Skip to content

Commit 35ec0c7

Browse files
feat(dom): add screen (#39)
1 parent f8e782a commit 35ec0c7

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

packages/dom/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod pretty_dom;
1111
pub mod queries;
1212
pub mod query_helpers;
1313
mod role_helpers;
14+
mod screen;
1415
mod suggestions;
1516
mod types;
1617
mod util;
@@ -30,6 +31,7 @@ pub use role_helpers::{
3031
GetRolesOptions, PrettyRolesOptions, get_implicit_aria_roles, get_roles, is_inaccessible,
3132
log_roles,
3233
};
34+
pub use screen::*;
3335
pub use suggestions::*;
3436
pub use types::*;
3537
pub use wait_for::*;

packages/dom/src/pretty_dom.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ pub fn pretty_dom(dom: Option<DocumentOrElement>, max_length: Option<usize>) ->
8686
debug_content
8787
}
8888
}
89+
90+
pub fn log_dom(dom: Option<DocumentOrElement>, max_length: Option<usize>) {
91+
// TODO: User code frame.
92+
log::info!("{}", pretty_dom(dom, max_length));
93+
}

packages/dom/src/screen.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::ops::Deref;
2+
3+
use web_sys::window;
4+
5+
use crate::{BoundFunctions, DocumentOrElement, get_queries_for_element, log_dom};
6+
7+
pub struct Screen(BoundFunctions);
8+
9+
impl Screen {
10+
pub fn debug(&self, elements: Option<Vec<DocumentOrElement>>, max_length: Option<usize>) {
11+
if let Some(elements) = elements {
12+
for element in elements {
13+
log_dom(Some(element), max_length);
14+
}
15+
} else {
16+
log_dom(None, max_length);
17+
}
18+
}
19+
}
20+
21+
impl Deref for Screen {
22+
type Target = BoundFunctions;
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.0
26+
}
27+
}
28+
29+
pub fn screen() -> Screen {
30+
let body = window()
31+
.and_then(|window| window.document())
32+
.and_then(|document| document.body())
33+
.expect("For queries bound to document.body a global document has to be available.");
34+
Screen(get_queries_for_element(body))
35+
}

0 commit comments

Comments
 (0)