use std::cell::Cell;
use dom_struct::dom_struct;
use style::shared_lock::SharedRwLock;
use style::stylesheets::{CssRule as StyleCssRule, CssRuleType};
use crate::dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::Reflector;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::cssfontfacerule::CSSFontFaceRule;
use crate::dom::cssimportrule::CSSImportRule;
use crate::dom::csskeyframerule::CSSKeyframeRule;
use crate::dom::csskeyframesrule::CSSKeyframesRule;
use crate::dom::csslayerblockrule::CSSLayerBlockRule;
use crate::dom::csslayerstatementrule::CSSLayerStatementRule;
use crate::dom::cssmediarule::CSSMediaRule;
use crate::dom::cssnamespacerule::CSSNamespaceRule;
use crate::dom::cssstylerule::CSSStyleRule;
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::csssupportsrule::CSSSupportsRule;
use crate::dom::window::Window;
#[dom_struct]
pub struct CSSRule {
reflector_: Reflector,
parent_stylesheet: Dom<CSSStyleSheet>,
parent_stylesheet_removed: Cell<bool>,
}
impl CSSRule {
#[allow(crown::unrooted_must_root)]
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet) -> CSSRule {
CSSRule {
reflector_: Reflector::new(),
parent_stylesheet: Dom::from_ref(parent_stylesheet),
parent_stylesheet_removed: Cell::new(false),
}
}
pub fn as_specific(&self) -> &dyn SpecificCSSRule {
if let Some(rule) = self.downcast::<CSSStyleRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSFontFaceRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSKeyframesRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSMediaRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSNamespaceRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSKeyframeRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSImportRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSLayerBlockRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSLayerStatementRule>() {
rule as &dyn SpecificCSSRule
} else {
unreachable!()
}
}
pub fn new_specific(
window: &Window,
parent_stylesheet: &CSSStyleSheet,
rule: StyleCssRule,
) -> DomRoot<CSSRule> {
match rule {
StyleCssRule::Import(s) => {
DomRoot::upcast(CSSImportRule::new(window, parent_stylesheet, s))
},
StyleCssRule::Style(s) => {
DomRoot::upcast(CSSStyleRule::new(window, parent_stylesheet, s))
},
StyleCssRule::FontFace(s) => {
DomRoot::upcast(CSSFontFaceRule::new(window, parent_stylesheet, s))
},
StyleCssRule::FontFeatureValues(_) => unimplemented!(),
StyleCssRule::CounterStyle(_) => unimplemented!(),
StyleCssRule::Keyframes(s) => {
DomRoot::upcast(CSSKeyframesRule::new(window, parent_stylesheet, s))
},
StyleCssRule::Media(s) => {
DomRoot::upcast(CSSMediaRule::new(window, parent_stylesheet, s))
},
StyleCssRule::Namespace(s) => {
DomRoot::upcast(CSSNamespaceRule::new(window, parent_stylesheet, s))
},
StyleCssRule::Supports(s) => {
DomRoot::upcast(CSSSupportsRule::new(window, parent_stylesheet, s))
},
StyleCssRule::Page(_) => unreachable!(),
StyleCssRule::Container(_) => unimplemented!(), StyleCssRule::Document(_) => unimplemented!(), StyleCssRule::LayerBlock(s) => {
DomRoot::upcast(CSSLayerBlockRule::new(window, parent_stylesheet, s))
},
StyleCssRule::LayerStatement(s) => {
DomRoot::upcast(CSSLayerStatementRule::new(window, parent_stylesheet, s))
},
StyleCssRule::FontPaletteValues(_) => unimplemented!(), StyleCssRule::Property(_) => unimplemented!(), StyleCssRule::Margin(_) => unimplemented!(), StyleCssRule::Scope(_) => unimplemented!(), StyleCssRule::StartingStyle(_) => unimplemented!(), StyleCssRule::PositionTry(_) => unimplemented!(), StyleCssRule::NestedDeclarations(_) => unimplemented!(), }
}
pub fn detach(&self) {
self.deparent();
}
pub fn deparent(&self) {
self.parent_stylesheet_removed.set(true);
self.as_specific().deparent_children();
}
pub fn parent_stylesheet(&self) -> &CSSStyleSheet {
&self.parent_stylesheet
}
pub fn shared_lock(&self) -> &SharedRwLock {
&self.parent_stylesheet.style_stylesheet().shared_lock
}
}
impl CSSRuleMethods<crate::DomTypeHolder> for CSSRule {
fn Type(&self) -> u16 {
let rule_type = self.as_specific().ty() as u16;
if rule_type > 15 {
0
} else {
rule_type
}
}
fn GetParentStyleSheet(&self) -> Option<DomRoot<CSSStyleSheet>> {
if self.parent_stylesheet_removed.get() {
None
} else {
Some(DomRoot::from_ref(&*self.parent_stylesheet))
}
}
fn CssText(&self) -> DOMString {
self.as_specific().get_css()
}
fn SetCssText(&self, _: DOMString) {
}
}
pub trait SpecificCSSRule {
fn ty(&self) -> CssRuleType;
fn get_css(&self) -> DOMString;
fn deparent_children(&self) {
}
}