Thread ParseError return values through CSS parsing.

This commit is contained in:
Josh Matthews 2017-04-28 00:35:22 -04:00
parent 58e39bfffa
commit 27ae1ef2e7
121 changed files with 2133 additions and 1505 deletions

View file

@ -10,7 +10,7 @@ path = "lib.rs"
doctest = false
[dependencies]
cssparser = "0.13.7"
cssparser = "0.14.0"
gfx = {path = "../../../components/gfx"}
ipc-channel = "0.7"
style = {path = "../../../components/style"}

View file

@ -15,7 +15,7 @@ testing = ["style/testing"]
[dependencies]
byteorder = "1.0"
app_units = "0.4.1"
cssparser = "0.13.7"
cssparser = "0.14.0"
euclid = "0.13"
html5ever = "0.17"
parking_lot = "0.3"

View file

@ -8,7 +8,7 @@ use servo_url::ServoUrl;
use std::borrow::ToOwned;
use style::Atom;
use style::context::QuirksMode;
use style::error_reporting::ParseErrorReporter;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
use style::media_queries::*;
use style::servo::media_queries::*;
use style::shared_lock::SharedRwLock;
@ -20,12 +20,12 @@ use style_traits::ToCss;
pub struct CSSErrorReporterTest;
impl ParseErrorReporter for CSSErrorReporterTest {
fn report_error(&self,
_input: &mut Parser,
_position: SourcePosition,
_message: &str,
_url: &ServoUrl,
_line_number_offset: u64) {
fn report_error<'a>(&self,
_input: &mut Parser,
_position: SourcePosition,
_error: ContextualParseError<'a>,
_url: &ServoUrl,
_line_number_offset: u64) {
}
}

View file

