ShadowRoot stylesheet list

This commit is contained in:
Fernando Jiménez Moreno 2019-02-05 20:50:44 +01:00
parent 0d6bd24245
commit 3bb50cc479
8 changed files with 93 additions and 29 deletions

View file

@ -126,7 +126,7 @@ use style::media_queries::MediaList;
use style::properties::PropertyDeclarationBlock; use style::properties::PropertyDeclarationBlock;
use style::selector_parser::{PseudoElement, Snapshot}; use style::selector_parser::{PseudoElement, Snapshot};
use style::shared_lock::{Locked as StyleLocked, SharedRwLock as StyleSharedRwLock}; use style::shared_lock::{Locked as StyleLocked, SharedRwLock as StyleSharedRwLock};
use style::stylesheet_set::DocumentStylesheetSet; use style::stylesheet_set::{AuthorStylesheetSet, DocumentStylesheetSet};
use style::stylesheets::keyframes_rule::Keyframe; use style::stylesheets::keyframes_rule::Keyframe;
use style::stylesheets::{CssRules, FontFaceRule, KeyframesRule, MediaRule, Stylesheet}; use style::stylesheets::{CssRules, FontFaceRule, KeyframesRule, MediaRule, Stylesheet};
use style::stylesheets::{ImportRule, NamespaceRule, StyleRule, SupportsRule, ViewportRule}; use style::stylesheets::{ImportRule, NamespaceRule, StyleRule, SupportsRule, ViewportRule};
@ -717,6 +717,17 @@ where
} }
} }
unsafe impl<S> JSTraceable for AuthorStylesheetSet<S>
where
S: JSTraceable + ::style::stylesheets::StylesheetInDocument + PartialEq + 'static,
{
unsafe fn trace(&self, tracer: *mut JSTracer) {
for s in self.iter() {
s.trace(tracer)
}
}
}
unsafe impl<Sink> JSTraceable for LossyDecoder<Sink> unsafe impl<Sink> JSTraceable for LossyDecoder<Sink>
where where
Sink: JSTraceable + TendrilSink<UTF8>, Sink: JSTraceable + TendrilSink<UTF8>,

View file

@ -2838,20 +2838,6 @@ impl Document {
Device::new(MediaType::screen(), viewport_size, device_pixel_ratio) Device::new(MediaType::screen(), viewport_size, device_pixel_ratio)
} }
/// Remove a stylesheet owned by `owner` from the list of document sheets.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
pub fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
self.document_or_shadow_root.remove_stylesheet(owner, s)
}
/// Add a stylesheet owned by `owner` to the list of document sheets, in the
/// correct tree position.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
pub fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>) {
self.document_or_shadow_root
.add_stylesheet(owner, sheet, self.style_shared_lock());
}
pub fn salvageable(&self) -> bool { pub fn salvageable(&self) -> bool {
self.salvageable.get() self.salvageable.get()
} }
@ -4570,4 +4556,18 @@ impl StyleSheetListOwner for Dom<Document> {
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> { fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
self.document_or_shadow_root.stylesheet_at(index) self.document_or_shadow_root.stylesheet_at(index)
} }
/// Add a stylesheet owned by `owner` to the list of document sheets, in the
/// correct tree position.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>) {
self.document_or_shadow_root
.add_stylesheet(owner, sheet, self.style_shared_lock());
}
/// Remove a stylesheet owned by `owner` from the list of document sheets.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
self.document_or_shadow_root.remove_stylesheet(owner, s)
}
} }

View file

