Make CSSRule always keep a pointer to its parent stylesheet

even when the parentStylesheet IDL attribute returns null.
This commit is contained in:
Simon Sapin 2016-11-28 16:33:47 +01:00
parent 0714e2291c
commit f1d49d3773
11 changed files with 84 additions and 69 deletions

View file

@ -5,7 +5,7 @@
use dom::bindings::codegen::Bindings::CSSRuleBinding;
use dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleMethods;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::cssfontfacerule::CSSFontFaceRule;
@ -17,27 +17,34 @@ use dom::cssstylerule::CSSStyleRule;
use dom::cssstylesheet::CSSStyleSheet;
use dom::cssviewportrule::CSSViewportRule;
use dom::window::Window;
use std::cell::Cell;
use style::stylesheets::CssRule as StyleCssRule;
#[dom_struct]
pub struct CSSRule {
reflector_: Reflector,
parent: MutNullableHeap<JS<CSSStyleSheet>>,
parent_stylesheet: JS<CSSStyleSheet>,
/// Whether the parentStyleSheet attribute should return null.
/// We keep parent_stylesheet in that case because insertRule needs it
/// for the stylesheets base URL and namespace prefixes.
parent_stylesheet_removed: Cell<bool>,
}
impl CSSRule {
#[allow(unrooted_must_root)]
pub fn new_inherited(parent: Option<&CSSStyleSheet>) -> CSSRule {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet) -> CSSRule {
CSSRule {
reflector_: Reflector::new(),
parent: MutNullableHeap::new(parent),
parent_stylesheet: JS::from_ref(parent_stylesheet),
parent_stylesheet_removed: Cell::new(false),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>) -> Root<CSSRule> {
reflect_dom_object(box CSSRule::new_inherited(parent),
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet) -> Root<CSSRule> {
reflect_dom_object(box CSSRule::new_inherited(parent_stylesheet),
window,
CSSRuleBinding::Wrap)
}
@ -64,16 +71,16 @@ 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: Option<&CSSStyleSheet>,
pub fn new_specific(window: &Window, parent_stylesheet: &CSSStyleSheet,
rule: StyleCssRule) -> Root<CSSRule> {
// be sure to update the match in as_specific when this is updated
match rule {
StyleCssRule::Style(s) => Root::upcast(CSSStyleRule::new(window, parent, s)),
StyleCssRule::FontFace(s) => Root::upcast(CSSFontFaceRule::new(window, parent, s)),
StyleCssRule::Keyframes(s) => Root::upcast(CSSKeyframesRule::new(window, parent, s)),
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent, s)),
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::new(window, parent, s)),
StyleCssRule::Viewport(s) => Root::upcast(CSSViewportRule::new(window, parent, s)),
StyleCssRule::Style(s) => Root::upcast(CSSStyleRule::new(window, parent_stylesheet, s)),
StyleCssRule::FontFace(s) => Root::upcast(CSSFontFaceRule::new(window, parent_stylesheet, s)),
StyleCssRule::Keyframes(s) => Root::upcast(CSSKeyframesRule::new(window, parent_stylesheet, s)),
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent_stylesheet, s)),
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::new(window, parent_stylesheet, s)),
StyleCssRule::Viewport(s) => Root::upcast(CSSViewportRule::new(window, parent_stylesheet, s)),
}
}
@ -85,12 +92,16 @@ impl CSSRule {
/// Sets owner sheet to null (and does the same for all children)
pub fn deparent(&self) {
self.parent.set(None);
self.parent_stylesheet_removed.set(true);
// https://github.com/w3c/csswg-drafts/issues/722
// Spec doesn't ask us to do this, but it makes sense
// and browsers implement this behavior
self.as_specific().deparent_children();
}
pub fn parent_stylesheet(&self) -> &CSSStyleSheet {
&self.parent_stylesheet
}
}
impl CSSRuleMethods for CSSRule {
@ -101,7 +112,11 @@ impl CSSRuleMethods for CSSRule {
// https://drafts.csswg.org/cssom/#dom-cssrule-parentstylesheet
fn GetParentStyleSheet(&self) -> Option<Root<CSSStyleSheet>> {
self.parent.get()
if self.parent_stylesheet_removed.get() {
None
} else {
Some(Root::from_ref(&*self.parent_stylesheet))
}
}
// https://drafts.csswg.org/cssom/#dom-cssrule-csstext
@ -118,7 +133,7 @@ impl CSSRuleMethods for CSSRule {
pub trait SpecificCSSRule {
fn ty(&self) -> u16;
fn get_css(&self) -> DOMString;
/// Remove CSSStyleSheet parent from all transitive children
/// Remove parentStylesheet from all transitive children
fn deparent_children(&self) {
// most CSSRules do nothing here
}