@ -143,13 +143,13 @@ fn border_image_shorthand_should_parse_with_just_source() {
#[test]
fn border_image_outset_should_error_on_negative_length() {
let result = parse(border_image_outset::parse, "-1em");
assert_eq!(result, Err(()));
assert!(result.is_err());
}
#[test]
fn border_image_outset_should_error_on_negative_number() {
let result = parse(border_image_outset::parse, "-15");
assert_eq!(result, Err(()));
assert!(result.is_err());
}
#[test]

View file

@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use parsing::parse;
use style::parser::Parse;
use style::values::specified::image::*;
use style_traits::ToCss;

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use media_queries::CSSErrorReporterTest;
use parsing::parse;
use style::context::QuirksMode;
@ -45,7 +45,8 @@ fn test_parsing_modes() {
let context = ParserContext::new(Origin::Author, &url, &reporter,
Some(CssRuleType::Style), PARSING_MODE_ALLOW_UNITLESS_LENGTH,
QuirksMode::NoQuirks);
let mut parser = Parser::new("1");
let mut input = ParserInput::new("1");
let mut parser = Parser::new(&mut input);
let result = Length::parse(&context, &mut parser);
assert!(result.is_ok());
assert_eq!(result.unwrap(), Length::NoCalc(NoCalcLength::Absolute(AbsoluteLength::Px(1.))));

View file

@ -4,7 +4,7 @@
//! Tests for parsing and serialization of values/properties
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use euclid::size::TypedSize2D;
use media_queries::CSSErrorReporterTest;
use style::context::QuirksMode;
@ -14,24 +14,38 @@ use style::parser::{PARSING_MODE_DEFAULT, ParserContext};
use style::properties::{ComputedValues, StyleBuilder};
use style::stylesheets::{CssRuleType, Origin};
use style::values::computed::{Context, ToComputedValue};
use style_traits::ToCss;
use style_traits::{ToCss, ParseError};
fn parse<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
fn parse<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>>
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>> {
let mut input = ParserInput::new(s);
parse_input(f, &mut input)
}
fn parse_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>>
where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style),
PARSING_MODE_DEFAULT,
QuirksMode::NoQuirks);
let mut parser = Parser::new(s);
let mut parser = Parser::new(input);
f(&context, &mut parser)
}
fn parse_entirely<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
parse(|context, parser| parser.parse_entirely(|p| f(context, p)), s)
fn parse_entirely<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>>
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>> {
let mut input = ParserInput::new(s);
parse_entirely_input(f, &mut input)
}
fn assert_computed_serialization<C, F, T>(f: F, input: &str, output: &str)
where F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>,
fn parse_entirely_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>>
where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
parse_input(|context, parser| parser.parse_entirely(|p| f(context, p)), input)
}
fn assert_computed_serialization<C, F, T>(f: F, input: &'static str, output: &str)
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>,
T: ToComputedValue<ComputedValue=C>, C: ToCss
{
let viewport_size = TypedSize2D::new(0., 0.);
@ -63,21 +77,23 @@ macro_rules! assert_roundtrip_with_context {
assert_roundtrip_with_context!($fun, $string, $string);
};
($fun:expr, $input:expr, $output:expr) => {{
let serialized = parse(|context, i| {
let mut input = ::cssparser::ParserInput::new($input);
let serialized = super::parse_input(|context, i| {
let parsed = $fun(context, i)
.expect(&format!("Failed to parse {}", $input));
let serialized = ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output);
Ok(serialized)
}, $input).unwrap();
}, &mut input).unwrap();
parse(|context, i| {
let mut input = ::cssparser::ParserInput::new(&serialized);
super::parse_input(|context, i| {
let re_parsed = $fun(context, i)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized);
Ok(())
}, &serialized).unwrap()
}, &mut input).unwrap()
}}
}
@ -86,13 +102,15 @@ macro_rules! assert_roundtrip {
assert_roundtrip!($fun, $string, $string);
};
($fun:expr, $input:expr, $output:expr) => {
let mut parser = Parser::new($input);
let mut input = ParserInput::new($input);
let mut parser = Parser::new(&mut input);
let parsed = $fun(&mut parser)
.expect(&format!("Failed to parse {}", $input));
let serialized = ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output);
let mut parser = Parser::new(&serialized);
let mut input = ParserInput::new(&serialized);
let mut parser = Parser::new(&mut input);
let re_parsed = $fun(&mut parser)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
@ -113,7 +131,7 @@ macro_rules! assert_parser_exhausted {
macro_rules! parse_longhand {
($name:ident, $s:expr) => {
parse($name::parse, $s).unwrap()
parse($name::parse, $s).unwrap()
};
}

View file

@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::{Parser, ToCss};
use cssparser::{Parser, ParserInput, ToCss};
use selectors::parser::SelectorList;
use style::selector_parser::{SelectorImpl, SelectorParser};
use style::stylesheets::{Origin, Namespaces};
use style_traits::ParseError;
fn parse_selector(input: &mut Parser) -> Result<SelectorList<SelectorImpl>, ()> {
fn parse_selector<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SelectorList<SelectorImpl>, ParseError<'i>> {
let mut ns = Namespaces::default();
ns.prefixes.insert("svg".into(), (ns!(svg), ()));
let parser = SelectorParser {

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use style::stylesheets::supports_rule::SupportsCondition;
use style_traits::ToCss;

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use app_units::Au;
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use media_queries::CSSErrorReporterTest;
use style::context::QuirksMode;
use style::parser::{PARSING_MODE_ALLOW_ALL_NUMERIC_VALUES, ParserContext};
@ -27,7 +27,8 @@ fn test_parsing_allo_all_numeric_values() {
let context = ParserContext::new(Origin::Author, &url, &reporter,
Some(CssRuleType::Style), PARSING_MODE_ALLOW_ALL_NUMERIC_VALUES,
QuirksMode::NoQuirks);
let mut parser = Parser::new("-1");
let mut input = ParserInput::new("-1");
let mut parser = Parser::new(&mut input);
let result = Number::parse_non_negative(&context, &mut parser);
assert!(result.is_ok());
assert_eq!(result.unwrap(), Number::new(-1.));

View file

@ -2,19 +2,27 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use media_queries::CSSErrorReporterTest;
use style::context::QuirksMode;
use style::parser::{PARSING_MODE_DEFAULT, ParserContext};
use style::stylesheets::{CssRuleType, Origin};
use style_traits::ParseError;
fn parse<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
fn parse<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>>
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>> {
let mut input = ParserInput::new(s);
parse_input(f, &mut input)
}
fn parse_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>>
where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style),
PARSING_MODE_DEFAULT,
QuirksMode::NoQuirks);
let mut parser = Parser::new(s);
let mut parser = Parser::new(input);
f(&context, &mut parser)
}
@ -31,13 +39,14 @@ macro_rules! assert_roundtrip_with_context {
Ok(serialized)
}, $input).unwrap();
parse(|context, i| {
let mut input = ::cssparser::ParserInput::new(&serialized);
parse_input(|context, i| {
let re_parsed = $fun(context, i)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized);
Ok(())
}, &serialized).unwrap()
}, &mut input).unwrap()
}}
}

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use properties::parse;
use properties::{parse, parse_input};
use style::computed_values::display::T::inline_block;
use style::properties::{PropertyDeclaration, Importance, PropertyId};
use style::properties::parse_property_declaration_list;

