mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Support basic immutable CSSOM
This commit is contained in:
parent
2220fcdc0b
commit
177d6fa4ee
33 changed files with 861 additions and 43 deletions
67
components/script/dom/cssrulelist.rs
Normal file
67
components/script/dom/cssrulelist.rs
Normal file
|
@ -0,0 +1,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.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[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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue