Replace Stylesheet::set_media with a constructor argument

This commit is contained in:
Simon Sapin 2016-11-15 16:43:33 +01:00
parent f4dcc38816
commit 236c575c50
8 changed files with 29 additions and 31 deletions

View file

@ -1531,6 +1531,7 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
None, None,
None, None,
Origin::UserAgent, Origin::UserAgent,
Default::default(),
Box::new(StdoutErrorReporter), Box::new(StdoutErrorReporter),
ParserContextExtraData::default())) ParserContextExtraData::default()))
} }
@ -1543,8 +1544,8 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
} }
for &(ref contents, ref url) in &opts::get().user_stylesheets { for &(ref contents, ref url) in &opts::get().user_stylesheets {
user_or_user_agent_stylesheets.push(Stylesheet::from_bytes( user_or_user_agent_stylesheets.push(Stylesheet::from_bytes(
&contents, url.clone(), None, None, Origin::User, Box::new(StdoutErrorReporter), &contents, url.clone(), None, None, Origin::User, Default::default(),
ParserContextExtraData::default())); Box::new(StdoutErrorReporter), ParserContextExtraData::default()));
} }
let quirks_mode_stylesheet = try!(parse_ua_stylesheet("quirks-mode.css")); let quirks_mode_stylesheet = try!(parse_ua_stylesheet("quirks-mode.css"));

View file

@ -370,12 +370,10 @@ impl FetchResponseListener for StylesheetContext {
let win = window_from_node(&*elem); let win = window_from_node(&*elem);
let mut sheet = Stylesheet::from_bytes(&data, final_url, protocol_encoding_label, let sheet = Arc::new(Stylesheet::from_bytes(
Some(environment_encoding), Origin::Author, &data, final_url, protocol_encoding_label, Some(environment_encoding),
win.css_error_reporter(), Origin::Author, self.media.take().unwrap(), win.css_error_reporter(),
ParserContextExtraData::default()); ParserContextExtraData::default()));
sheet.set_media(self.media.take().unwrap());
let sheet = Arc::new(sheet);
let win = window_from_node(&*elem); let win = window_from_node(&*elem);
win.layout_chan().send(Msg::AddStylesheet(sheet.clone())).unwrap(); win.layout_chan().send(Msg::AddStylesheet(sheet.clone())).unwrap();

View file

@ -65,10 +65,9 @@ impl HTMLStyleElement {
}; };
let data = node.GetTextContent().expect("Element.textContent must be a string"); let data = node.GetTextContent().expect("Element.textContent must be a string");
let mut sheet = Stylesheet::from_str(&data, url, Origin::Author, win.css_error_reporter(), let mq = parse_media_query_list(&mut CssParser::new(&mq_str));
let sheet = Stylesheet::from_str(&data, url, Origin::Author, mq, win.css_error_reporter(),
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut css_parser = CssParser::new(&mq_str);
sheet.set_media(parse_media_query_list(&mut css_parser));
let sheet = Arc::new(sheet); let sheet = Arc::new(sheet);
win.layout_chan().send(Msg::AddStylesheet(sheet.clone())).unwrap(); win.layout_chan().send(Msg::AddStylesheet(sheet.clone())).unwrap();

View file

@ -174,15 +174,17 @@ impl Stylesheet {
base_url: ServoUrl, base_url: ServoUrl,
protocol_encoding_label: Option<&str>, protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingRef>, environment_encoding: Option<EncodingRef>,
origin: Origin, error_reporter: Box<ParseErrorReporter + Send>, origin: Origin,
media: MediaList,
error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData) extra_data: ParserContextExtraData)
-> Stylesheet { -> Stylesheet {
let (string, _) = decode_stylesheet_bytes( let (string, _) = decode_stylesheet_bytes(
bytes, protocol_encoding_label, environment_encoding); bytes, protocol_encoding_label, environment_encoding);
Stylesheet::from_str(&string, base_url, origin, error_reporter, extra_data) Stylesheet::from_str(&string, base_url, origin, media, error_reporter, extra_data)
} }
pub fn from_str(css: &str, base_url: ServoUrl, origin: Origin, pub fn from_str(css: &str, base_url: ServoUrl, origin: Origin, media: MediaList,
error_reporter: Box<ParseErrorReporter + Send>, error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData) -> Stylesheet { extra_data: ParserContextExtraData) -> Stylesheet {
let rule_parser = TopLevelRuleParser { let rule_parser = TopLevelRuleParser {
@ -212,17 +214,12 @@ impl Stylesheet {
Stylesheet { Stylesheet {
origin: origin, origin: origin,
rules: rules.into(), rules: rules.into(),
media: Arc::new(RwLock::new(Default::default())), media: Arc::new(RwLock::new(media)),
dirty_on_viewport_size_change: dirty_on_viewport_size_change:
input.seen_viewport_percentages(), input.seen_viewport_percentages(),
} }
} }
/// Set the MediaList associated with the style-sheet.
pub fn set_media(&mut self, media: MediaList) {
*self.media.write() = media;
}
/// Returns whether the style-sheet applies for the current device depending /// Returns whether the style-sheet applies for the current device depending
/// on the associated MediaList. /// on the associated MediaList.
/// ///

View file

@ -174,8 +174,8 @@ pub extern "C" fn Servo_StyleSheet_Empty(mode: SheetParsingMode) -> RawServoStyl
SheetParsingMode::eUserSheetFeatures => Origin::User, SheetParsingMode::eUserSheetFeatures => Origin::User,
SheetParsingMode::eAgentSheetFeatures => Origin::UserAgent, SheetParsingMode::eAgentSheetFeatures => Origin::UserAgent,
}; };
let sheet = Arc::new(Stylesheet::from_str("", url, origin, Box::new(StdoutErrorReporter), let sheet = Arc::new(Stylesheet::from_str(
extra_data)); "", url, origin, Default::default(), Box::new(StdoutErrorReporter), extra_data));
unsafe { unsafe {
transmute(sheet) transmute(sheet)
} }
@ -204,8 +204,8 @@ pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(data: *const nsACString,
referrer: Some(GeckoArcURI::new(referrer)), referrer: Some(GeckoArcURI::new(referrer)),
principal: Some(GeckoArcPrincipal::new(principal)), principal: Some(GeckoArcPrincipal::new(principal)),
}}; }};
let sheet = Arc::new(Stylesheet::from_str(input, url, origin, Box::new(StdoutErrorReporter), let sheet = Arc::new(Stylesheet::from_str(
extra_data)); input, url, origin, Default::default(), Box::new(StdoutErrorReporter), extra_data));
unsafe { unsafe {
transmute(sheet) transmute(sheet)
} }

