Flush shadow roots stylesheets only if they changed

This commit is contained in:
Fernando Jiménez Moreno 2019-02-21 12:34:59 +01:00
parent a841c713d6
commit 2674a3e717
4 changed files with 54 additions and 7 deletions

View file

@ -379,6 +379,8 @@ pub struct Document {
completely_loaded: Cell<bool>,
/// List of shadow roots bound to the document tree.
shadow_roots: DomRefCell<Vec<Dom<ShadowRoot>>>,
/// Whether any of the shadow roots need the stylesheets flushed.
shadow_roots_styles_changed: Cell<bool>,
}
#[derive(JSTraceable, MallocSizeOf)]
@ -2431,6 +2433,8 @@ pub trait LayoutDocumentHelpers {
unsafe fn quirks_mode(&self) -> QuirksMode;
unsafe fn style_shared_lock(&self) -> &StyleSharedRwLock;
unsafe fn shadow_roots(&self) -> Vec<LayoutDom<ShadowRoot>>;
unsafe fn shadow_roots_styles_changed(&self) -> bool;
unsafe fn flush_shadow_roots_stylesheets(&self);
}
#[allow(unsafe_code)]
@ -2485,6 +2489,16 @@ impl LayoutDocumentHelpers for LayoutDom<Document> {
.map(|sr| sr.to_layout())
.collect()
}
#[inline]
unsafe fn shadow_roots_styles_changed(&self) -> bool {
(*self.unsafe_get()).shadow_roots_styles_changed()
}
#[inline]
unsafe fn flush_shadow_roots_stylesheets(&self) {
(*self.unsafe_get()).flush_shadow_roots_stylesheets()
}
}
// https://html.spec.whatwg.org/multipage/#is-a-registrable-domain-suffix-of-or-is-equal-to
@ -2694,6 +2708,7 @@ impl Document {
script_and_layout_blockers: Cell::new(0),
delayed_tasks: Default::default(),
shadow_roots: DomRefCell::new(Vec::new()),
shadow_roots_styles_changed: Cell::new(false),
}
}
@ -3151,6 +3166,7 @@ impl Document {
self.shadow_roots
.borrow_mut()
.push(Dom::from_ref(shadow_root));
self.invalidate_shadow_roots_stylesheets();
}
pub fn unregister_shadow_root(&self, shadow_root: &ShadowRoot) {
@ -3160,6 +3176,21 @@ impl Document {
shadow_roots.remove(index);
}
}
pub fn invalidate_shadow_roots_stylesheets(&self) {
self.shadow_roots_styles_changed.set(true);
}
pub fn shadow_roots_styles_changed(&self) -> bool {
self.shadow_roots_styles_changed.get()
}
pub fn flush_shadow_roots_stylesheets(&self) {
if !self.shadow_roots_styles_changed.get() {
return;
}
self.shadow_roots_styles_changed.set(false);
}
}
impl Element {