Auto merge of #8682 - jdm:css-error-reporter, r=jdm

Defined new trait ParseErrorReporter and added error_reporter member …

…to ParserContext.

Rebase of #8210.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8682)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-11-27 03:56:08 +05:30
commit f5ef2f4f75
24 changed files with 194 additions and 69 deletions

View file

@ -3,16 +3,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use app_units::Au;
use cssparser::{Parser, SourcePosition};
use euclid::size::Size2D;
use std::borrow::ToOwned;
use style::media_queries::*;
use style::stylesheets::{Origin, Stylesheet, CSSRuleIteratorExt};
use style::values::specified;
use style_traits::ParseErrorReporter;
pub struct CSSErrorReporterTest;
impl ParseErrorReporter for CSSErrorReporterTest {
fn report_error(&self, _input: &mut Parser, _position: SourcePosition, _message: &str) {
}
fn clone(&self) -> Box<ParseErrorReporter + Send + Sync> {
let error_reporter = Box::new(CSSErrorReporterTest);
return error_reporter;
}
}
fn test_media_rule<F>(css: &str, callback: F) where F: Fn(&MediaQueryList, &str) {
let url = url!("http://localhost");
let stylesheet = Stylesheet::from_str(css, url, Origin::Author);
let stylesheet = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest));
let mut rule_count = 0;
for rule in stylesheet.rules().media() {
rule_count += 1;
@ -23,7 +35,7 @@ fn test_media_rule<F>(css: &str, callback: F) where F: Fn(&MediaQueryList, &str)
fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) {
let url = url!("http://localhost");
let ss = Stylesheet::from_str(css, url, Origin::Author);
let ss = Stylesheet::from_str(css, url, Origin::Author, Box::new(CSSErrorReporterTest));
let rule_count = ss.effective_rules(device).style().count();
assert!(rule_count == expected_rule_count, css.to_owned());
}

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser;
use media_queries::CSSErrorReporterTest;
use selectors::parser::*;
use std::borrow::ToOwned;
use std::sync::Arc;
@ -10,7 +11,6 @@ use string_cache::Atom;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands};
use style::stylesheets::{CSSRule, StyleRule, Origin, Stylesheet};
#[test]
fn test_parse_stylesheet() {
let css = r"
@ -21,7 +21,7 @@ fn test_parse_stylesheet() {
#d1 > .ok { background: blue; }
";
let url = url!("about::test");
let stylesheet = Stylesheet::from_str(css, url, Origin::UserAgent);
let stylesheet = Stylesheet::from_str(css, url, Origin::UserAgent, Box::new(CSSErrorReporterTest));
assert_eq!(stylesheet, Stylesheet {
origin: Origin::UserAgent,
media: None,

View file

@ -5,6 +5,7 @@
use cssparser::Parser;
use euclid::scale_factor::ScaleFactor;
use euclid::size::Size2D;
use media_queries::CSSErrorReporterTest;
use style::media_queries::{Device, MediaType};
use style::parser::ParserContext;
use style::stylesheets::{Origin, Stylesheet, CSSRuleIteratorExt};
@ -12,11 +13,12 @@ use style::values::specified::Length::{self, ViewportPercentage};
use style::values::specified::LengthOrPercentageOrAuto::{self, Auto};
use style::values::specified::ViewportPercentageLength::Vw;
use style::viewport::*;
use style_traits::ParseErrorReporter;
use style_traits::viewport::*;
macro_rules! stylesheet {
($css:expr, $origin:ident) => {
Stylesheet::from_str($css, url!("http://localhost"), Origin::$origin);
($css:expr, $origin:ident, $error_reporter:expr) => {
Stylesheet::from_str($css, url!("http://localhost"), Origin::$origin, $error_reporter);
}
}
@ -27,8 +29,7 @@ fn test_viewport_rule<F>(css: &str,
{
::util::prefs::set_pref("layout.viewport.enabled",
::util::prefs::PrefValue::Boolean(true));
let stylesheet = stylesheet!(css, Author);
let stylesheet = stylesheet!(css, Author, Box::new(CSSErrorReporterTest));
let mut rule_count = 0;
for rule in stylesheet.effective_rules(&device).viewport() {
rule_count += 1;
@ -244,11 +245,11 @@ fn multiple_stylesheets_cascading() {
::util::prefs::set_pref("layout.viewport.enabled",
::util::prefs::PrefValue::Boolean(true));
let device = Device::new(MediaType::Screen, Size2D::typed(800., 600.));
let error_reporter = CSSErrorReporterTest;
let stylesheets = vec![
stylesheet!("@viewport { min-width: 100px; min-height: 100px; zoom: 1; }", UserAgent),
stylesheet!("@viewport { min-width: 200px; min-height: 200px; }", User),
stylesheet!("@viewport { min-width: 300px; }", Author)];
stylesheet!("@viewport { min-width: 100px; min-height: 100px; zoom: 1; }", UserAgent, error_reporter.clone()),
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())
@ -260,11 +261,11 @@ fn multiple_stylesheets_cascading() {
assert_decl_eq!(&declarations[2], Author, MinWidth: viewport_length!(300., px));
let stylesheets = vec![
stylesheet!("@viewport { min-width: 100px !important; }", UserAgent),
stylesheet!("@viewport { min-width: 200px !important; min-height: 200px !important; }", User),
stylesheet!(
"@viewport { min-width: 300px !important; min-height: 300px !important; zoom: 3 !important; }", Author)];
stylesheet!("@viewport { min-width: 100px !important; }", UserAgent, error_reporter.clone()),
stylesheet!("@viewport { min-width: 200px !important; min-height: 200px !important; }",
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()
@ -278,7 +279,7 @@ fn multiple_stylesheets_cascading() {
#[test]
fn constrain_viewport() {
let url = url!("http://localhost");
let context = ParserContext::new(Origin::Author, &url);
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
macro_rules! from_css {
($css:expr) => {