Remove stylesheets ownership from DocumentOrShadowRoot

This commit is contained in:
Fernando Jiménez Moreno 2019-02-08 19:57:00 +01:00
parent 3bb50cc479
commit 23b92d54d4
8 changed files with 200 additions and 124 deletions

View file

@ -119,6 +119,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::{Arc, Mutex};
use std::time::{Instant, SystemTime};
use style::attr::{AttrIdentifier, AttrValue, LengthOrPercentageOrAuto};
use style::author_styles::AuthorStyles;
use style::context::QuirksMode;
use style::dom::OpaqueNode;
use style::element_state::*;
@ -130,6 +131,7 @@ use style::stylesheet_set::{AuthorStylesheetSet, DocumentStylesheetSet};
use style::stylesheets::keyframes_rule::Keyframe;
use style::stylesheets::{CssRules, FontFaceRule, KeyframesRule, MediaRule, Stylesheet};
use style::stylesheets::{ImportRule, NamespaceRule, StyleRule, SupportsRule, ViewportRule};
use style::stylist::CascadeData;
use style::values::specified::Length;
use tendril::fmt::UTF8;
use tendril::stream::LossyDecoder;
@ -499,6 +501,7 @@ unsafe_no_jsmanaged_fields!(Rotation3D<f64>, Transform2D<f32>, Transform3D<f64>)
unsafe_no_jsmanaged_fields!(Point2D<f32>, Vector2D<f32>, Rect<Au>);
unsafe_no_jsmanaged_fields!(Rect<f32>, RigidTransform3D<f64>);
unsafe_no_jsmanaged_fields!(StyleSheetListOwner);
unsafe_no_jsmanaged_fields!(CascadeData);
unsafe impl<'a> JSTraceable for &'a str {
#[inline]
@ -728,6 +731,15 @@ where
}
}
unsafe impl<S> JSTraceable for AuthorStyles<S>
where
S: JSTraceable + ::style::stylesheets::StylesheetInDocument + PartialEq + 'static,
{
unsafe fn trace(&self, tracer: *mut JSTracer) {
self.stylesheets.trace(tracer)
}
}
unsafe impl<Sink> JSTraceable for LossyDecoder<Sink>
where
Sink: JSTraceable + TendrilSink<UTF8>,

View file

