aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorVincent Ricard <magic@magicninja.org>2020-10-08 22:30:22 +0200
committerVincent Ricard <magic@magicninja.org>2020-10-10 21:21:00 +0200
commite7199c029f3ce276c77ce9680eb198fdfdb650d8 (patch)
tree5c819d27f614b6dd142af1efc4bbbb80c03052b1 /components/script/dom
parent406d15974f3231dc769c2703e89b986ef9b20210 (diff)
downloadservo-e7199c029f3ce276c77ce9680eb198fdfdb650d8.tar.gz
servo-e7199c029f3ce276c77ce9680eb198fdfdb650d8.zip
Implements Stylesheet.ownerNode
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/cssrulelist.rs12
-rw-r--r--components/script/dom/cssstyledeclaration.rs6
-rw-r--r--components/script/dom/cssstylerule.rs5
-rw-r--r--components/script/dom/cssstylesheet.rs23
-rw-r--r--components/script/dom/htmllinkelement.rs10
-rw-r--r--components/script/dom/htmlstyleelement.rs10
-rw-r--r--components/script/dom/stylesheet.rs7
-rw-r--r--components/script/dom/webidls/StyleSheet.webidl2
8 files changed, 52 insertions, 23 deletions
diff --git a/components/script/dom/cssrulelist.rs b/components/script/dom/cssrulelist.rs
index 812434fd180..d4fde8ae308 100644
--- a/components/script/dom/cssrulelist.rs
+++ b/components/script/dom/cssrulelist.rs
@@ -5,7 +5,6 @@
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::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::csskeyframerule::CSSKeyframeRule;
@@ -13,6 +12,7 @@ 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 servo_arc::Arc;
@@ -107,9 +107,11 @@ impl CSSRuleList {
let owner = self
.parent_stylesheet
.get_owner()
- .downcast::<HTMLElement>()
- .unwrap();
- let loader = StylesheetLoader::for_element(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,
@@ -117,7 +119,7 @@ impl CSSRuleList {
&parent_stylesheet.contents,
index,
nested,
- Some(&loader),
+ loader.as_ref().map(|l| l as &dyn StyleStylesheetLoader),
AllowImportRules::Yes,
)
})?;
diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs
index ba7112b8899..3c0d63ce92f 100644
--- a/components/script/dom/cssstyledeclaration.rs
+++ b/components/script/dom/cssstyledeclaration.rs
@@ -113,8 +113,10 @@ impl CSSStyleOwner {
if changed {
// If this is changed, see also
// CSSStyleRule::SetSelectorText, which does the same thing.
- stylesheets_owner_from_node(rule.parent_stylesheet().owner().upcast::<Node>())
- .invalidate_stylesheets();
+ if let Some(owner) = rule.parent_stylesheet().get_owner() {
+ stylesheets_owner_from_node(owner.upcast::<Node>())
+ .invalidate_stylesheets();
+ }
}
result
},
diff --git a/components/script/dom/cssstylerule.rs b/components/script/dom/cssstylerule.rs
index 30e4e197ce5..a84c5c598b7 100644
--- a/components/script/dom/cssstylerule.rs
+++ b/components/script/dom/cssstylerule.rs
@@ -117,8 +117,9 @@ impl CSSStyleRuleMethods for CSSStyleRule {
let mut guard = self.cssrule.shared_lock().write();
let stylerule = self.stylerule.write_with(&mut guard);
mem::swap(&mut stylerule.selectors, &mut s);
- stylesheets_owner_from_node(self.cssrule.parent_stylesheet().owner().upcast::<Node>())
- .invalidate_stylesheets();
+ if let Some(owner) = self.cssrule.parent_stylesheet().get_owner() {
+ stylesheets_owner_from_node(owner.upcast::<Node>()).invalidate_stylesheets();
+ }
}
}
}
diff --git a/components/script/dom/cssstylesheet.rs b/components/script/dom/cssstylesheet.rs
index 35277ddc0fc..7ee5e444394 100644
--- a/components/script/dom/cssstylesheet.rs
+++ b/components/script/dom/cssstylesheet.rs
@@ -6,7 +6,7 @@ use crate::dom::bindings::codegen::Bindings::CSSStyleSheetBinding::CSSStyleSheet
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
-use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
+use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::cssrulelist::{CSSRuleList, RulesSource};
use crate::dom::element::Element;
@@ -22,7 +22,7 @@ use style::stylesheets::Stylesheet as StyleStyleSheet;
#[dom_struct]
pub struct CSSStyleSheet {
stylesheet: StyleSheet,
- owner: Dom<Element>,
+ owner: MutNullableDom<Element>,
rulelist: MutNullableDom<CSSRuleList>,
#[ignore_malloc_size_of = "Arc"]
style_stylesheet: Arc<StyleStyleSheet>,
@@ -39,7 +39,7 @@ impl CSSStyleSheet {
) -> CSSStyleSheet {
CSSStyleSheet {
stylesheet: StyleSheet::new_inherited(type_, href, title),
- owner: Dom::from_ref(owner),
+ owner: MutNullableDom::new(Some(owner)),
rulelist: MutNullableDom::new(None),
style_stylesheet: stylesheet,
origin_clean: Cell::new(true),
@@ -63,10 +63,6 @@ impl CSSStyleSheet {
)
}
- pub fn owner(&self) -> DomRoot<Element> {
- DomRoot::from_ref(&*self.owner)
- }
-
fn rulelist(&self) -> DomRoot<CSSRuleList> {
self.rulelist.or_init(|| {
let rules = self.style_stylesheet.contents.rules.clone();
@@ -78,16 +74,21 @@ impl CSSStyleSheet {
self.style_stylesheet.disabled()
}
- pub fn get_owner(&self) -> &Element {
- &*self.owner
+ pub fn get_owner(&self) -> Option<DomRoot<Element>> {
+ self.owner.get()
}
pub fn set_disabled(&self, disabled: bool) {
- if self.style_stylesheet.set_disabled(disabled) {
- stylesheets_owner_from_node(self.owner().upcast::<Node>()).invalidate_stylesheets();
+ if self.style_stylesheet.set_disabled(disabled) && self.get_owner().is_some() {
+ stylesheets_owner_from_node(self.get_owner().unwrap().upcast::<Node>())
+ .invalidate_stylesheets();
}
}
+ pub fn set_owner(&self, value: Option<&Element>) {
+ self.owner.set(value);
+ }
+
pub fn shared_lock(&self) -> &SharedRwLock {
&self.style_stylesheet.shared_lock
}
diff --git a/components/script/dom/htmllinkelement.rs b/components/script/dom/htmllinkelement.rs
index 93879fb60b7..43df6000184 100644
--- a/components/script/dom/htmllinkelement.rs
+++ b/components/script/dom/htmllinkelement.rs
@@ -116,7 +116,7 @@ impl HTMLLinkElement {
stylesheets_owner.remove_stylesheet(self.upcast(), s)
}
*self.stylesheet.borrow_mut() = Some(s.clone());
- self.cssom_stylesheet.set(None);
+ self.clean_stylesheet_ownership();
stylesheets_owner.add_stylesheet(self.upcast(), s);
}
@@ -148,6 +148,13 @@ impl HTMLLinkElement {
None => false,
}
}
+
+ fn clean_stylesheet_ownership(&self) {
+ if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
+ cssom_stylesheet.set_owner(None);
+ }
+ self.cssom_stylesheet.set(None);
+ }
}
fn get_attr(element: &Element, local_name: &LocalName) -> Option<String> {
@@ -255,6 +262,7 @@ impl VirtualMethods for HTMLLinkElement {
}
if let Some(s) = self.stylesheet.borrow_mut().take() {
+ self.clean_stylesheet_ownership();
stylesheets_owner_from_node(self).remove_stylesheet(self.upcast(), &s);
}
}
diff --git a/components/script/dom/htmlstyleelement.rs b/components/script/dom/htmlstyleelement.rs
index 6d10b0838f7..4e81837fc35 100644
--- a/components/script/dom/htmlstyleelement.rs
+++ b/components/script/dom/htmlstyleelement.rs
@@ -144,7 +144,7 @@ impl HTMLStyleElement {
stylesheets_owner.remove_stylesheet(self.upcast(), s)
}
*self.stylesheet.borrow_mut() = Some(s.clone());
- self.cssom_stylesheet.set(None);
+ self.clean_stylesheet_ownership();
stylesheets_owner.add_stylesheet(self.upcast(), s);
}
@@ -166,6 +166,13 @@ impl HTMLStyleElement {
})
})
}
+
+ fn clean_stylesheet_ownership(&self) {
+ if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
+ cssom_stylesheet.set_owner(None);
+ }
+ self.cssom_stylesheet.set(None);
+ }
}
impl VirtualMethods for HTMLStyleElement {
@@ -217,6 +224,7 @@ impl VirtualMethods for HTMLStyleElement {
if context.tree_connected {
if let Some(s) = self.stylesheet.borrow_mut().take() {
+ self.clean_stylesheet_ownership();
stylesheets_owner_from_node(self).remove_stylesheet(self.upcast(), &s)
}
}
diff --git a/components/script/dom/stylesheet.rs b/components/script/dom/stylesheet.rs
index 7d11ccdb897..56ce505d152 100644
--- a/components/script/dom/stylesheet.rs
+++ b/components/script/dom/stylesheet.rs
@@ -5,8 +5,10 @@
use crate::dom::bindings::codegen::Bindings::StyleSheetBinding::StyleSheetMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::Reflector;
+use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::cssstylesheet::CSSStyleSheet;
+use crate::dom::element::Element;
use dom_struct::dom_struct;
#[dom_struct]
@@ -44,6 +46,11 @@ impl StyleSheetMethods for StyleSheet {
self.href.clone()
}
+ // https://drafts.csswg.org/cssom/#dom-stylesheet-ownernode
+ fn GetOwnerNode(&self) -> Option<DomRoot<Element>> {
+ self.downcast::<CSSStyleSheet>().and_then(|s| s.get_owner())
+ }
+
// https://drafts.csswg.org/cssom/#dom-stylesheet-title
fn GetTitle(&self) -> Option<DOMString> {
self.title.clone()
diff --git a/components/script/dom/webidls/StyleSheet.webidl b/components/script/dom/webidls/StyleSheet.webidl
index 827bd30a9af..67ac5f965e1 100644
--- a/components/script/dom/webidls/StyleSheet.webidl
+++ b/components/script/dom/webidls/StyleSheet.webidl
@@ -8,7 +8,7 @@ interface StyleSheet {
readonly attribute DOMString type_;
readonly attribute DOMString? href;
- // readonly attribute (Element or ProcessingInstruction)? ownerNode;
+ readonly attribute Element? ownerNode;
// readonly attribute StyleSheet? parentStyleSheet;
readonly attribute DOMString? title;