aboutsummaryrefslogtreecommitdiffstats
path: root/components/style/stylesheet_set.rs
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2018-02-08 15:56:26 +0100
committerEmilio Cobos Álvarez <emilio@crisal.io>2018-02-09 19:45:02 +0100
commit5e8dd8a2d4011e0767159d70f6f9858eec18dbe4 (patch)
tree45bbf2ca556c6668302ae7bd954dae86272fa152 /components/style/stylesheet_set.rs
parenta254dc12a48f7b9fb154057111feaa5b93434e11 (diff)
downloadservo-5e8dd8a2d4011e0767159d70f6f9858eec18dbe4.tar.gz
servo-5e8dd8a2d4011e0767159d70f6f9858eec18dbe4.zip
style: Move the stylesheet set methods under a macro to reuse it soon.
MozReview-Commit-ID: 50Srw4Mjw18
Diffstat (limited to 'components/style/stylesheet_set.rs')
-rw-r--r--components/style/stylesheet_set.rs159
1 files changed, 89 insertions, 70 deletions
diff --git a/components/style/stylesheet_set.rs b/components/style/stylesheet_set.rs
index eeb75e738e6..fcaebe5b7c6 100644
--- a/components/style/stylesheet_set.rs
+++ b/components/style/stylesheet_set.rs
@@ -394,6 +394,85 @@ where
invalidations: StylesheetInvalidationSet,
}
+/// This macro defines methods common to DocumentStylesheetSet and
+/// AuthorStylesheetSet.
+///
+/// We could simplify the setup moving invalidations to SheetCollection, but
+/// that would imply not sharing invalidations across origins of the same
+/// documents, which is slightly annoying.
+macro_rules! sheet_set_methods {
+ ($set_name:expr) => {
+ fn collect_invalidations_for(
+ &mut self,
+ device: Option<&Device>,
+ sheet: &S,
+ guard: &SharedRwLockReadGuard,
+ ) {
+ if let Some(device) = device {
+ self.invalidations.collect_invalidations_for(device, sheet, guard);
+ }
+ }
+
+ /// Appends a new stylesheet to the current set.
+ ///
+ /// No device implies not computing invalidations.
+ pub fn append_stylesheet(
+ &mut self,
+ device: Option<&Device>,
+ sheet: S,
+ guard: &SharedRwLockReadGuard
+ ) {
+ debug!(concat!($set_name, "::append_stylesheet"));
+ self.collect_invalidations_for(device, &sheet, guard);
+ let collection = self.collection_for(&sheet, guard);
+ collection.append(sheet);
+ }
+
+ /// Prepend a new stylesheet to the current set.
+ pub fn prepend_stylesheet(
+ &mut self,
+ device: Option<&Device>,
+ sheet: S,
+ guard: &SharedRwLockReadGuard
+ ) {
+ debug!(concat!($set_name, "::prepend_stylesheet"));
+ self.collect_invalidations_for(device, &sheet, guard);
+
+ let collection = self.collection_for(&sheet, guard);
+ collection.prepend(sheet);
+ }
+
+ /// Insert a given stylesheet before another stylesheet in the document.
+ pub fn insert_stylesheet_before(
+ &mut self,
+ device: Option<&Device>,
+ sheet: S,
+ before_sheet: S,
+ guard: &SharedRwLockReadGuard,
+ ) {
+ debug!(concat!($set_name, "::insert_stylesheet_before"));
+ self.collect_invalidations_for(device, &sheet, guard);
+
+ let collection = self.collection_for(&sheet, guard);
+ collection.insert_before(sheet, &before_sheet);
+ }
+
+ /// Remove a given stylesheet from the set.
+ pub fn remove_stylesheet(
+ &mut self,
+ device: Option<&Device>,
+ sheet: S,
+ guard: &SharedRwLockReadGuard,
+ ) {
+ debug!(concat!($set_name, "::remove_stylesheet"));
+ self.collect_invalidations_for(device, &sheet, guard);
+
+ let collection = self.collection_for(&sheet, guard);
+ collection.remove(&sheet)
+ }
+ }
+}
+
impl<S> DocumentStylesheetSet<S>
where
S: StylesheetInDocument + PartialEq + 'static,
@@ -406,85 +485,25 @@ where
}
}
- /// Returns the number of stylesheets in the set.
- pub fn len(&self) -> usize {
- self.collections.iter_origins().fold(0, |s, (item, _)| s + item.len())
- }
-
- /// Returns the `index`th stylesheet in the set for the given origin.
- pub fn get(&self, origin: Origin, index: usize) -> Option<&S> {
- self.collections.borrow_for_origin(&origin).get(index)
- }
-
- fn collect_invalidations_for(
+ fn collection_for(
&mut self,
- device: Option<&Device>,
sheet: &S,
guard: &SharedRwLockReadGuard,
- ) {
- if let Some(device) = device {
- self.invalidations.collect_invalidations_for(device, sheet, guard);
- }
- }
-
- /// Appends a new stylesheet to the current set.
- ///
- /// No device implies not computing invalidations.
- pub fn append_stylesheet(
- &mut self,
- device: Option<&Device>,
- sheet: S,
- guard: &SharedRwLockReadGuard
- ) {
- debug!("DocumentStylesheetSet::append_stylesheet");
- self.collect_invalidations_for(device, &sheet, guard);
- let origin = sheet.contents(guard).origin;
- self.collections.borrow_mut_for_origin(&origin).append(sheet);
- }
-
- /// Prepend a new stylesheet to the current set.
- pub fn prepend_stylesheet(
- &mut self,
- device: Option<&Device>,
- sheet: S,
- guard: &SharedRwLockReadGuard
- ) {
- debug!("DocumentStylesheetSet::prepend_stylesheet");
- self.collect_invalidations_for(device, &sheet, guard);
-
+ ) -> &mut SheetCollection<S> {
let origin = sheet.contents(guard).origin;
- self.collections.borrow_mut_for_origin(&origin).prepend(sheet)
+ self.collections.borrow_mut_for_origin(&origin)
}
- /// Insert a given stylesheet before another stylesheet in the document.
- pub fn insert_stylesheet_before(
- &mut self,
- device: Option<&Device>,
- sheet: S,
- before_sheet: S,
- guard: &SharedRwLockReadGuard,
- ) {
- debug!("DocumentStylesheetSet::insert_stylesheet_before");
- self.collect_invalidations_for(device, &sheet, guard);
+ sheet_set_methods!("DocumentStylesheetSet");
- let origin = sheet.contents(guard).origin;
- self.collections
- .borrow_mut_for_origin(&origin)
- .insert_before(sheet, &before_sheet)
+ /// Returns the number of stylesheets in the set.
+ pub fn len(&self) -> usize {
+ self.collections.iter_origins().fold(0, |s, (item, _)| s + item.len())
}
- /// Remove a given stylesheet from the set.
- pub fn remove_stylesheet(
- &mut self,
- device: Option<&Device>,
- sheet: S,
- guard: &SharedRwLockReadGuard,
- ) {
- debug!("StylesheetSet::remove_stylesheet");
- self.collect_invalidations_for(device, &sheet, guard);
-
- let origin = sheet.contents(guard).origin;
- self.collections.borrow_mut_for_origin(&origin).remove(&sheet)
+ /// Returns the `index`th stylesheet in the set for the given origin.
+ pub fn get(&self, origin: Origin, index: usize) -> Option<&S> {
+ self.collections.borrow_for_origin(&origin).get(index)
}
/// Returns whether the given set has changed from the last flush.