aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/cssrulelist.rs
blob: 1c383403a0055dc2ce7a964f9daaf0fae351a088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::CSSRuleListBinding;
use dom::bindings::codegen::Bindings::CSSRuleListBinding::CSSRuleListMethods;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::cssrule::CSSRule;
use dom::cssstylesheet::CSSStyleSheet;
use dom::window::Window;
use style::stylesheets::CssRules;

no_jsmanaged_fields!(CssRules);

#[dom_struct]
pub struct CSSRuleList {
    reflector_: Reflector,
    sheet: JS<CSSStyleSheet>,
    #[ignore_heap_size_of = "Arc"]
    rules: CssRules,
    dom_rules: Vec<MutNullableHeap<JS<CSSRule>>>
}

impl CSSRuleList {
    #[allow(unrooted_must_root)]
    pub fn new_inherited(sheet: &CSSStyleSheet, rules: CssRules) -> CSSRuleList {
        let dom_rules = rules.0.read().iter().map(|_| MutNullableHeap::new(None)).collect();
        CSSRuleList {
            reflector_: Reflector::new(),
            sheet: JS::from_ref(sheet),
            rules: rules,
            dom_rules: dom_rules,
        }
    }

    #[allow(unrooted_must_root)]
    pub fn new(window: &Window, sheet: &CSSStyleSheet, rules: CssRules) -> Root<CSSRuleList> {
        reflect_dom_object(box CSSRuleList::new_inherited(sheet, rules),
                           window,
                           CSSRuleListBinding::Wrap)
    }
}

impl CSSRuleListMethods for CSSRuleList {
    // https://drafts.csswg.org/cssom/#ref-for-dom-cssrulelist-item-1
    fn Item(&self, idx: u32) -> Option<Root<CSSRule>> {
        self.dom_rules.get(idx as usize).map(|rule| {
            rule.or_init(|| {
                CSSRule::new_specific(self.global().as_window(),
                                     &self.sheet,
                                     self.rules.0.read()[idx as usize].clone())
            })
        })
    }

    // https://drafts.csswg.org/cssom/#dom-cssrulelist-length
    fn Length(&self) -> u32 {
        self.dom_rules.len() as u32
    }

    // check-tidy: no specs after this line
    fn IndexedGetter(&self, index: u32) -> Option<Root<CSSRule>> {
        self.Item(index)
    }
}