diff options
Diffstat (limited to 'components/script/dom/cssrulelist.rs')
-rw-r--r-- | components/script/dom/cssrulelist.rs | 144 |
1 files changed, 84 insertions, 60 deletions
diff --git a/components/script/dom/cssrulelist.rs b/components/script/dom/cssrulelist.rs index 67eac2beda2..d4fde8ae308 100644 --- a/components/script/dom/cssrulelist.rs +++ b/components/script/dom/cssrulelist.rs @@ -1,21 +1,25 @@ /* 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::cell::DOMRefCell; -use dom::bindings::codegen::Bindings::CSSRuleListBinding; -use dom::bindings::codegen::Bindings::CSSRuleListBinding::CSSRuleListMethods; -use dom::bindings::error::{Error, ErrorResult, Fallible}; -use dom::bindings::js::{JS, MutNullableJS, Root}; -use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::csskeyframerule::CSSKeyframeRule; -use dom::cssrule::CSSRule; -use dom::cssstylesheet::CSSStyleSheet; -use dom::window::Window; + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::CSSRuleListBinding::CSSRuleListMethods; +use crate::dom::bindings::error::{Error, ErrorResult, Fallible}; +use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom}; +use crate::dom::csskeyframerule::CSSKeyframeRule; +use crate::dom::cssrule::CSSRule; +use crate::dom::cssstylesheet::CSSStyleSheet; +use crate::dom::htmlelement::HTMLElement; +use crate::dom::window::Window; +use crate::style::stylesheets::StylesheetLoader as StyleStylesheetLoader; +use crate::stylesheet_loader::StylesheetLoader; use dom_struct::dom_struct; -use std::sync::Arc; +use servo_arc::Arc; use style::shared_lock::Locked; -use style::stylesheets::{CssRules, CssRulesHelpers, KeyframesRule, RulesMutateError}; +use style::stylesheets::{ + AllowImportRules, CssRules, CssRulesHelpers, KeyframesRule, RulesMutateError, +}; #[allow(unsafe_code)] unsafe_no_jsmanaged_fields!(RulesSource); @@ -36,10 +40,10 @@ impl From<RulesMutateError> for Error { #[dom_struct] pub struct CSSRuleList { reflector_: Reflector, - parent_stylesheet: JS<CSSStyleSheet>, - #[ignore_heap_size_of = "Arc"] + parent_stylesheet: Dom<CSSStyleSheet>, + #[ignore_malloc_size_of = "Arc"] rules: RulesSource, - dom_rules: DOMRefCell<Vec<MutNullableJS<CSSRule>>> + dom_rules: DomRefCell<Vec<MutNullableDom<CSSRule>>>, } pub enum RulesSource { @@ -52,28 +56,38 @@ impl CSSRuleList { pub fn new_inherited(parent_stylesheet: &CSSStyleSheet, rules: RulesSource) -> CSSRuleList { let guard = parent_stylesheet.shared_lock().read(); let dom_rules = match rules { - RulesSource::Rules(ref rules) => { - rules.read_with(&guard).0.iter().map(|_| MutNullableJS::new(None)).collect() - } - RulesSource::Keyframes(ref rules) => { - rules.read_with(&guard).keyframes.iter().map(|_| MutNullableJS::new(None)).collect() - } + RulesSource::Rules(ref rules) => rules + .read_with(&guard) + .0 + .iter() + .map(|_| MutNullableDom::new(None)) + .collect(), + RulesSource::Keyframes(ref rules) => rules + .read_with(&guard) + .keyframes + .iter() + .map(|_| MutNullableDom::new(None)) + .collect(), }; CSSRuleList { reflector_: Reflector::new(), - parent_stylesheet: JS::from_ref(parent_stylesheet), + parent_stylesheet: Dom::from_ref(parent_stylesheet), rules: rules, - dom_rules: DOMRefCell::new(dom_rules), + dom_rules: DomRefCell::new(dom_rules), } } #[allow(unrooted_must_root)] - pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet, - rules: RulesSource) -> Root<CSSRuleList> { - reflect_dom_object(box CSSRuleList::new_inherited(parent_stylesheet, rules), - window, - CSSRuleListBinding::Wrap) + pub fn new( + window: &Window, + parent_stylesheet: &CSSStyleSheet, + rules: RulesSource, + ) -> DomRoot<CSSRuleList> { + reflect_dom_object( + Box::new(CSSRuleList::new_inherited(parent_stylesheet, rules)), + window, + ) } /// Should only be called for CssRules-backed rules. Use append_lazy_rule @@ -90,18 +104,32 @@ impl CSSRuleList { let index = idx as usize; let parent_stylesheet = self.parent_stylesheet.style_stylesheet(); - let new_rule = - css_rules.insert_rule(&parent_stylesheet.shared_lock, - rule, - parent_stylesheet, - index, - nested, - None)?; + let owner = self + .parent_stylesheet + .get_owner() + .map(DomRoot::downcast::<HTMLElement>) + .flatten(); + let loader = owner + .as_ref() + .map(|element| StylesheetLoader::for_element(&**element)); + let new_rule = css_rules.with_raw_offset_arc(|arc| { + arc.insert_rule( + &parent_stylesheet.shared_lock, + rule, + &parent_stylesheet.contents, + index, + nested, + loader.as_ref().map(|l| l as &dyn StyleStylesheetLoader), + AllowImportRules::Yes, + ) + })?; let parent_stylesheet = &*self.parent_stylesheet; let dom_rule = CSSRule::new_specific(&window, parent_stylesheet, new_rule); - self.dom_rules.borrow_mut().insert(index, MutNullableJS::new(Some(&*dom_rule))); - Ok((idx)) + self.dom_rules + .borrow_mut() + .insert(index, MutNullableDom::new(Some(&*dom_rule))); + Ok(idx) } // In case of a keyframe rule, index must be valid. @@ -116,7 +144,7 @@ impl CSSRuleList { dom_rules[index].get().map(|r| r.detach()); dom_rules.remove(index); Ok(()) - } + }, RulesSource::Keyframes(ref kf) => { // https://drafts.csswg.org/css-animations/#dom-csskeyframesrule-deleterule let mut dom_rules = self.dom_rules.borrow_mut(); @@ -124,37 +152,34 @@ impl CSSRuleList { dom_rules.remove(index); kf.write_with(&mut guard).keyframes.remove(index); Ok(()) - } + }, } } // Remove parent stylesheets from all children pub fn deparent_all(&self) { for rule in self.dom_rules.borrow().iter() { - rule.get().map(|r| Root::upcast(r).deparent()); + rule.get().map(|r| DomRoot::upcast(r).deparent()); } } - pub fn item(&self, idx: u32) -> Option<Root<CSSRule>> { + pub fn item(&self, idx: u32) -> Option<DomRoot<CSSRule>> { self.dom_rules.borrow().get(idx as usize).map(|rule| { rule.or_init(|| { let parent_stylesheet = &self.parent_stylesheet; let guard = parent_stylesheet.shared_lock().read(); match self.rules { - RulesSource::Rules(ref rules) => { - CSSRule::new_specific(self.global().as_window(), - parent_stylesheet, - rules.read_with(&guard).0[idx as usize].clone()) - } - RulesSource::Keyframes(ref rules) => { - Root::upcast(CSSKeyframeRule::new(self.global().as_window(), - parent_stylesheet, - rules.read_with(&guard) - .keyframes[idx as usize] - .clone())) - } + RulesSource::Rules(ref rules) => CSSRule::new_specific( + self.global().as_window(), + parent_stylesheet, + rules.read_with(&guard).0[idx as usize].clone(), + ), + RulesSource::Keyframes(ref rules) => DomRoot::upcast(CSSKeyframeRule::new( + self.global().as_window(), + parent_stylesheet, + rules.read_with(&guard).keyframes[idx as usize].clone(), + )), } - }) }) } @@ -168,13 +193,13 @@ impl CSSRuleList { if let RulesSource::Rules(..) = self.rules { panic!("Can only call append_lazy_rule with keyframes-backed CSSRules"); } - self.dom_rules.borrow_mut().push(MutNullableJS::new(None)); + self.dom_rules.borrow_mut().push(MutNullableDom::new(None)); } } impl CSSRuleListMethods for CSSRuleList { // https://drafts.csswg.org/cssom/#ref-for-dom-cssrulelist-item-1 - fn Item(&self, idx: u32) -> Option<Root<CSSRule>> { + fn Item(&self, idx: u32) -> Option<DomRoot<CSSRule>> { self.item(idx) } @@ -184,8 +209,7 @@ impl CSSRuleListMethods for CSSRuleList { } // check-tidy: no specs after this line - fn IndexedGetter(&self, index: u32) -> Option<Root<CSSRule>> { + fn IndexedGetter(&self, index: u32) -> Option<DomRoot<CSSRule>> { self.Item(index) } } - |