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 <color> value parsing
errors, so just add formatting/support for that.
This commit is contained in:
Cameron McCormack 2017-12-04 12:52:39 +08:00
parent 7940061c9a
commit 7015d5b22e
2 changed files with 28 additions and 2 deletions

View file

@ -46,6 +46,8 @@ pub enum ContextualParseError<'a> {
InvalidCounterStyleExtendsWithAdditiveSymbols, InvalidCounterStyleExtendsWithAdditiveSymbols,
/// A media rule was invalid for some reason. /// A media rule was invalid for some reason.
InvalidMediaRule(&'a str, ParseError<'a>), InvalidMediaRule(&'a str, ParseError<'a>),
/// A value was not recognized.
UnsupportedValue(&'a str, ParseError<'a>),
} }
impl<'a> fmt::Display for ContextualParseError<'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)?; write!(f, "Invalid media rule: {}, ", media_rule)?;
parse_error_to_str(err, f) parse_error_to_str(err, f)
} }
ContextualParseError::UnsupportedValue(_value, ref err) => {
parse_error_to_str(err, f)
}
} }
} }
} }

View file

@ -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::ErrorReporter as GeckoErrorReporter;
use style::gecko_bindings::structs::URLExtraData as RawUrlExtraData; use style::gecko_bindings::structs::URLExtraData as RawUrlExtraData;
use style::stylesheets::UrlExtraData; use style::stylesheets::UrlExtraData;
use style_traits::StyleParseErrorKind; use style_traits::{StyleParseErrorKind, ValueParseErrorKind};
pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>; pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>;
@ -139,6 +139,9 @@ fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option<ErrorParams<'a>> {
ParseErrorKind::Custom( ParseErrorKind::Custom(
StyleParseErrorKind::ExpectedIdentifier(token) StyleParseErrorKind::ExpectedIdentifier(token)
) |
ParseErrorKind::Custom(
StyleParseErrorKind::ValueError(ValueParseErrorKind::InvalidColor(token))
) => { ) => {
(Some(ErrorString::UnexpectedToken(token)), None) (Some(ErrorString::UnexpectedToken(token)), None)
} }
@ -198,7 +201,8 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
ContextualParseError::UnsupportedRule(s, err) | ContextualParseError::UnsupportedRule(s, err) |
ContextualParseError::UnsupportedViewportDescriptorDeclaration(s, err) | ContextualParseError::UnsupportedViewportDescriptorDeclaration(s, err) |
ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(s, err) | ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(s, err) |
ContextualParseError::InvalidMediaRule(s, err) => { ContextualParseError::InvalidMediaRule(s, err) |
ContextualParseError::UnsupportedValue(s, err) => {
(s.into(), err.kind) (s.into(), err.kind)
} }
ContextualParseError::InvalidCounterStyleWithoutSymbols(s) | ContextualParseError::InvalidCounterStyleWithoutSymbols(s) |
@ -360,6 +364,23 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
ContextualParseError::UnsupportedFontFeatureValuesDescriptor(..) | ContextualParseError::UnsupportedFontFeatureValuesDescriptor(..) |
ContextualParseError::InvalidFontFeatureValuesRule(..) => ContextualParseError::InvalidFontFeatureValuesRule(..) =>
(b"PEUnknownAtRule\0", Action::Skip), (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) (None, msg, action)
} }