View file

@ -4,7 +4,7 @@
#[test]
fn smoke_restyle_hints() {
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use selectors::parser::SelectorList;
use style::restyle_hints::DependencySet;
use style::selector_parser::SelectorParser;
@ -17,7 +17,8 @@ fn smoke_restyle_hints() {
let mut dependencies = DependencySet::new();
let mut p = Parser::new(":not(:active) ~ label");
let mut input = ParserInput::new(":not(:active) ~ label");
let mut p = Parser::new(&mut input);
let selectors = SelectorList::parse(&parser, &mut p).unwrap();
assert_eq!((selectors.0).len(), 1);

View file

@ -6,7 +6,7 @@ use cssparser::{Parser, SourcePosition};
use rayon;
use servo_url::ServoUrl;
use style::context::QuirksMode;
use style::error_reporting::ParseErrorReporter;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
use style::media_queries::MediaList;
use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock};
use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource};
@ -17,15 +17,15 @@ use test::{self, Bencher};
struct ErrorringErrorReporter;
impl ParseErrorReporter for ErrorringErrorReporter {
fn report_error(&self,
input: &mut Parser,
position: SourcePosition,
message: &str,
url: &ServoUrl,
line_number_offset: u64) {
fn report_error<'a>(&self,
input: &mut Parser,
position: SourcePosition,
error: ContextualParseError<'a>,
url: &ServoUrl,
line_number_offset: u64) {
let location = input.source_location(position);
let line_offset = location.line + line_number_offset as usize;
panic!("CSS error: {}\t\n{}:{} {}", url.as_str(), line_offset, location.column, message);
panic!("CSS error: {}\t\n{}:{} {}", url.as_str(), line_offset, location.column, error.to_string());
}
}

View file

