script/dom/
cssfontfacerule.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 servo_arc::Arc;
9use style::shared_lock::{Locked, ToCssWithGuard};
10use style::stylesheets::{CssRuleType, FontFaceRule};
11
12use crate::dom::bindings::reflector::reflect_dom_object;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::cssrule::{CSSRule, SpecificCSSRule};
16use crate::dom::cssstylesheet::CSSStyleSheet;
17use crate::dom::window::Window;
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub(crate) struct CSSFontFaceRule {
22    cssrule: CSSRule,
23    #[ignore_malloc_size_of = "Arc"]
24    #[no_trace]
25    fontfacerule: RefCell<Arc<Locked<FontFaceRule>>>,
26}
27
28impl CSSFontFaceRule {
29    fn new_inherited(
30        parent_stylesheet: &CSSStyleSheet,
31        fontfacerule: Arc<Locked<FontFaceRule>>,
32    ) -> CSSFontFaceRule {
33        CSSFontFaceRule {
34            cssrule: CSSRule::new_inherited(parent_stylesheet),
35            fontfacerule: RefCell::new(fontfacerule),
36        }
37    }
38
39    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    pub(crate) fn new(
41        window: &Window,
42        parent_stylesheet: &CSSStyleSheet,
43        fontfacerule: Arc<Locked<FontFaceRule>>,
44        can_gc: CanGc,
45    ) -> DomRoot<CSSFontFaceRule> {
46        reflect_dom_object(
47            Box::new(CSSFontFaceRule::new_inherited(
48                parent_stylesheet,
49                fontfacerule,
50            )),
51            window,
52            can_gc,
53        )
54    }
55
56    pub(crate) fn update_rule(&self, fontfacerule: Arc<Locked<FontFaceRule>>) {
57        *self.fontfacerule.borrow_mut() = fontfacerule;
58    }
59}
60
61impl SpecificCSSRule for CSSFontFaceRule {
62    fn ty(&self) -> CssRuleType {
63        CssRuleType::FontFace
64    }
65
66    fn get_css(&self) -> DOMString {
67        let guard = self.cssrule.shared_lock().read();
68        self.fontfacerule
69            .borrow()
70            .read_with(&guard)
71            .to_css_string(&guard)
72            .into()
73    }
74}