From 1a041084ce6005c0ebc615e5872b301904eda76c Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 9 Oct 2017 17:47:19 +0200 Subject: [PATCH] CSS parsing error types: flatten nested enums some more --- components/style_traits/lib.rs | 27 ++++++++++++++------ ports/geckolib/error_reporter.rs | 44 +++++++++++++++++--------------- tests/unit/style/size_of.rs | 8 +++--- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/components/style_traits/lib.rs b/components/style_traits/lib.rs index a87a7031e92..fea9d074ecf 100644 --- a/components/style_traits/lib.rs +++ b/components/style_traits/lib.rs @@ -176,8 +176,12 @@ pub enum PropertyDeclarationParseErrorKind<'i> { UnknownVendorProperty, /// The property declaration was for a disabled experimental property. ExperimentalProperty, + /// The property declaration contained an invalid color value. + InvalidColor(CowRcStr<'i>, Token<'i>), + /// The property declaration contained an invalid filter value. + InvalidFilter(CowRcStr<'i>, Token<'i>), /// The property declaration contained an invalid value. - InvalidValue(CowRcStr<'i>, Option>), + OtherInvalidValue(CowRcStr<'i>), /// The declaration contained an animation property, and we were parsing /// this as a keyframe block (so that property should be ignored). /// @@ -190,14 +194,21 @@ pub enum PropertyDeclarationParseErrorKind<'i> { impl<'i> PropertyDeclarationParseErrorKind<'i> { /// Create an InvalidValue parse error pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> PropertyDeclarationParseError<'i> { - cssparser::ParseError { - kind: cssparser::ParseErrorKind::Custom(PropertyDeclarationParseErrorKind::InvalidValue( - name, - match value_error.kind { - cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => Some(e), - _ => None, + let variant = match value_error.kind { + cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => { + match e { + ValueParseErrorKind::InvalidColor(token) => { + PropertyDeclarationParseErrorKind::InvalidColor(name, token) + } + ValueParseErrorKind::InvalidFilter(token) => { + PropertyDeclarationParseErrorKind::InvalidFilter(name, token) + } } - )), + } + _ => PropertyDeclarationParseErrorKind::OtherInvalidValue(name), + }; + cssparser::ParseError { + kind: cssparser::ParseErrorKind::Custom(variant), location: value_error.location, } } diff --git a/ports/geckolib/error_reporter.rs b/ports/geckolib/error_reporter.rs index c35cece1a89..f86213c3e56 100644 --- a/ports/geckolib/error_reporter.rs +++ b/ports/geckolib/error_reporter.rs @@ -18,7 +18,7 @@ use style::gecko_bindings::structs::ErrorReporter as GeckoErrorReporter; use style::gecko_bindings::structs::URLExtraData as RawUrlExtraData; use style::gecko_bindings::sugar::refptr::RefPtr; use style::stylesheets::UrlExtraData; -use style_traits::{StyleParseErrorKind, PropertyDeclarationParseErrorKind, ValueParseErrorKind}; +use style_traits::{StyleParseErrorKind, PropertyDeclarationParseErrorKind}; pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>; @@ -92,7 +92,7 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option> { ParseErrorKind::Custom( StyleParseErrorKind::PropertyDeclaration( - PropertyDeclarationParseErrorKind::InvalidValue(property, None) + PropertyDeclarationParseErrorKind::OtherInvalidValue(property) ) ) => { ErrorString::Snippet(property) @@ -124,13 +124,6 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option> { }) } -fn extract_value_error_param<'a>(err: ValueParseErrorKind<'a>) -> ErrorString<'a> { - match err { - ValueParseErrorKind::InvalidColor(t) | - ValueParseErrorKind::InvalidFilter(t) => ErrorString::UnexpectedToken(t), - } -} - struct ErrorParams<'a> { prefix_param: Option>, main_param: Option>, @@ -142,10 +135,15 @@ fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option> { let (main, prefix) = match err { ParseErrorKind::Custom( StyleParseErrorKind::PropertyDeclaration( - PropertyDeclarationParseErrorKind::InvalidValue(property, Some(e)) + PropertyDeclarationParseErrorKind::InvalidColor(property, token) + ) + ) | + ParseErrorKind::Custom( + StyleParseErrorKind::PropertyDeclaration( + PropertyDeclarationParseErrorKind::InvalidFilter(property, token) ) ) => { - (Some(ErrorString::Snippet(property.into())), Some(extract_value_error_param(e))) + (Some(ErrorString::Snippet(property.into())), Some(ErrorString::UnexpectedToken(token))) } ParseErrorKind::Custom( @@ -250,17 +248,23 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> { } ContextualParseError::UnsupportedPropertyDeclaration( _, ParseError { kind: ParseErrorKind::Custom( - StyleParseErrorKind::PropertyDeclaration( - PropertyDeclarationParseErrorKind::InvalidValue(_, ref err) - ) + StyleParseErrorKind::PropertyDeclaration(ref err) ), .. } ) => { - let prefix = match *err { - Some(ValueParseErrorKind::InvalidColor(_)) => Some(&b"PEColorNotColor\0"[..]), - Some(ValueParseErrorKind::InvalidFilter(_)) => Some(&b"PEExpectedNoneOrURLOrFilterFunction\0"[..]), - _ => None, - }; - return (prefix, b"PEValueParsingError\0", Action::Drop); + match *err { + PropertyDeclarationParseErrorKind::InvalidColor(_, _) => { + return (Some(b"PEColorNotColor\0"), + b"PEValueParsingError\0", Action::Drop) + } + PropertyDeclarationParseErrorKind::InvalidFilter(_, _) => { + return (Some(b"PEExpectedNoneOrURLOrFilterFunction\0"), + b"PEValueParsingError\0", Action::Drop) + } + PropertyDeclarationParseErrorKind::OtherInvalidValue(_) => { + (b"PEValueParsingError\0", Action::Drop) + } + _ => (b"PEUnknownProperty\0", Action::Drop) + } } ContextualParseError::UnsupportedPropertyDeclaration(..) => (b"PEUnknownProperty\0", Action::Drop), diff --git a/tests/unit/style/size_of.rs b/tests/unit/style/size_of.rs index 79568b29301..d988df27f25 100644 --- a/tests/unit/style/size_of.rs +++ b/tests/unit/style/size_of.rs @@ -15,11 +15,11 @@ size_of_test!(test_size_of_property_declaration, properties::PropertyDeclaration size_of_test!(test_size_of_parsed_declaration, properties::SourcePropertyDeclaration, 576); size_of_test!(test_size_of_selector_parse_error_kind, SelectorParseErrorKind, 40); -size_of_test!(test_size_of_style_parse_error_kind, ::style_traits::StyleParseErrorKind, 80); +size_of_test!(test_size_of_style_parse_error_kind, ::style_traits::StyleParseErrorKind, 64); size_of_test!(test_size_of_value_parse_error_kind, ::style_traits::ValueParseErrorKind, 40); -size_of_test!(test_size_of_declaration_parse_error_kind, ::style_traits::PropertyDeclarationParseErrorKind, 72); +size_of_test!(test_size_of_declaration_parse_error_kind, ::style_traits::PropertyDeclarationParseErrorKind, 56); size_of_test!(test_size_of_selector_parse_error, SelectorParseError, 56); -size_of_test!(test_size_of_style_traits_parse_error, ::style_traits::ParseError, 96); +size_of_test!(test_size_of_style_traits_parse_error, ::style_traits::ParseError, 80); size_of_test!(test_size_of_value_parse_error, ::style_traits::ValueParseError, 56); -size_of_test!(test_size_of_declaration_parse_error, ::style_traits::PropertyDeclarationParseError, 88); +size_of_test!(test_size_of_declaration_parse_error, ::style_traits::PropertyDeclarationParseError, 72);