@ -14,7 +14,7 @@ use std::borrow::ToOwned;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use style::context::QuirksMode;
use style::error_reporting::ParseErrorReporter;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
use style::media_queries::MediaList;
use style::properties::Importance;
use style::properties::{CSSWideKeyword, DeclaredValueOwned, PropertyDeclaration, PropertyDeclarationBlock};
@ -267,12 +267,12 @@ impl CSSInvalidErrorReporterTest {
}
impl ParseErrorReporter for CSSInvalidErrorReporterTest {
fn report_error(&self,
input: &mut CssParser,
position: SourcePosition,
message: &str,
url: &ServoUrl,
line_number_offset: u64) {
fn report_error<'a>(&self,
input: &mut CssParser,
position: SourcePosition,
error: ContextualParseError<'a>,
url: &ServoUrl,
line_number_offset: u64) {
let location = input.source_location(position);
let line_offset = location.line + line_number_offset as usize;
@ -283,7 +283,7 @@ impl ParseErrorReporter for CSSInvalidErrorReporterTest {
url: url.clone(),
line: line_offset,
column: location.column,
message: message.to_owned()
message: error.to_string()
}
);
}
@ -312,12 +312,13 @@ fn test_report_error_stylesheet() {
let mut errors = errors.lock().unwrap();
let error = errors.pop().unwrap();
assert_eq!("Unsupported property declaration: 'invalid: true;'", error.message);
assert_eq!("Unsupported property declaration: 'invalid: true;', found unexpected identifier true", error.message);
assert_eq!(10, error.line);
assert_eq!(9, error.column);
let error = errors.pop().unwrap();
assert_eq!("Unsupported property declaration: 'display: invalid;'", error.message);
assert_eq!("Unsupported property declaration: 'display: invalid;', \
Custom(PropertyDeclaration(InvalidValue))", error.message);
assert_eq!(9, error.line);
assert_eq!(9, error.column);

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use cssparser::{Parser, ParserInput};
use euclid::size::TypedSize2D;
use media_queries::CSSErrorReporterTest;
use servo_config::prefs::{PREFS, PrefValue};
@ -300,15 +300,17 @@ fn constrain_viewport() {
macro_rules! from_css {
($css:expr) => {
&ViewportRule::parse(&context, &mut Parser::new($css)).unwrap()
&ViewportRule::parse(&context, &mut Parser::new(&mut $css)).unwrap()
}
}
let initial_viewport = TypedSize2D::new(800., 600.);
let device = Device::new(MediaType::Screen, initial_viewport);
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!(""), QuirksMode::NoQuirks), None);
let mut input = ParserInput::new("");
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!(input), QuirksMode::NoQuirks), None);
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!("width: 320px auto"), QuirksMode::NoQuirks),
let mut input = ParserInput::new("width: 320px auto");
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!(input), QuirksMode::NoQuirks),
Some(ViewportConstraints {
size: initial_viewport,
@ -320,7 +322,8 @@ fn constrain_viewport() {
orientation: Orientation::Auto
}));
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!("width: 320px auto"), QuirksMode::NoQuirks),
let mut input = ParserInput::new("width: 320px auto");
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!(input), QuirksMode::NoQuirks),
Some(ViewportConstraints {
size: initial_viewport,
@ -332,11 +335,12 @@ fn constrain_viewport() {
orientation: Orientation::Auto
}));
assert_eq!(ViewportConstraints::maybe_new(&device,
from_css!("width: 800px; height: 600px;\
let mut input = ParserInput::new("width: 800px; height: 600px;\
zoom: 1;\
user-zoom: zoom;\
orientation: auto;"),
orientation: auto;");
assert_eq!(ViewportConstraints::maybe_new(&device,
from_css!(input),
QuirksMode::NoQuirks),
Some(ViewportConstraints {
size: initial_viewport,
@ -351,7 +355,8 @@ fn constrain_viewport() {
let initial_viewport = TypedSize2D::new(200., 150.);
let device = Device::new(MediaType::Screen, initial_viewport);
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!("width: 320px auto"), QuirksMode::NoQuirks),
let mut input = ParserInput::new("width: 320px auto");
assert_eq!(ViewportConstraints::maybe_new(&device, from_css!(input), QuirksMode::NoQuirks),
Some(ViewportConstraints {
size: TypedSize2D::new(320., 240.),

View file

@ -16,7 +16,7 @@ testing = ["style/testing"]
[dependencies]
atomic_refcell = "0.1"
cssparser = "0.13.7"
cssparser = "0.14.0"
env_logger = "0.4"
euclid = "0.13"
geckoservo = {path = "../../../ports/geckolib"}