@ -18,7 +18,9 @@ use crate::dom::element::{
}; };
use crate::dom::element::{AttributeMutation, Element, ElementCreator}; use crate::dom::element::{AttributeMutation, Element, ElementCreator};
use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlelement::HTMLElement;
use crate::dom::node::{document_from_node, window_from_node, Node, UnbindContext}; use crate::dom::node::{
document_from_node, stylesheets_owner_from_node, window_from_node, Node, UnbindContext,
};
use crate::dom::stylesheet::StyleSheet as DOMStyleSheet; use crate::dom::stylesheet::StyleSheet as DOMStyleSheet;
use crate::dom::virtualmethods::VirtualMethods; use crate::dom::virtualmethods::VirtualMethods;
use crate::stylesheet_loader::{StylesheetContextSource, StylesheetLoader, StylesheetOwner}; use crate::stylesheet_loader::{StylesheetContextSource, StylesheetLoader, StylesheetOwner};
@ -108,13 +110,13 @@ impl HTMLLinkElement {
// FIXME(emilio): These methods are duplicated with // FIXME(emilio): These methods are duplicated with
// HTMLStyleElement::set_stylesheet. // HTMLStyleElement::set_stylesheet.
pub fn set_stylesheet(&self, s: Arc<Stylesheet>) { pub fn set_stylesheet(&self, s: Arc<Stylesheet>) {
let doc = document_from_node(self); let stylesheets_owner = stylesheets_owner_from_node(self);
if let Some(ref s) = *self.stylesheet.borrow() { if let Some(ref s) = *self.stylesheet.borrow() {
doc.remove_stylesheet(self.upcast(), s) stylesheets_owner.remove_stylesheet(self.upcast(), s)
} }
*self.stylesheet.borrow_mut() = Some(s.clone()); *self.stylesheet.borrow_mut() = Some(s.clone());
self.cssom_stylesheet.set(None); self.cssom_stylesheet.set(None);
doc.add_stylesheet(self.upcast(), s); stylesheets_owner.add_stylesheet(self.upcast(), s);
} }
pub fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> { pub fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> {
@ -252,7 +254,7 @@ impl VirtualMethods for HTMLLinkElement {
} }
if let Some(s) = self.stylesheet.borrow_mut().take() { if let Some(s) = self.stylesheet.borrow_mut().take() {
document_from_node(self).remove_stylesheet(self.upcast(), &s); stylesheets_owner_from_node(self).remove_stylesheet(self.upcast(), &s);
} }
} }
} }

View file

@ -15,7 +15,9 @@ use crate::dom::document::Document;
use crate::dom::element::{AttributeMutation, Element}; use crate::dom::element::{AttributeMutation, Element};
use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlheadelement::HTMLHeadElement; use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::node::{document_from_node, window_from_node, Node, UnbindContext}; use crate::dom::node::{
document_from_node, stylesheets_owner_from_node, window_from_node, Node, UnbindContext,
};
use crate::dom::virtualmethods::VirtualMethods; use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix}; use html5ever::{LocalName, Prefix};
@ -106,6 +108,7 @@ impl HTMLMetaElement {
let content = content.value(); let content = content.value();
if !content.is_empty() { if !content.is_empty() {
if let Some(translated_rule) = ViewportRule::from_meta(&**content) { if let Some(translated_rule) = ViewportRule::from_meta(&**content) {
let stylesheets_owner = stylesheets_owner_from_node(self);
let document = document_from_node(self); let document = document_from_node(self);
let shared_lock = document.style_shared_lock(); let shared_lock = document.style_shared_lock();
let rule = CssRule::Viewport(Arc::new(shared_lock.wrap(translated_rule))); let rule = CssRule::Viewport(Arc::new(shared_lock.wrap(translated_rule)));
@ -124,7 +127,7 @@ impl HTMLMetaElement {
disabled: AtomicBool::new(false), disabled: AtomicBool::new(false),
}); });
*self.stylesheet.borrow_mut() = Some(sheet.clone()); *self.stylesheet.borrow_mut() = Some(sheet.clone());
document.add_stylesheet(self.upcast(), sheet); stylesheets_owner.add_stylesheet(self.upcast(), sheet);
} }
} }
} }
@ -208,7 +211,7 @@ impl VirtualMethods for HTMLMetaElement {
self.process_referrer_attribute(); self.process_referrer_attribute();
if let Some(s) = self.stylesheet.borrow_mut().take() { if let Some(s) = self.stylesheet.borrow_mut().take() {
document_from_node(self).remove_stylesheet(self.upcast(), &s); stylesheets_owner_from_node(self).remove_stylesheet(self.upcast(), &s);
} }
} }
} }

View file

