script/dom/css/
csskeyframerule.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::RefCell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use servo_arc::Arc;
10use style::shared_lock::{Locked, SharedRwLockReadGuard, ToCssWithGuard};
11use style::stylesheets::CssRuleType;
12use style::stylesheets::keyframes_rule::Keyframe;
13
14use super::cssrule::{CSSRule, SpecificCSSRule};
15use super::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
16use super::cssstylesheet::CSSStyleSheet;
17use crate::dom::bindings::codegen::Bindings::CSSKeyframeRuleBinding::CSSKeyframeRuleMethods;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
20use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
21use crate::dom::bindings::str::DOMString;
22use crate::dom::window::Window;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct CSSKeyframeRule {
27    css_rule: CSSRule,
28    #[ignore_malloc_size_of = "Stylo"]
29    #[no_trace]
30    keyframe_rule: RefCell<Arc<Locked<Keyframe>>>,
31    style_declaration: MutNullableDom<CSSStyleDeclaration>,
32}
33
34impl CSSKeyframeRule {
35    fn new_inherited(
36        parent_stylesheet: &CSSStyleSheet,
37        keyframerule: Arc<Locked<Keyframe>>,
38    ) -> CSSKeyframeRule {
39        CSSKeyframeRule {
40            css_rule: CSSRule::new_inherited(parent_stylesheet),
41            keyframe_rule: RefCell::new(keyframerule),
42            style_declaration: Default::default(),
43        }
44    }
45
46    pub(crate) fn new(
47        window: &Window,
48        parent_stylesheet: &CSSStyleSheet,
49        keyframerule: Arc<Locked<Keyframe>>,
50        can_gc: CanGc,
51    ) -> DomRoot<CSSKeyframeRule> {
52        reflect_dom_object(
53            Box::new(CSSKeyframeRule::new_inherited(
54                parent_stylesheet,
55                keyframerule,
56            )),
57            window,
58            can_gc,
59        )
60    }
61
62    pub(crate) fn update_rule(
63        &self,
64        keyframerule: Arc<Locked<Keyframe>>,
65        guard: &SharedRwLockReadGuard,
66    ) {
67        if let Some(ref style_decl) = self.style_declaration.get() {
68            style_decl.update_property_declaration_block(&keyframerule.read_with(guard).block);
69        }
70        *self.keyframe_rule.borrow_mut() = keyframerule;
71    }
72}
73
74impl CSSKeyframeRuleMethods<crate::DomTypeHolder> for CSSKeyframeRule {
75    /// <https://drafts.csswg.org/css-animations/#dom-csskeyframerule-style>
76    fn Style(&self, cx: &mut JSContext) -> DomRoot<CSSStyleDeclaration> {
77        self.style_declaration.or_init(|| {
78            let guard = self.css_rule.shared_lock().read();
79            CSSStyleDeclaration::new(
80                self.global().as_window(),
81                CSSStyleOwner::CSSRule(
82                    Dom::from_ref(self.upcast()),
83                    RefCell::new(self.keyframe_rule.borrow().read_with(&guard).block.clone()),
84                ),
85                None,
86                CSSModificationAccess::ReadWrite,
87                CanGc::from_cx(cx),
88            )
89        })
90    }
91}
92
93impl SpecificCSSRule for CSSKeyframeRule {
94    fn ty(&self) -> CssRuleType {
95        CssRuleType::Keyframe
96    }
97
98    fn get_css(&self) -> DOMString {
99        let guard = self.css_rule.shared_lock().read();
100        self.keyframe_rule
101            .borrow()
102            .read_with(&guard)
103            .to_css_string(&guard)
104            .into()
105    }
106}