Make StylesheetSet an enum instead of a trait object

This commit is contained in:
Fernando Jiménez Moreno 2019-03-01 11:43:45 +01:00
parent 3e53962b25
commit 8b353ee3ce
4 changed files with 51 additions and 28 deletions

View file

@ -155,7 +155,7 @@ use style::media_queries::{Device, MediaType};
use style::selector_parser::{RestyleDamage, Snapshot}; use style::selector_parser::{RestyleDamage, Snapshot};
use style::shared_lock::SharedRwLock as StyleSharedRwLock; use style::shared_lock::SharedRwLock as StyleSharedRwLock;
use style::str::{split_html_space_chars, str_join}; 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 style::stylesheets::{Origin, OriginSet, Stylesheet};
use url::percent_encoding::percent_decode; use url::percent_encoding::percent_decode;
use url::Host; use url::Host;
@ -4643,9 +4643,9 @@ impl StyleSheetListOwner for Dom<Document> {
)) ))
.unwrap(); .unwrap();
self.document_or_shadow_root.add_stylesheet( DocumentOrShadowRoot::add_stylesheet(
owner, owner,
stylesheets, StylesheetSet::Document(stylesheets),
sheet, sheet,
insertion_point, insertion_point,
self.style_shared_lock(), self.style_shared_lock(),
@ -4660,10 +4660,10 @@ impl StyleSheetListOwner for Dom<Document> {
.send(Msg::RemoveStylesheet(s.clone())) .send(Msg::RemoveStylesheet(s.clone()))
.unwrap(); .unwrap();
self.document_or_shadow_root.remove_stylesheet( DocumentOrShadowRoot::remove_stylesheet(
owner, owner,
s, s,
&mut *self.stylesheets.borrow_mut(), StylesheetSet::Document(&mut *self.stylesheets.borrow_mut()),
) )
} }

View file

@ -211,10 +211,9 @@ impl DocumentOrShadowRoot {
/// Remove a stylesheet owned by `owner` from the list of document sheets. /// Remove a stylesheet owned by `owner` from the list of document sheets.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
pub fn remove_stylesheet( pub fn remove_stylesheet(
&self,
owner: &Element, owner: &Element,
s: &Arc<Stylesheet>, s: &Arc<Stylesheet>,
stylesheets: &mut StylesheetSet<StyleSheetInDocument>, mut stylesheets: StylesheetSet<StyleSheetInDocument>,
) { ) {
let guard = s.shared_lock.read(); let guard = s.shared_lock.read();
@ -233,9 +232,8 @@ impl DocumentOrShadowRoot {
/// correct tree position. /// correct tree position.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
pub fn add_stylesheet( pub fn add_stylesheet(
&self,
owner: &Element, owner: &Element,
stylesheets: &mut StylesheetSet<StyleSheetInDocument>, mut stylesheets: StylesheetSet<StyleSheetInDocument>,
sheet: Arc<Stylesheet>, sheet: Arc<Stylesheet>,
insertion_point: Option<StyleSheetInDocument>, insertion_point: Option<StyleSheetInDocument>,
style_shared_lock: &StyleSharedRwLock, style_shared_lock: &StyleSharedRwLock,

View file

@ -24,6 +24,7 @@ use style::author_styles::AuthorStyles;
use style::dom::TElement; use style::dom::TElement;
use style::media_queries::Device; use style::media_queries::Device;
use style::shared_lock::SharedRwLockReadGuard; use style::shared_lock::SharedRwLockReadGuard;
use style::stylesheet_set::StylesheetSet;
use style::stylesheets::Stylesheet; use style::stylesheets::Stylesheet;
// https://dom.spec.whatwg.org/#interface-shadowroot // https://dom.spec.whatwg.org/#interface-shadowroot
@ -219,9 +220,9 @@ impl StyleSheetListOwner for Dom<ShadowRoot> {
.is_before(sheet_in_shadow.owner.upcast()) .is_before(sheet_in_shadow.owner.upcast())
}) })
.cloned(); .cloned();
self.document_or_shadow_root.add_stylesheet( DocumentOrShadowRoot::add_stylesheet(
owner, owner,
stylesheets, StylesheetSet::Author(stylesheets),
sheet, sheet,
insertion_point, insertion_point,
self.document.style_shared_lock(), self.document.style_shared_lock(),
@ -231,10 +232,10 @@ impl StyleSheetListOwner for Dom<ShadowRoot> {
/// Remove a stylesheet owned by `owner` from the list of shadow root sheets. /// Remove a stylesheet owned by `owner` from the list of shadow root sheets.
#[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily.
fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) { fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
self.document_or_shadow_root.remove_stylesheet( DocumentOrShadowRoot::remove_stylesheet(
owner, owner,
s, s,
&mut self.author_styles.borrow_mut().stylesheets, StylesheetSet::Author(&mut self.author_styles.borrow_mut().stylesheets),
) )
} }

View file

@ -374,36 +374,65 @@ where
} }
/// Functionality common to DocumentStylesheetSet and AuthorStylesheetSet. /// Functionality common to DocumentStylesheetSet and AuthorStylesheetSet.
pub trait StylesheetSet<S> pub enum StylesheetSet<'a, S>
where
S: StylesheetInDocument + PartialEq + 'static,
{
/// Author stylesheet set.
Author(&'a mut AuthorStylesheetSet<S>),
/// Document stylesheet set.
Document(&'a mut DocumentStylesheetSet<S>),
}
impl<'a, S> StylesheetSet<'a, S>
where where
S: StylesheetInDocument + PartialEq + 'static, S: StylesheetInDocument + PartialEq + 'static,
{ {
/// Appends a new stylesheet to the current set. /// Appends a new stylesheet to the current set.
/// ///
/// No device implies not computing invalidations. /// No device implies not computing invalidations.
fn append_stylesheet( pub fn append_stylesheet(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,
guard: &SharedRwLockReadGuard, 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. /// Insert a given stylesheet before another stylesheet in the document.
fn insert_stylesheet_before( pub fn insert_stylesheet_before(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,
before_sheet: S, before_sheet: S,
guard: &SharedRwLockReadGuard, 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. /// Remove a given stylesheet from the set.
fn remove_stylesheet( pub fn remove_stylesheet(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,
guard: &SharedRwLockReadGuard, 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 /// This macro defines methods common to DocumentStylesheetSet and
@ -429,16 +458,11 @@ macro_rules! stylesheetset_impl {
.collect_invalidations_for(device, sheet, guard); .collect_invalidations_for(device, sheet, guard);
} }
} }
}
impl<S> StylesheetSet<S> for $set_type
where
S: StylesheetInDocument + PartialEq + 'static,
{
/// Appends a new stylesheet to the current set. /// Appends a new stylesheet to the current set.
/// ///
/// No device implies not computing invalidations. /// No device implies not computing invalidations.
fn append_stylesheet( pub fn append_stylesheet(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,
@ -451,7 +475,7 @@ macro_rules! stylesheetset_impl {
} }
/// Insert a given stylesheet before another stylesheet in the document. /// Insert a given stylesheet before another stylesheet in the document.
fn insert_stylesheet_before( pub fn insert_stylesheet_before(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,
@ -466,7 +490,7 @@ macro_rules! stylesheetset_impl {
} }
/// Remove a given stylesheet from the set. /// Remove a given stylesheet from the set.
fn remove_stylesheet( pub fn remove_stylesheet(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
sheet: S, sheet: S,