use std::collections::HashMap;
use crate::parser::dom::ElementNode;
use crate::types::Color;
#[derive(Debug, Clone, Copy)]
pub struct MediaContext {
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone)]
pub struct AncestorInfo<'a> {
pub element: &'a ElementNode,
pub child_index: usize,
pub sibling_count: usize,
pub preceding_siblings: Vec<(String, Vec<String>)>,
}
#[derive(Debug, Clone, Default)]
pub struct SelectorContext<'a> {
pub ancestors: Vec<AncestorInfo<'a>>,
pub child_index: usize,
pub sibling_count: usize,
pub preceding_siblings: Vec<(String, Vec<String>)>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CalcOp {
Add,
Sub,
Mul,
Div,
}
#[derive(Debug, Clone)]
pub enum CalcToken {
Length(f32),
Percent(f32),
Em(f32),
Rem(f32),
Vw(f32),
Vh(f32),
Op(CalcOp),
}
#[derive(Debug, Clone)]
pub enum CssValue {
Length(f32),
Color(Color),
Keyword(String),
Number(f32),
Percentage(f32),
Rem(f32),
Vw(f32),
Vh(f32),
Calc(Vec<CalcToken>),
Var(String, Option<String>),
}
#[derive(Debug, Clone, Default)]
pub struct StyleMap {
pub properties: HashMap<String, CssValue>,
pub important: HashMap<String, bool>,
}
impl StyleMap {
pub fn new() -> Self {
Self::default()
}
pub fn set(&mut self, key: &str, value: CssValue) {
self.set_with_importance(key, value, false);
}
pub fn set_with_importance(&mut self, key: &str, value: CssValue, is_important: bool) {
if self.is_important(key) && !is_important {
return;
}
self.properties.insert(key.to_string(), value);
self.important.insert(key.to_string(), is_important);
}
pub fn get(&self, key: &str) -> Option<&CssValue> {
self.properties.get(key)
}
pub fn remove(&mut self, key: &str) {
self.properties.remove(key);
self.important.remove(key);
}
pub fn is_important(&self, key: &str) -> bool {
self.important.get(key).copied().unwrap_or(false)
}
#[allow(dead_code)]
pub fn merge(&mut self, other: &StyleMap) {
for (key, value) in &other.properties {
self.set_with_importance(key, value.clone(), other.is_important(key));
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PseudoElement {
Before,
After,
}
#[derive(Debug, Clone)]
pub struct CssRule {
pub selector: String,
pub declarations: StyleMap,
pub pseudo_element: Option<PseudoElement>,
}
#[derive(Debug, Clone)]
pub struct FontFaceRule {
pub font_family: String,
pub src_path: String,
}
#[derive(Debug, Clone)]
pub struct ImportRule {
pub path: String,
}
#[derive(Debug, Clone, Default)]
pub struct PageRule {
pub width: Option<f32>,
pub height: Option<f32>,
pub margin_top: Option<f32>,
pub margin_right: Option<f32>,
pub margin_bottom: Option<f32>,
pub margin_left: Option<f32>,
}