Make StyleSheetListOwner an enum instead of a trait object

This commit is contained in:
Fernando Jiménez Moreno 2019-03-01 18:10:18 +01:00
parent 8b353ee3ce
commit 2350f0e3d1
7 changed files with 177 additions and 134 deletions

View file

@ -5,33 +5,76 @@
use crate::dom::bindings::codegen::Bindings::StyleSheetListBinding;
use crate::dom::bindings::codegen::Bindings::StyleSheetListBinding::StyleSheetListMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::trace::JSTraceable;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::shadowroot::ShadowRoot;
use crate::dom::stylesheet::StyleSheet;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use servo_arc::Arc;
use style::stylesheets::Stylesheet;
pub trait StyleSheetListOwner: JSTraceable {
fn stylesheet_count(&self) -> usize;
fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>>;
fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>);
fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>);
fn invalidate_stylesheets(&self);
#[must_root]
#[derive(JSTraceable, MallocSizeOf)]
pub enum StyleSheetListOwner {
Document(Dom<Document>),
ShadowRoot(Dom<ShadowRoot>),
}
impl StyleSheetListOwner {
pub fn stylesheet_count(&self) -> usize {
match *self {
StyleSheetListOwner::Document(ref doc) => doc.stylesheet_count(),
StyleSheetListOwner::ShadowRoot(ref shadow_root) => shadow_root.stylesheet_count(),
}
}
pub fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
match *self {
StyleSheetListOwner::Document(ref doc) => doc.stylesheet_at(index),
StyleSheetListOwner::ShadowRoot(ref shadow_root) => shadow_root.stylesheet_at(index),
}
}
pub fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>) {
match *self {
StyleSheetListOwner::Document(ref doc) => doc.add_stylesheet(owner, sheet),
StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
shadow_root.add_stylesheet(owner, sheet)
},
}
}
pub fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
match *self {
StyleSheetListOwner::Document(ref doc) => doc.remove_stylesheet(owner, s),
StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
shadow_root.remove_stylesheet(owner, s)
},
}
}
pub fn invalidate_stylesheets(&self) {
match *self {
StyleSheetListOwner::Document(ref doc) => doc.invalidate_stylesheets(),
StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
shadow_root.invalidate_stylesheets()
},
}
}
}
#[dom_struct]
pub struct StyleSheetList {
reflector_: Reflector,
#[ignore_malloc_size_of = "trait object"]
document_or_shadow_root: Box<StyleSheetListOwner>,
document_or_shadow_root: StyleSheetListOwner,
}
impl StyleSheetList {
fn new_inherited(doc_or_sr: Box<StyleSheetListOwner>) -> StyleSheetList {
#[allow(unrooted_must_root)]
fn new_inherited(doc_or_sr: StyleSheetListOwner) -> StyleSheetList {
StyleSheetList {
reflector_: Reflector::new(),
document_or_shadow_root: doc_or_sr,
@ -39,7 +82,7 @@ impl StyleSheetList {
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, doc_or_sr: Box<StyleSheetListOwner>) -> DomRoot<StyleSheetList> {
pub fn new(window: &Window, doc_or_sr: StyleSheetListOwner) -> DomRoot<StyleSheetList> {
reflect_dom_object(
Box::new(StyleSheetList::new_inherited(doc_or_sr)),
window,