From 7015d5b22e8157450677cf0e01d83e8afc8c0d1d Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Mon, 4 Dec 2017 12:52:39 +0800 Subject: [PATCH] style: Add a new CSS error type for lone value parsing errors. This allows us to report errors in functions that want to just parse a single CSS value, rather than a value for a particular property declaration. The one value type that we need to support for now is value parsing errors, so just add formatting/support for that. --- components/style/error_reporting.rs | 5 +++++ ports/geckolib/error_reporter.rs | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/components/style/error_reporting.rs b/components/style/error_reporting.rs index 19e89e2338f5..d989857d1416 100644 --- a/components/style/error_reporting.rs +++ b/components/style/error_reporting.rs @@ -46,6 +46,8 @@ pub enum ContextualParseError<'a> { InvalidCounterStyleExtendsWithAdditiveSymbols, /// A media rule was invalid for some reason. InvalidMediaRule(&'a str, ParseError<'a>), + /// A value was not recognized. + UnsupportedValue(&'a str, ParseError<'a>), } impl<'a> fmt::Display for ContextualParseError<'a> { @@ -173,6 +175,9 @@ impl<'a> fmt::Display for ContextualParseError<'a> { write!(f, "Invalid media rule: {}, ", media_rule)?; parse_error_to_str(err, f) } + ContextualParseError::UnsupportedValue(_value, ref err) => { + parse_error_to_str(err, f) + } } } } diff --git a/ports/geckolib/error_reporter.rs b/ports/geckolib/error_reporter.rs index efca5b55ede1..e3e3cdf49c3f 100644 --- a/ports/geckolib/error_reporter.rs +++ b/ports/geckolib/error_reporter.rs @@ -17,7 +17,7 @@ use style::gecko_bindings::structs::{Loader, ServoStyleSheet, nsIURI}; use style::gecko_bindings::structs::ErrorReporter as GeckoErrorReporter; use style::gecko_bindings::structs::URLExtraData as RawUrlExtraData; use style::stylesheets::UrlExtraData; -use style_traits::StyleParseErrorKind; +use style_traits::{StyleParseErrorKind, ValueParseErrorKind}; pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>; @@ -139,6 +139,9 @@ fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option> { ParseErrorKind::Custom( StyleParseErrorKind::ExpectedIdentifier(token) + ) | + ParseErrorKind::Custom( + StyleParseErrorKind::ValueError(ValueParseErrorKind::InvalidColor(token)) ) => { (Some(ErrorString::UnexpectedToken(token)), None) } @@ -198,7 +201,8 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> { ContextualParseError::UnsupportedRule(s, err) | ContextualParseError::UnsupportedViewportDescriptorDeclaration(s, err) | ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(s, err) | - ContextualParseError::InvalidMediaRule(s, err) => { + ContextualParseError::InvalidMediaRule(s, err) | + ContextualParseError::UnsupportedValue(s, err) => { (s.into(), err.kind) } ContextualParseError::InvalidCounterStyleWithoutSymbols(s) | @@ -360,6 +364,23 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> { ContextualParseError::UnsupportedFontFeatureValuesDescriptor(..) | ContextualParseError::InvalidFontFeatureValuesRule(..) => (b"PEUnknownAtRule\0", Action::Skip), + ContextualParseError::UnsupportedValue(_, ParseError { ref kind, .. }) => { + match *kind { + ParseErrorKind::Custom( + StyleParseErrorKind::ValueError( + ValueParseErrorKind::InvalidColor(..) + ) + ) => (b"PEColorNotColor", Action::Nothing), + _ => { + // Not the best error message, since we weren't parsing + // a declaration, just a value. But we don't produce + // UnsupportedValue errors other than InvalidColors + // currently. + debug_assert!(false, "should use a more specific error message"); + (b"PEDeclDropped", Action::Nothing) + } + } + } }; (None, msg, action) }