Skip to content

Commit

Permalink
Make parent stylesheet optional for CSSRules
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 23, 2016
1 parent 52a3a71 commit 53c9966
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 32 deletions.
4 changes: 2 additions & 2 deletions components/script/dom/cssfontfacerule.rs
Expand Up @@ -22,15 +22,15 @@ pub struct CSSFontFaceRule {
}

impl CSSFontFaceRule {
fn new_inherited(parent: &CSSStyleSheet, fontfacerule: Arc<RwLock<FontFaceRule>>) -> CSSFontFaceRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, fontfacerule: Arc<RwLock<FontFaceRule>>) -> CSSFontFaceRule {
CSSFontFaceRule {
cssrule: CSSRule::new_inherited(parent),
fontfacerule: fontfacerule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
fontfacerule: Arc<RwLock<FontFaceRule>>) -> Root<CSSFontFaceRule> {
reflect_dom_object(box CSSFontFaceRule::new_inherited(parent, fontfacerule),
window,
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/cssgroupingrule.rs
Expand Up @@ -15,14 +15,14 @@ pub struct CSSGroupingRule {
}

impl CSSGroupingRule {
pub fn new_inherited(parent: &CSSStyleSheet) -> CSSGroupingRule {
pub fn new_inherited(parent: Option<&CSSStyleSheet>) -> CSSGroupingRule {
CSSGroupingRule {
cssrule: CSSRule::new_inherited(parent),
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet) -> Root<CSSGroupingRule> {
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>) -> Root<CSSGroupingRule> {
reflect_dom_object(box CSSGroupingRule::new_inherited(parent),
window,
CSSGroupingRuleBinding::Wrap)
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/csskeyframerule.rs
Expand Up @@ -21,15 +21,15 @@ pub struct CSSKeyframeRule {
}

impl CSSKeyframeRule {
fn new_inherited(parent: &CSSStyleSheet, keyframerule: Arc<RwLock<Keyframe>>) -> CSSKeyframeRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, keyframerule: Arc<RwLock<Keyframe>>) -> CSSKeyframeRule {
CSSKeyframeRule {
cssrule: CSSRule::new_inherited(parent),
keyframerule: keyframerule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
keyframerule: Arc<RwLock<Keyframe>>) -> Root<CSSKeyframeRule> {
reflect_dom_object(box CSSKeyframeRule::new_inherited(parent, keyframerule),
window,
Expand Down
15 changes: 9 additions & 6 deletions components/script/dom/csskeyframesrule.rs
Expand Up @@ -27,7 +27,7 @@ pub struct CSSKeyframesRule {
}

impl CSSKeyframesRule {
fn new_inherited(parent: &CSSStyleSheet, keyframesrule: Arc<RwLock<KeyframesRule>>) -> CSSKeyframesRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, keyframesrule: Arc<RwLock<KeyframesRule>>) -> CSSKeyframesRule {
CSSKeyframesRule {
cssrule: CSSRule::new_inherited(parent),
keyframesrule: keyframesrule,
Expand All @@ -36,18 +36,21 @@ impl CSSKeyframesRule {
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
keyframesrule: Arc<RwLock<KeyframesRule>>) -> Root<CSSKeyframesRule> {
reflect_dom_object(box CSSKeyframesRule::new_inherited(parent, keyframesrule),
window,
CSSKeyframesRuleBinding::Wrap)
}

fn rulelist(&self) -> Root<CSSRuleList> {
self.rulelist.or_init(|| CSSRuleList::new(self.global().as_window(),
// temporary unwrap
&self.upcast::<CSSRule>().GetParentStyleSheet().unwrap(),
RulesSource::Keyframes(self.keyframesrule.clone())))
self.rulelist.or_init(|| {
let sheet = self.upcast::<CSSRule>().GetParentStyleSheet();
let sheet = sheet.as_ref().map(|s| &**s);
CSSRuleList::new(self.global().as_window(),
sheet,
RulesSource::Keyframes(self.keyframesrule.clone()))
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/cssmediarule.rs
Expand Up @@ -23,15 +23,15 @@ pub struct CSSMediaRule {
}

impl CSSMediaRule {
fn new_inherited(parent: &CSSStyleSheet, mediarule: Arc<RwLock<MediaRule>>) -> CSSMediaRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, mediarule: Arc<RwLock<MediaRule>>) -> CSSMediaRule {
CSSMediaRule {
cssrule: CSSGroupingRule::new_inherited(parent),
mediarule: mediarule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
mediarule: Arc<RwLock<MediaRule>>) -> Root<CSSMediaRule> {
reflect_dom_object(box CSSMediaRule::new_inherited(parent, mediarule),
window,
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/cssnamespacerule.rs
Expand Up @@ -22,15 +22,15 @@ pub struct CSSNamespaceRule {
}

impl CSSNamespaceRule {
fn new_inherited(parent: &CSSStyleSheet, namespacerule: Arc<RwLock<NamespaceRule>>) -> CSSNamespaceRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, namespacerule: Arc<RwLock<NamespaceRule>>) -> CSSNamespaceRule {
CSSNamespaceRule {
cssrule: CSSRule::new_inherited(parent),
namespacerule: namespacerule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
namespacerule: Arc<RwLock<NamespaceRule>>) -> Root<CSSNamespaceRule> {
reflect_dom_object(box CSSNamespaceRule::new_inherited(parent, namespacerule),
window,
Expand Down
9 changes: 5 additions & 4 deletions components/script/dom/cssrule.rs
Expand Up @@ -28,15 +28,15 @@ pub struct CSSRule {

impl CSSRule {
#[allow(unrooted_must_root)]
pub fn new_inherited(parent: &CSSStyleSheet) -> CSSRule {
pub fn new_inherited(parent: Option<&CSSStyleSheet>) -> CSSRule {
CSSRule {
reflector_: Reflector::new(),
parent: MutNullableHeap::new(Some(parent)),
parent: MutNullableHeap::new(parent),
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet) -> Root<CSSRule> {
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>) -> Root<CSSRule> {
reflect_dom_object(box CSSRule::new_inherited(parent),
window,
CSSRuleBinding::Wrap)
Expand Down Expand Up @@ -64,7 +64,7 @@ impl CSSRule {

// Given a StyleCssRule, create a new instance of a derived class of
// CSSRule based on which rule it is
pub fn new_specific(window: &Window, parent: &CSSStyleSheet,
pub fn new_specific(window: &Window, parent: Option<&CSSStyleSheet>,
rule: StyleCssRule) -> Root<CSSRule> {
// be sure to update the match in as_specific when this is updated
match rule {
Expand All @@ -81,6 +81,7 @@ impl CSSRule {
pub fn disown(&self) {
self.parent.set(None);
// should set parent rule to None when we add parent rule support
// Should we disown children as well? (https://github.com/w3c/csswg-drafts/issues/722)
}
}

Expand Down
19 changes: 12 additions & 7 deletions components/script/dom/cssrulelist.rs
Expand Up @@ -24,7 +24,7 @@ no_jsmanaged_fields!(RulesSource);
#[dom_struct]
pub struct CSSRuleList {
reflector_: Reflector,
sheet: JS<CSSStyleSheet>,
sheet: MutNullableHeap<JS<CSSStyleSheet>>,
#[ignore_heap_size_of = "Arc"]
rules: RulesSource,
dom_rules: DOMRefCell<Vec<MutNullableHeap<JS<CSSRule>>>>
Expand All @@ -37,7 +37,7 @@ pub enum RulesSource {

impl CSSRuleList {
#[allow(unrooted_must_root)]
pub fn new_inherited(sheet: &CSSStyleSheet, rules: RulesSource) -> CSSRuleList {
pub fn new_inherited(sheet: Option<&CSSStyleSheet>, rules: RulesSource) -> CSSRuleList {
let dom_rules = match rules {
RulesSource::Rules(ref rules) => {
rules.0.read().iter().map(|_| MutNullableHeap::new(None)).collect()
Expand All @@ -49,14 +49,15 @@ impl CSSRuleList {

CSSRuleList {
reflector_: Reflector::new(),
sheet: JS::from_ref(sheet),
sheet: MutNullableHeap::new(sheet),
rules: rules,
dom_rules: DOMRefCell::new(dom_rules),
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, sheet: &CSSStyleSheet, rules: RulesSource) -> Root<CSSRuleList> {
pub fn new(window: &Window, sheet: Option<&CSSStyleSheet>,
rules: RulesSource) -> Root<CSSRuleList> {
reflect_dom_object(box CSSRuleList::new_inherited(sheet, rules),
window,
CSSRuleListBinding::Wrap)
Expand Down Expand Up @@ -132,7 +133,9 @@ impl CSSRuleList {
};

insert(&mut css_rules.0.write(), index, new_rule.clone());
let dom_rule = CSSRule::new_specific(&window, &self.sheet, new_rule);
let sheet = self.sheet.get();
let sheet = sheet.as_ref().map(|sheet| &**sheet);
let dom_rule = CSSRule::new_specific(&window, sheet, new_rule);
insert(&mut self.dom_rules.borrow_mut(),
index, MutNullableHeap::new(Some(&*dom_rule)));
Ok((idx))
Expand Down Expand Up @@ -174,15 +177,17 @@ impl CSSRuleListMethods for CSSRuleList {
fn Item(&self, idx: u32) -> Option<Root<CSSRule>> {
self.dom_rules.borrow().get(idx as usize).map(|rule| {
rule.or_init(|| {
let sheet = self.sheet.get();
let sheet = sheet.as_ref().map(|sheet| &**sheet);
match self.rules {
RulesSource::Rules(ref rules) => {
CSSRule::new_specific(self.global().as_window(),
&self.sheet,
sheet,
rules.0.read()[idx as usize].clone())
}
RulesSource::Keyframes(ref rules) => {
Root::upcast(CSSKeyframeRule::new(self.global().as_window(),
&self.sheet,
sheet,
rules.read()
.keyframes[idx as usize]
.clone()))
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/cssstylerule.rs
Expand Up @@ -22,15 +22,15 @@ pub struct CSSStyleRule {
}

impl CSSStyleRule {
fn new_inherited(parent: &CSSStyleSheet, stylerule: Arc<RwLock<StyleRule>>) -> CSSStyleRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, stylerule: Arc<RwLock<StyleRule>>) -> CSSStyleRule {
CSSStyleRule {
cssrule: CSSRule::new_inherited(parent),
stylerule: stylerule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
stylerule: Arc<RwLock<StyleRule>>) -> Root<CSSStyleRule> {
reflect_dom_object(box CSSStyleRule::new_inherited(parent, stylerule),
window,
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/cssstylesheet.rs
Expand Up @@ -44,7 +44,7 @@ impl CSSStyleSheet {

fn rulelist(&self) -> Root<CSSRuleList> {
self.rulelist.or_init(|| CSSRuleList::new(self.global().as_window(),
self,
Some(self),
RulesSource::Rules(self.style_stylesheet
.rules.clone())))
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/cssviewportrule.rs
Expand Up @@ -22,15 +22,15 @@ pub struct CSSViewportRule {
}

impl CSSViewportRule {
fn new_inherited(parent: &CSSStyleSheet, viewportrule: Arc<RwLock<ViewportRule>>) -> CSSViewportRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, viewportrule: Arc<RwLock<ViewportRule>>) -> CSSViewportRule {
CSSViewportRule {
cssrule: CSSRule::new_inherited(parent),
viewportrule: viewportrule,
}
}

#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: &CSSStyleSheet,
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
viewportrule: Arc<RwLock<ViewportRule>>) -> Root<CSSViewportRule> {
reflect_dom_object(box CSSViewportRule::new_inherited(parent, viewportrule),
window,
Expand Down

0 comments on commit 53c9966

Please sign in to comment.