@ -156,7 +156,7 @@ use style::selector_parser::{RestyleDamage, Snapshot};
use style::shared_lock::SharedRwLock as StyleSharedRwLock;
use style::str::{split_html_space_chars, str_join};
use style::stylesheet_set::DocumentStylesheetSet;
use style::stylesheets::Stylesheet;
use style::stylesheets::{Origin, OriginSet, Stylesheet};
use url::percent_encoding::percent_decode;
use url::Host;
@ -574,7 +574,7 @@ impl Document {
// FIXME: This should check the dirty bit on the document,
// not the document element. Needs some layout changes to make
// that workable.
self.document_or_shadow_root.stylesheets_have_changed() ||
self.stylesheets.borrow().has_changed() ||
self.GetDocumentElement().map_or(false, |root| {
root.upcast::<Node>().has_dirty_descendants() ||
!self.pending_restyles.borrow().is_empty() ||
@ -1536,7 +1536,7 @@ impl Document {
}
pub fn invalidate_stylesheets(&self) {
self.document_or_shadow_root.invalidate_stylesheets();
self.stylesheets.borrow_mut().force_dirty(OriginSet::all());
// Mark the document element dirty so a reflow will be performed.
//
@ -2821,9 +2821,9 @@ impl Document {
// and normal stylesheets additions / removals, because in the last case
// the layout thread already has that information and we could avoid
// dirtying the whole thing.
let have_changed = self.document_or_shadow_root.stylesheets_have_changed();
self.document_or_shadow_root
.flush_stylesheets_without_invalidation();
let mut stylesheets = self.stylesheets.borrow_mut();
let have_changed = stylesheets.has_changed();
stylesheets.flush_without_invalidation();
have_changed
}
@ -4550,24 +4550,47 @@ impl PendingScript {
impl StyleSheetListOwner for Dom<Document> {
fn stylesheet_count(&self) -> usize {
self.document_or_shadow_root.stylesheet_count()
self.stylesheets.borrow().len()
}
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
self.document_or_shadow_root.stylesheet_at(index)
let stylesheets = self.stylesheets.borrow();
stylesheets
.get(Origin::Author, index)
.and_then(|s| s.owner.upcast::<Node>().get_cssom_stylesheet())
}
/// 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());
let stylesheets = &mut *self.stylesheets.borrow_mut();
let insertion_point = stylesheets
.iter()
.map(|(sheet, _origin)| sheet)
.find(|sheet_in_doc| {
owner
.upcast::<Node>()
.is_before(sheet_in_doc.owner.upcast())
})
.cloned();
self.document_or_shadow_root.add_stylesheet(
owner,
stylesheets,
sheet,
insertion_point,
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)
self.document_or_shadow_root.remove_stylesheet(
owner,
s,
&mut *self.stylesheets.borrow_mut(),
)
}
}

View file

@ -2,16 +2,14 @@
* 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/. */
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::element::Element;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlmetaelement::HTMLMetaElement;
use crate::dom::node::{self, Node};
use crate::dom::node;
use crate::dom::window::Window;
use euclid::Point2D;
use js::jsapi::JS_GetRuntime;
@ -22,15 +20,15 @@ use std::fmt;
use style::context::QuirksMode;
use style::media_queries::MediaList;
use style::shared_lock::{SharedRwLock as StyleSharedRwLock, SharedRwLockReadGuard};
use style::stylesheet_set::DocumentStylesheetSet;
use style::stylesheets::{CssRule, Origin, OriginSet, Stylesheet};
use style::stylesheet_set::StylesheetSet;
use style::stylesheets::{CssRule, Origin, Stylesheet};
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[must_root]
pub struct StyleSheetInDocument {
#[ignore_malloc_size_of = "Arc"]
sheet: Arc<Stylesheet>,
owner: Dom<Element>,
pub sheet: Arc<Stylesheet>,
pub owner: Dom<Element>,
}
impl fmt::Debug for StyleSheetInDocument {
@ -72,16 +70,12 @@ impl ::style::stylesheets::StylesheetInDocument for StyleSheetInDocument {
#[derive(JSTraceable, MallocSizeOf)]
pub struct DocumentOrShadowRoot {
window: Dom<Window>,
/// List of stylesheets associated with nodes in this document or shadow root.
/// |None| if the list needs to be refreshed.
stylesheets: DomRefCell<DocumentStylesheetSet<StyleSheetInDocument>>,
}
impl DocumentOrShadowRoot {
pub fn new(window: &Window) -> Self {
Self {
window: Dom::from_ref(window),
stylesheets: DomRefCell::new(DocumentStylesheetSet::new()),
}
}
@ -207,33 +201,14 @@ impl DocumentOrShadowRoot {
}
}
pub fn stylesheet_count(&self) -> usize {
self.stylesheets.borrow().len()
}
pub fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
let stylesheets = self.stylesheets.borrow();
stylesheets
.get(Origin::Author, index)
.and_then(|s| s.owner.upcast::<Node>().get_cssom_stylesheet())
}
pub fn stylesheets_have_changed(&self) -> bool {
self.stylesheets.borrow().has_changed()
}
pub fn invalidate_stylesheets(&self) {
self.stylesheets.borrow_mut().force_dirty(OriginSet::all());
}
pub fn flush_stylesheets_without_invalidation(&self) {
self.stylesheets.borrow_mut().flush_without_invalidation();
}
/// 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>) {
pub fn remove_stylesheet(
&self,
owner: &Element,
s: &Arc<Stylesheet>,
stylesheets: &mut StylesheetSet<StyleSheetInDocument>,
) {
self.window
.layout_chan()
.send(Msg::RemoveStylesheet(s.clone()))
@ -242,7 +217,7 @@ impl DocumentOrShadowRoot {
let guard = s.shared_lock.read();
// FIXME(emilio): Would be nice to remove the clone, etc.
self.stylesheets.borrow_mut().remove_stylesheet(
stylesheets.remove_stylesheet(
None,
StyleSheetInDocument {
sheet: s.clone(),
@ -258,7 +233,9 @@ impl DocumentOrShadowRoot {
pub fn add_stylesheet(
&self,
owner: &Element,
stylesheets: &mut StylesheetSet<StyleSheetInDocument>,
sheet: Arc<Stylesheet>,
insertion_point: Option<StyleSheetInDocument>,
style_shared_lock: &StyleSharedRwLock,
) {
// FIXME(emilio): It'd be nice to unify more code between the elements
@ -269,17 +246,6 @@ impl DocumentOrShadowRoot {
"Wat"
);
let mut stylesheets = self.stylesheets.borrow_mut();
let insertion_point = stylesheets
.iter()
.map(|(sheet, _origin)| sheet)
.find(|sheet_in_doc| {
owner
.upcast::<Node>()
.is_before(sheet_in_doc.owner.upcast())
})
.cloned();
self.window
.layout_chan()
.send(Msg::AddStylesheet(

View file

@ -82,7 +82,7 @@ impl HTMLStyleElement {
pub fn parse_own_css(&self) {
let node = self.upcast::<Node>();
let element = self.upcast::<Element>();
assert!(node.is_in_doc());
assert!(node.is_connected());
let window = window_from_node(node);
let doc = document_from_node(self);

View file

@ -19,7 +19,7 @@ use crate::dom::stylesheetlist::{StyleSheetList, StyleSheetListOwner};
use crate::dom::window::Window;
use dom_struct::dom_struct;
use servo_arc::Arc;
use style::stylesheet_set::AuthorStylesheetSet;
use style::author_styles::AuthorStyles;
use style::stylesheets::Stylesheet;
// https://dom.spec.whatwg.org/#interface-shadowroot
@ -31,7 +31,7 @@ pub struct ShadowRoot {
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>>,
author_styles: DomRefCell<AuthorStyles<StyleSheetInDocument>>,
stylesheet_list: MutNullableDom<StyleSheetList>,
window: Dom<Window>,
}
@ -48,7 +48,7 @@ impl ShadowRoot {
document_or_shadow_root: DocumentOrShadowRoot::new(document.window()),
document: Dom::from_ref(document),
host: Dom::from_ref(host),
stylesheets: DomRefCell::new(AuthorStylesheetSet::new()),
author_styles: DomRefCell::new(AuthorStyles::new()),
stylesheet_list: MutNullableDom::new(None),
window: Dom::from_ref(document.window()),
}
@ -131,20 +131,35 @@ impl LayoutShadowRootHelpers for LayoutDom<ShadowRoot> {
impl StyleSheetListOwner for Dom<ShadowRoot> {
fn stylesheet_count(&self) -> usize {
self.document_or_shadow_root.stylesheet_count()
self.author_styles.borrow().stylesheets.len()
}
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
self.document_or_shadow_root.stylesheet_at(index)
let stylesheets = &self.author_styles.borrow().stylesheets;
stylesheets
.get(index)
.and_then(|s| s.owner.upcast::<Node>().get_cssom_stylesheet())
}
/// 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>) {
let stylesheets = &mut self.author_styles.borrow_mut().stylesheets;
let insertion_point = stylesheets
.iter()
.find(|sheet_in_shadow| {
owner
.upcast::<Node>()
.is_before(sheet_in_shadow.owner.upcast())
})
.cloned();
self.document_or_shadow_root.add_stylesheet(
owner,
stylesheets,
sheet,
insertion_point,
self.document.style_shared_lock(),
);
}
@ -152,6 +167,10 @@ impl StyleSheetListOwner for Dom<ShadowRoot> {
/// 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)
self.document_or_shadow_root.remove_stylesheet(
owner,
s,
&mut self.author_styles.borrow_mut().stylesheets,
)
}
}