View file

@ -26,8 +26,9 @@ impl ParseErrorReporter for CSSErrorReporterTest {
fn test_media_rule<F>(css: &str, callback: F) where F: Fn(&MediaList, &str) { fn test_media_rule<F>(css: &str, callback: F) where F: Fn(&MediaList, &str) {
let url = ServoUrl::parse("http://localhost").unwrap(); let url = ServoUrl::parse("http://localhost").unwrap();
let stylesheet = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest), let stylesheet = Stylesheet::from_str(
ParserContextExtraData::default()); css, url, Origin::Author, Default::default(),
Box::new(CSSErrorReporterTest), ParserContextExtraData::default());
let mut rule_count = 0; let mut rule_count = 0;
media_queries(&stylesheet.rules.0.read(), &mut |mq| { media_queries(&stylesheet.rules.0.read(), &mut |mq| {
rule_count += 1; rule_count += 1;
@ -49,8 +50,9 @@ fn media_queries<F>(rules: &[CssRule], f: &mut F) where F: FnMut(&MediaList) {
fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) { fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) {
let url = ServoUrl::parse("http://localhost").unwrap(); let url = ServoUrl::parse("http://localhost").unwrap();
let ss = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest), let ss = Stylesheet::from_str(
ParserContextExtraData::default()); css, url, Origin::Author, Default::default(),
Box::new(CSSErrorReporterTest), ParserContextExtraData::default());
let mut rule_count = 0; let mut rule_count = 0;
ss.effective_style_rules(device, |_| rule_count += 1); ss.effective_style_rules(device, |_| rule_count += 1);
assert!(rule_count == expected_rule_count, css.to_owned()); assert!(rule_count == expected_rule_count, css.to_owned());

View file

@ -49,7 +49,7 @@ fn test_parse_stylesheet() {
} }
}"; }";
let url = ServoUrl::parse("about::test").unwrap(); let url = ServoUrl::parse("about::test").unwrap();
let stylesheet = Stylesheet::from_str(css, url, Origin::UserAgent, let stylesheet = Stylesheet::from_str(css, url, Origin::UserAgent, Default::default(),
Box::new(CSSErrorReporterTest), Box::new(CSSErrorReporterTest),
ParserContextExtraData::default()); ParserContextExtraData::default());
let expected = Stylesheet { let expected = Stylesheet {
@ -320,7 +320,7 @@ fn test_report_error_stylesheet() {
let errors = error_reporter.errors.clone(); let errors = error_reporter.errors.clone();
Stylesheet::from_str(css, url, Origin::UserAgent, error_reporter, Stylesheet::from_str(css, url, Origin::UserAgent, Default::default(), error_reporter,
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut errors = errors.lock().unwrap(); let mut errors = errors.lock().unwrap();

View file

@ -23,6 +23,7 @@ macro_rules! stylesheet {
$css, $css,
ServoUrl::parse("http://localhost").unwrap(), ServoUrl::parse("http://localhost").unwrap(),
Origin::$origin, Origin::$origin,
Default::default(),
$error_reporter, $error_reporter,
ParserContextExtraData::default() ParserContextExtraData::default()
)) ))