Add RwLock in more Arc’d things in stylesheets.

This commit is contained in:
Simon Sapin 2016-10-19 19:08:14 +02:00
parent 9e3cf3189b
commit 742681a623
9 changed files with 250 additions and 334 deletions

View file

@ -9,7 +9,7 @@ use std::borrow::ToOwned;
use style::error_reporting::ParseErrorReporter;
use style::media_queries::*;
use style::parser::ParserContextExtraData;
use style::stylesheets::{Stylesheet, Origin, CSSRuleIteratorExt};
use style::stylesheets::{Stylesheet, Origin, CSSRule};
use style::values::specified;
use url::Url;
@ -28,18 +28,30 @@ fn test_media_rule<F>(css: &str, callback: F) where F: Fn(&MediaQueryList, &str)
let stylesheet = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest),
ParserContextExtraData::default());
let mut rule_count = 0;
for rule in stylesheet.rules().media() {
media_queries(&stylesheet.rules, &mut |mq| {
rule_count += 1;
callback(&rule.media_queries, css);
}
callback(mq, css);
});
assert!(rule_count > 0);
}
fn media_queries<F>(rules: &[CSSRule], f: &mut F) where F: FnMut(&MediaQueryList) {
for rule in rules {
rule.with_nested_rules_and_mq(|rules, mq| {
if let Some(mq) = mq {
f(mq)
}
media_queries(rules, f)
})
}
}
fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) {
let url = Url::parse("http://localhost").unwrap();
let ss = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest),
ParserContextExtraData::default());
let rule_count = ss.effective_rules(device).style().count();
let mut rule_count = 0;
ss.effective_style_rules(device, |_| rule_count += 1);
assert!(rule_count == expected_rule_count, css.to_owned());
}

View file

@ -56,11 +56,11 @@ fn test_parse_stylesheet() {
media: None,
dirty_on_viewport_size_change: false,
rules: vec![
CSSRule::Namespace(Arc::new(NamespaceRule {
CSSRule::Namespace(Arc::new(RwLock::new(NamespaceRule {
prefix: None,
url: NsAtom(Atom::from("http://www.w3.org/1999/xhtml"))
})),
CSSRule::Style(Arc::new(StyleRule {
}))),
CSSRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
@ -98,8 +98,8 @@ fn test_parse_stylesheet() {
],
important_count: 2,
})),
})),
CSSRule::Style(Arc::new(StyleRule {
}))),
CSSRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
@ -144,8 +144,8 @@ fn test_parse_stylesheet() {
],
important_count: 0,
})),
})),
CSSRule::Style(Arc::new(StyleRule {
}))),
CSSRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
@ -220,11 +220,11 @@ fn test_parse_stylesheet() {
],
important_count: 0,
})),
})),
CSSRule::Keyframes(Arc::new(KeyframesRule {
}))),
CSSRule::Keyframes(Arc::new(RwLock::new(KeyframesRule {
name: "foo".into(),
keyframes: vec![
Arc::new(Keyframe {
Arc::new(RwLock::new(Keyframe {
selector: KeyframeSelector::new_for_unit_testing(
vec![KeyframePercentage::new(0.)]),
block: Arc::new(RwLock::new(PropertyDeclarationBlock {
@ -235,8 +235,8 @@ fn test_parse_stylesheet() {
],
important_count: 0,
}))
}),
Arc::new(Keyframe {
})),
Arc::new(RwLock::new(Keyframe {
selector: KeyframeSelector::new_for_unit_testing(
vec![KeyframePercentage::new(1.)]),
block: Arc::new(RwLock::new(PropertyDeclarationBlock {
@ -251,9 +251,9 @@ fn test_parse_stylesheet() {
],
important_count: 0,
})),
}),
})),
]
}))
})))
],
};

View file

@ -9,7 +9,7 @@ use media_queries::CSSErrorReporterTest;
use style::error_reporting::ParseErrorReporter;
use style::media_queries::{Device, MediaType};
use style::parser::{ParserContext, ParserContextExtraData};
use style::stylesheets::{Stylesheet, Origin, CSSRuleIteratorExt};
use style::stylesheets::{Stylesheet, Origin};
use style::values::specified::Length::{self, ViewportPercentage};
use style::values::specified::LengthOrPercentageOrAuto::{self, Auto};
use style::values::specified::ViewportPercentageLength::Vw;
@ -19,8 +19,13 @@ use url::Url;
macro_rules! stylesheet {
($css:expr, $origin:ident, $error_reporter:expr) => {
Stylesheet::from_str($css, Url::parse("http://localhost").unwrap(), Origin::$origin, $error_reporter,
ParserContextExtraData::default());
Box::new(Stylesheet::from_str(
$css,
Url::parse("http://localhost").unwrap(),
Origin::$origin,
$error_reporter,
ParserContextExtraData::default()
))
}
}
@ -33,10 +38,10 @@ fn test_viewport_rule<F>(css: &str,
::util::prefs::PrefValue::Boolean(true));
let stylesheet = stylesheet!(css, Author, Box::new(CSSErrorReporterTest));
let mut rule_count = 0;
for rule in stylesheet.effective_rules(&device).viewport() {
stylesheet.effective_viewport_rules(&device, |rule| {
rule_count += 1;
callback(&rule.declarations, css);
}
});
assert!(rule_count > 0);
}
@ -253,10 +258,7 @@ fn multiple_stylesheets_cascading() {
stylesheet!("@viewport { min-width: 200px; min-height: 200px; }", User, error_reporter.clone()),
stylesheet!("@viewport { min-width: 300px; }", Author, error_reporter.clone())];
let declarations = stylesheets.iter()
.flat_map(|s| s.effective_rules(&device).viewport())
.cascade()
.declarations;
let declarations = Cascade::from_stylesheets(&stylesheets, &device).finish();
assert_decl_len!(declarations == 3);
assert_decl_eq!(&declarations[0], UserAgent, Zoom: Zoom::Number(1.));
assert_decl_eq!(&declarations[1], User, MinHeight: viewport_length!(200., px));
@ -268,10 +270,7 @@ fn multiple_stylesheets_cascading() {
User, error_reporter.clone()),
stylesheet!("@viewport { min-width: 300px !important; min-height: 300px !important; zoom: 3 !important; }",
Author, error_reporter.clone())];
let declarations = stylesheets.iter()
.flat_map(|s| s.effective_rules(&device).viewport())
.cascade()
.declarations;
let declarations = Cascade::from_stylesheets(&stylesheets, &device).finish();
assert_decl_len!(declarations == 3);
assert_decl_eq!(&declarations[0], UserAgent, MinWidth: viewport_length!(100., px), !important);
assert_decl_eq!(&declarations[1], User, MinHeight: viewport_length!(200., px), !important);