From f173504ded726ddecf7a92f1864ba6e9662d9eae Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Mon, 5 Oct 2015 15:49:26 +0200 Subject: [PATCH] Make UA and user stylesheets static and shared by all Stylist instances --- components/layout/layout_task.rs | 6 +-- components/style/selector_matching.rs | 57 +++++++++++++++------------ 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/components/layout/layout_task.rs b/components/layout/layout_task.rs index d4931848f05f..cb3fe04eb571 100644 --- a/components/layout/layout_task.rs +++ b/components/layout/layout_task.rs @@ -75,7 +75,7 @@ use style::computed_values::{self, filter, mix_blend_mode}; use style::media_queries::{Device, MediaQueryList, MediaType}; use style::properties::longhands::{display, position}; use style::properties::style_structs; -use style::selector_matching::Stylist; +use style::selector_matching::{Stylist, USER_OR_USER_AGENT_STYLESHEETS}; use style::stylesheets::{CSSRule, CSSRuleIteratorExt, Origin, Stylesheet}; use style::values::AuExtensionMethods; use style::viewport::ViewportRule; @@ -377,8 +377,8 @@ impl LayoutTask { let stylist = box Stylist::new(device); let outstanding_web_fonts_counter = Arc::new(AtomicUsize::new(0)); - for user_or_user_agent_stylesheet in stylist.stylesheets() { - add_font_face_rules(user_or_user_agent_stylesheet, + for stylesheet in &*USER_OR_USER_AGENT_STYLESHEETS { + add_font_face_rules(stylesheet, &stylist.device, &font_cache_task, &font_cache_sender, diff --git a/components/style/selector_matching.rs b/components/style/selector_matching.rs index 53f1a0d35895..4ceedb1f51e2 100644 --- a/components/style/selector_matching.rs +++ b/components/style/selector_matching.rs @@ -26,6 +26,36 @@ use viewport::{MaybeNew, ViewportRuleCascade}; pub type DeclarationBlock = GenericDeclarationBlock>; +lazy_static! { + pub static ref USER_OR_USER_AGENT_STYLESHEETS: Vec = { + let mut stylesheets = vec!(); + // FIXME: presentational-hints.css should be at author origin with zero specificity. + // (Does it make a difference?) + for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] { + match read_resource_file(&[filename]) { + Ok(res) => { + let ua_stylesheet = Stylesheet::from_bytes( + &res, + Url::parse(&format!("chrome:///{:?}", filename)).unwrap(), + None, + None, + Origin::UserAgent); + stylesheets.push(ua_stylesheet); + } + Err(..) => { + error!("Failed to load UA stylesheet {}!", filename); + process::exit(1); + } + } + } + for &(ref contents, ref url) in &opts::get().user_stylesheets { + stylesheets.push(Stylesheet::from_bytes( + &contents, url.clone(), None, None, Origin::User)); + } + stylesheets + }; +} + pub struct Stylist { // List of stylesheets (including all media rules) stylesheets: Vec, @@ -51,7 +81,7 @@ pub struct Stylist { impl Stylist { #[inline] pub fn new(device: Device) -> Stylist { - let mut stylist = Stylist { + let stylist = Stylist { stylesheets: vec!(), device: device, is_dirty: true, @@ -63,29 +93,6 @@ impl Stylist { state_deps: StateDependencySet::new(), }; // FIXME: Add iso-8859-9.css when the document’s encoding is ISO-8859-8. - // FIXME: presentational-hints.css should be at author origin with zero specificity. - // (Does it make a difference?) - for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] { - match read_resource_file(&[filename]) { - Ok(res) => { - let ua_stylesheet = Stylesheet::from_bytes( - &res, - Url::parse(&format!("chrome:///{:?}", filename)).unwrap(), - None, - None, - Origin::UserAgent); - stylist.add_stylesheet(ua_stylesheet); - } - Err(..) => { - error!("Stylist::new() failed at loading {}!", filename); - process::exit(1); - } - } - } - for &(ref contents, ref url) in &opts::get().user_stylesheets { - stylist.add_stylesheet(Stylesheet::from_bytes( - &contents, url.clone(), None, None, Origin::User)); - } stylist } @@ -110,7 +117,7 @@ impl Stylist { self.rules_source_order = 0; self.state_deps.clear(); - for stylesheet in &self.stylesheets { + for stylesheet in USER_OR_USER_AGENT_STYLESHEETS.iter().chain(&self.stylesheets) { let (mut element_map, mut before_map, mut after_map) = match stylesheet.origin { Origin::UserAgent => ( &mut self.element_map.user_agent,