use crate::dom::Dom;
use crate::layout::{LayoutEngine, Viewport};
use std::collections::HashMap;
use std::sync::Arc;
pub struct DomState {
pub dom: Dom,
pub layout_engine: LayoutEngine,
pub base_url: Option<url::Url>,
pub console_output: Vec<ConsoleMessage>,
pub storage: HashMap<String, HashMap<String, String>>,
pub stylesheets: Vec<String>,
pub cached_rules: Vec<CachedRule>,
pub stealth_profile: Option<crate::stealth::StealthProfile>,
pub csp_policy: Option<Arc<crate::net::csp::PolicySet>>,
pub csp_origin: Option<url::Url>,
pub resource_timings: Vec<crate::net::TimingStats>,
}
#[derive(Debug, Clone)]
pub struct CachedRule {
pub selector_str: String,
pub selectors: crate::css_selectors::SelectorList,
pub declarations: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub struct ConsoleMessage {
pub level: ConsoleLevel,
pub args: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConsoleLevel {
Log,
Warn,
Error,
Info,
Debug,
}
impl DomState {
pub fn new(dom: Dom) -> Self {
let mut storage = HashMap::new();
storage.insert("local".to_string(), HashMap::new());
storage.insert("session".to_string(), HashMap::new());
Self {
dom,
layout_engine: LayoutEngine::new(Viewport::new(1920.0, 1080.0)),
base_url: None,
console_output: Vec::new(),
storage,
stylesheets: Vec::new(),
cached_rules: Vec::new(),
stealth_profile: None,
csp_policy: None,
csp_origin: None,
resource_timings: Vec::new(),
}
}
pub fn update_cached_rules(&mut self) {
use crate::js_runtime::utils::tokens_to_string;
self.cached_rules.clear();
for css_text in &self.stylesheets {
let (stylesheet, _errors) = crate::css_parser::parse_stylesheet(css_text);
for rule in &stylesheet.rules {
if let crate::css_parser::ast::Rule::Qualified(qr) = rule {
let selector_str = tokens_to_string(&qr.prelude);
if selector_str.is_empty() {
continue;
}
let mut declarations = HashMap::new();
for d in &qr.declarations {
declarations.insert(
d.name.to_string(),
tokens_to_string(&d.value).trim().to_string(),
);
}
let selectors = crate::css_selectors::parse_selector_list(&selector_str)
.unwrap_or_default();
self.cached_rules.push(CachedRule {
selector_str,
selectors,
declarations,
});
}
}
}
}
pub fn with_base_url(mut self, url: url::Url) -> Self {
self.base_url = Some(url);
self
}
}