@ -13,7 +13,8 @@ use crate::dom::document::Document;
use crate::dom::element::{Element, ElementCreator}; use crate::dom::element::{Element, ElementCreator};
use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlelement::HTMLElement;
use crate::dom::node::{ use crate::dom::node::{
document_from_node, window_from_node, ChildrenMutation, Node, UnbindContext, document_from_node, stylesheets_owner_from_node, window_from_node, ChildrenMutation, Node,
UnbindContext,
}; };
use crate::dom::stylesheet::StyleSheet as DOMStyleSheet; use crate::dom::stylesheet::StyleSheet as DOMStyleSheet;
use crate::dom::virtualmethods::VirtualMethods; use crate::dom::virtualmethods::VirtualMethods;
@ -138,13 +139,13 @@ impl HTMLStyleElement {
// FIXME(emilio): This is duplicated with HTMLLinkElement::set_stylesheet. // FIXME(emilio): This is duplicated with HTMLLinkElement::set_stylesheet.
pub fn set_stylesheet(&self, s: Arc<Stylesheet>) { pub fn set_stylesheet(&self, s: Arc<Stylesheet>) {
let doc = document_from_node(self); let stylesheets_owner = stylesheets_owner_from_node(self);
if let Some(ref s) = *self.stylesheet.borrow() { if let Some(ref s) = *self.stylesheet.borrow() {
doc.remove_stylesheet(self.upcast(), s) stylesheets_owner.remove_stylesheet(self.upcast(), s)
} }
*self.stylesheet.borrow_mut() = Some(s.clone()); *self.stylesheet.borrow_mut() = Some(s.clone());
self.cssom_stylesheet.set(None); self.cssom_stylesheet.set(None);
doc.add_stylesheet(self.upcast(), s); stylesheets_owner.add_stylesheet(self.upcast(), s);
} }
pub fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> { pub fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> {
@ -216,7 +217,7 @@ impl VirtualMethods for HTMLStyleElement {
if context.tree_connected { if context.tree_connected {
if let Some(s) = self.stylesheet.borrow_mut().take() { if let Some(s) = self.stylesheet.borrow_mut().take() {
document_from_node(self).remove_stylesheet(self.upcast(), &s) stylesheets_owner_from_node(self).remove_stylesheet(self.upcast(), &s)
} }
} }
} }

View file

@ -54,6 +54,7 @@ use crate::dom::nodelist::NodeList;
use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::processinginstruction::ProcessingInstruction;
use crate::dom::range::WeakRangeVec; use crate::dom::range::WeakRangeVec;
use crate::dom::shadowroot::ShadowRoot; use crate::dom::shadowroot::ShadowRoot;
use crate::dom::stylesheetlist::StyleSheetListOwner;
use crate::dom::svgsvgelement::{LayoutSVGSVGElementHelpers, SVGSVGElement}; use crate::dom::svgsvgelement::{LayoutSVGSVGElementHelpers, SVGSVGElement};
use crate::dom::text::Text; use crate::dom::text::Text;
use crate::dom::virtualmethods::{vtable_for, VirtualMethods}; use crate::dom::virtualmethods::{vtable_for, VirtualMethods};
@ -2786,6 +2787,22 @@ pub fn document_from_node<T: DerivedFrom<Node> + DomObject>(derived: &T) -> DomR
derived.upcast().owner_doc() derived.upcast().owner_doc()
} }
pub fn shadow_root_from_node<T: DerivedFrom<Node> + DomObject>(
derived: &T,
) -> Option<DomRoot<ShadowRoot>> {
derived.upcast().owner_shadow_root()
}
pub fn stylesheets_owner_from_node<T: DerivedFrom<Node> + DomObject>(
derived: &T,
) -> Box<StyleSheetListOwner> {
if let Some(shadow_root) = shadow_root_from_node(derived) {
Box::new(Dom::from_ref(&*shadow_root))
} else {
Box::new(Dom::from_ref(&*document_from_node(derived)))
}
}
pub fn window_from_node<T: DerivedFrom<Node> + DomObject>(derived: &T) -> DomRoot<Window> { pub fn window_from_node<T: DerivedFrom<Node> + DomObject>(derived: &T) -> DomRoot<Window> {
let document = document_from_node(derived); let document = document_from_node(derived);
DomRoot::from_ref(document.window()) DomRoot::from_ref(document.window())

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootBinding::ShadowRootMethods; use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootBinding::ShadowRootMethods;
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{self, ShadowRootMode}; use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{self, ShadowRootMode};
use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::inheritance::Castable;
@ -11,12 +12,15 @@ use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom};
use crate::dom::cssstylesheet::CSSStyleSheet; use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::document::Document; use crate::dom::document::Document;
use crate::dom::documentfragment::DocumentFragment; use crate::dom::documentfragment::DocumentFragment;
use crate::dom::documentorshadowroot::DocumentOrShadowRoot; use crate::dom::documentorshadowroot::{DocumentOrShadowRoot, StyleSheetInDocument};
use crate::dom::element::Element; use crate::dom::element::Element;
use crate::dom::node::{Node, NodeFlags}; use crate::dom::node::{Node, NodeFlags};
use crate::dom::stylesheetlist::{StyleSheetList, StyleSheetListOwner}; use crate::dom::stylesheetlist::{StyleSheetList, StyleSheetListOwner};
use crate::dom::window::Window; use crate::dom::window::Window;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use servo_arc::Arc;
use style::stylesheet_set::AuthorStylesheetSet;
use style::stylesheets::Stylesheet;
// https://dom.spec.whatwg.org/#interface-shadowroot // https://dom.spec.whatwg.org/#interface-shadowroot
#[dom_struct] #[dom_struct]
@ -25,6 +29,9 @@ pub struct ShadowRoot {
document_or_shadow_root: DocumentOrShadowRoot, document_or_shadow_root: DocumentOrShadowRoot,
document: Dom<Document>, document: Dom<Document>,
host: Dom<Element>, host: Dom<Element>,
/// List of stylesheets associated with nodes in this shadow tree.
/// |None| if the list needs to be refreshed.
stylesheets: DomRefCell<AuthorStylesheetSet<StyleSheetInDocument>>,
stylesheet_list: MutNullableDom<StyleSheetList>, stylesheet_list: MutNullableDom<StyleSheetList>,
window: Dom<Window>, window: Dom<Window>,
} }
@ -41,6 +48,7 @@ impl ShadowRoot {
document_or_shadow_root: DocumentOrShadowRoot::new(document.window()), document_or_shadow_root: DocumentOrShadowRoot::new(document.window()),
document: Dom::from_ref(document), document: Dom::from_ref(document),
host: Dom::from_ref(host), host: Dom::from_ref(host),
stylesheets: DomRefCell::new(AuthorStylesheetSet::new()),
stylesheet_list: MutNullableDom::new(None), stylesheet_list: MutNullableDom::new(None),
window: Dom::from_ref(document.window()), window: Dom::from_ref(document.window()),
} }
@ -129,4 +137,21 @@ impl StyleSheetListOwner for Dom<ShadowRoot> {
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> { fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
self.document_or_shadow_root.stylesheet_at(index) self.document_or_shadow_root.stylesheet_at(index)
} }
/// Add a stylesheet owned by `owner` to the list of shadow root sheets, in the
/// correct tree position.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>) {
self.document_or_shadow_root.add_stylesheet(
owner,
sheet,
self.document.style_shared_lock(),
);
}
/// Remove a stylesheet owned by `owner` from the list of shadow root sheets.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
self.document_or_shadow_root.remove_stylesheet(owner, s)
}
} }

View file

@ -7,13 +7,18 @@ use crate::dom::bindings::codegen::Bindings::StyleSheetListBinding::StyleSheetLi
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::root::DomRoot;
use crate::dom::cssstylesheet::CSSStyleSheet; use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::element::Element;
use crate::dom::stylesheet::StyleSheet; use crate::dom::stylesheet::StyleSheet;
use crate::dom::window::Window; use crate::dom::window::Window;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use servo_arc::Arc;
use style::stylesheets::Stylesheet;
pub trait StyleSheetListOwner { pub trait StyleSheetListOwner {
fn stylesheet_count(&self) -> usize; fn stylesheet_count(&self) -> usize;
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>>; fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>>;
fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>);
fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>);
} }
#[dom_struct] #[dom_struct]