From 8b353ee3ced609d2de688a93dcfd825c3cef3eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Fri, 1 Mar 2019 11:43:45 +0100 Subject: [PATCH] Make StylesheetSet an enum instead of a trait object --- components/script/dom/document.rs | 10 ++-- components/script/dom/documentorshadowroot.rs | 6 +-- components/script/dom/shadowroot.rs | 9 ++-- components/style/stylesheet_set.rs | 54 +++++++++++++------ 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 8e1c09c2361..bef1edc68fd 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -155,7 +155,7 @@ use style::media_queries::{Device, MediaType}; 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::stylesheet_set::{DocumentStylesheetSet, StylesheetSet}; use style::stylesheets::{Origin, OriginSet, Stylesheet}; use url::percent_encoding::percent_decode; use url::Host; @@ -4643,9 +4643,9 @@ impl StyleSheetListOwner for Dom { )) .unwrap(); - self.document_or_shadow_root.add_stylesheet( + DocumentOrShadowRoot::add_stylesheet( owner, - stylesheets, + StylesheetSet::Document(stylesheets), sheet, insertion_point, self.style_shared_lock(), @@ -4660,10 +4660,10 @@ impl StyleSheetListOwner for Dom { .send(Msg::RemoveStylesheet(s.clone())) .unwrap(); - self.document_or_shadow_root.remove_stylesheet( + DocumentOrShadowRoot::remove_stylesheet( owner, s, - &mut *self.stylesheets.borrow_mut(), + StylesheetSet::Document(&mut *self.stylesheets.borrow_mut()), ) } diff --git a/components/script/dom/documentorshadowroot.rs b/components/script/dom/documentorshadowroot.rs index 27d52e086c6..0165c0b8384 100644 --- a/components/script/dom/documentorshadowroot.rs +++ b/components/script/dom/documentorshadowroot.rs @@ -211,10 +211,9 @@ impl DocumentOrShadowRoot { /// 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, - stylesheets: &mut StylesheetSet, + mut stylesheets: StylesheetSet, ) { let guard = s.shared_lock.read(); @@ -233,9 +232,8 @@ impl DocumentOrShadowRoot { /// correct tree position. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. pub fn add_stylesheet( - &self, owner: &Element, - stylesheets: &mut StylesheetSet, + mut stylesheets: StylesheetSet, sheet: Arc, insertion_point: Option, style_shared_lock: &StyleSharedRwLock, diff --git a/components/script/dom/shadowroot.rs b/components/script/dom/shadowroot.rs index d99a5f3b3d7..5233e7b4143 100644 --- a/components/script/dom/shadowroot.rs +++ b/components/script/dom/shadowroot.rs @@ -24,6 +24,7 @@ use style::author_styles::AuthorStyles; use style::dom::TElement; use style::media_queries::Device; use style::shared_lock::SharedRwLockReadGuard; +use style::stylesheet_set::StylesheetSet; use style::stylesheets::Stylesheet; // https://dom.spec.whatwg.org/#interface-shadowroot @@ -219,9 +220,9 @@ impl StyleSheetListOwner for Dom { .is_before(sheet_in_shadow.owner.upcast()) }) .cloned(); - self.document_or_shadow_root.add_stylesheet( + DocumentOrShadowRoot::add_stylesheet( owner, - stylesheets, + StylesheetSet::Author(stylesheets), sheet, insertion_point, self.document.style_shared_lock(), @@ -231,10 +232,10 @@ impl StyleSheetListOwner for Dom { /// 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) { - self.document_or_shadow_root.remove_stylesheet( + DocumentOrShadowRoot::remove_stylesheet( owner, s, - &mut self.author_styles.borrow_mut().stylesheets, + StylesheetSet::Author(&mut self.author_styles.borrow_mut().stylesheets), ) } diff --git a/components/style/stylesheet_set.rs b/components/style/stylesheet_set.rs index 9a53339f5f9..4c3e37d3cd8 100644 --- a/components/style/stylesheet_set.rs +++ b/components/style/stylesheet_set.rs @@ -374,36 +374,65 @@ where } /// Functionality common to DocumentStylesheetSet and AuthorStylesheetSet. -pub trait StylesheetSet +pub enum StylesheetSet<'a, S> +where + S: StylesheetInDocument + PartialEq + 'static, +{ + /// Author stylesheet set. + Author(&'a mut AuthorStylesheetSet), + /// Document stylesheet set. + Document(&'a mut DocumentStylesheetSet), +} + +impl<'a, S> StylesheetSet<'a, S> where S: StylesheetInDocument + PartialEq + 'static, { /// Appends a new stylesheet to the current set. /// /// No device implies not computing invalidations. - fn append_stylesheet( + pub fn append_stylesheet( &mut self, device: Option<&Device>, sheet: S, guard: &SharedRwLockReadGuard, - ); + ) { + match self { + StylesheetSet::Author(set) => set.append_stylesheet(device, sheet, guard), + StylesheetSet::Document(set) => set.append_stylesheet(device, sheet, guard), + } + } /// Insert a given stylesheet before another stylesheet in the document. - fn insert_stylesheet_before( + pub fn insert_stylesheet_before( &mut self, device: Option<&Device>, sheet: S, before_sheet: S, guard: &SharedRwLockReadGuard, - ); + ) { + match self { + StylesheetSet::Author(set) => { + set.insert_stylesheet_before(device, sheet, before_sheet, guard) + }, + StylesheetSet::Document(set) => { + set.insert_stylesheet_before(device, sheet, before_sheet, guard) + }, + } + } /// Remove a given stylesheet from the set. - fn remove_stylesheet( + pub fn remove_stylesheet( &mut self, device: Option<&Device>, sheet: S, guard: &SharedRwLockReadGuard, - ); + ) { + match self { + StylesheetSet::Author(set) => set.remove_stylesheet(device, sheet, guard), + StylesheetSet::Document(set) => set.remove_stylesheet(device, sheet, guard), + } + } } /// This macro defines methods common to DocumentStylesheetSet and @@ -429,16 +458,11 @@ macro_rules! stylesheetset_impl { .collect_invalidations_for(device, sheet, guard); } } - } - impl StylesheetSet for $set_type - where - S: StylesheetInDocument + PartialEq + 'static, - { /// Appends a new stylesheet to the current set. /// /// No device implies not computing invalidations. - fn append_stylesheet( + pub fn append_stylesheet( &mut self, device: Option<&Device>, sheet: S, @@ -451,7 +475,7 @@ macro_rules! stylesheetset_impl { } /// Insert a given stylesheet before another stylesheet in the document. - fn insert_stylesheet_before( + pub fn insert_stylesheet_before( &mut self, device: Option<&Device>, sheet: S, @@ -466,7 +490,7 @@ macro_rules! stylesheetset_impl { } /// Remove a given stylesheet from the set. - fn remove_stylesheet( + pub fn remove_stylesheet( &mut self, device: Option<&Device>, sheet: S,