Update to cssparser 0.22 (source location in error types)

This commit is contained in:
Simon Sapin 2017-09-29 21:18:35 +02:00
parent 056e599562
commit c0f8f15f39
90 changed files with 974 additions and 790 deletions

View file

@ -15,7 +15,7 @@ gecko_debug = ["style/gecko_debug"]
[dependencies]
atomic_refcell = "0.1"
cssparser = "0.21.1"
cssparser = "0.22.0"
env_logger = {version = "0.4", default-features = false} # disable `regex` to reduce code size
libc = "0.2"
log = {version = "0.3.5", features = ["release_max_level_info"]}

View file

@ -7,8 +7,8 @@
#![allow(unsafe_code)]
use cssparser::{CowRcStr, serialize_identifier, ToCss};
use cssparser::{SourceLocation, ParseError as CssParseError, Token, BasicParseError};
use selectors::parser::SelectorParseError;
use cssparser::{SourceLocation, ParseError, ParseErrorKind, Token, BasicParseErrorKind};
use selectors::parser::SelectorParseErrorKind;
use std::ptr;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
use style::gecko_bindings::bindings::{Gecko_CreateCSSErrorReporter, Gecko_DestroyCSSErrorReporter};
@ -18,7 +18,9 @@ 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::{ParseError, StyleParseError, PropertyDeclarationParseError, ValueParseError};
use style_traits::{StyleParseErrorKind, PropertyDeclarationParseErrorKind, ValueParseErrorKind};
pub type ErrorKind<'i> = ParseErrorKind<'i, SelectorParseErrorKind<'i, StyleParseErrorKind<'i>>>;
/// Wrapper around an instance of Gecko's CSS error reporter.
pub struct ErrorReporter(*mut GeckoErrorReporter);
@ -70,48 +72,48 @@ enum Action {
}
trait ErrorHelpers<'a> {
fn error_data(self) -> (CowRcStr<'a>, ParseError<'a>);
fn error_data(self) -> (CowRcStr<'a>, ErrorKind<'a>);
fn error_params(self) -> ErrorParams<'a>;
fn to_gecko_message(&self) -> (Option<&'static [u8]>, &'static [u8], Action);
}
fn extract_error_param<'a>(err: ParseError<'a>) -> Option<ErrorString<'a>> {
fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
Some(match err {
CssParseError::Basic(BasicParseError::UnexpectedToken(t)) => {
ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(t)) => {
ErrorString::UnexpectedToken(t)
}
CssParseError::Basic(BasicParseError::AtRuleInvalid(i)) |
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::UnsupportedAtRule(i)
ParseErrorKind::Basic(BasicParseErrorKind::AtRuleInvalid(i)) |
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::UnsupportedAtRule(i)
)) => {
let mut s = String::from("@");
serialize_identifier(&i, &mut s).unwrap();
ErrorString::Snippet(s.into())
}
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::PropertyDeclaration(
PropertyDeclarationParseError::InvalidValue(property, None)
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(property, None)
)
)) => {
ErrorString::Snippet(property)
}
CssParseError::Custom(SelectorParseError::UnexpectedIdent(ident)) => {
ParseErrorKind::Custom(SelectorParseErrorKind::UnexpectedIdent(ident)) => {
ErrorString::Ident(ident)
}
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::PropertyDeclaration(
PropertyDeclarationParseError::UnknownProperty(property)
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::UnknownProperty(property)
)
)) => {
ErrorString::Ident(property)
}
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::UnexpectedTokenWithinNamespace(token)
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::UnexpectedTokenWithinNamespace(token)
)) => {
ErrorString::UnexpectedToken(token)
}
@ -120,10 +122,10 @@ fn extract_error_param<'a>(err: ParseError<'a>) -> Option<ErrorString<'a>> {
})
}
fn extract_value_error_param<'a>(err: ValueParseError<'a>) -> ErrorString<'a> {
fn extract_value_error_param<'a>(err: ValueParseErrorKind<'a>) -> ErrorString<'a> {
match err {
ValueParseError::InvalidColor(t) |
ValueParseError::InvalidFilter(t) => ErrorString::UnexpectedToken(t),
ValueParseErrorKind::InvalidColor(t) |
ValueParseErrorKind::InvalidFilter(t) => ErrorString::UnexpectedToken(t),
}
}
@ -134,46 +136,53 @@ struct ErrorParams<'a> {
/// If an error parameter is present in the given error, return it. Additionally return
/// a second parameter if it exists, for use in the prefix for the eventual error message.
fn extract_error_params<'a>(err: ParseError<'a>) -> Option<ErrorParams<'a>> {
fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option<ErrorParams<'a>> {
let (main, prefix) = match err {
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::PropertyDeclaration(
PropertyDeclarationParseError::InvalidValue(property, Some(e))))) =>
(Some(ErrorString::Snippet(property.into())), Some(extract_value_error_param(e))),
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(property, Some(e))
)
)) => {
(Some(ErrorString::Snippet(property.into())), Some(extract_value_error_param(e)))
}
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::MediaQueryExpectedFeatureName(ident))) =>
(Some(ErrorString::Ident(ident)), None),
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident)
)) => {
(Some(ErrorString::Ident(ident)), None)
}
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::ExpectedIdentifier(token))) =>
(Some(ErrorString::UnexpectedToken(token)), None),
CssParseError::Custom(SelectorParseError::UnexpectedTokenInAttributeSelector(t)) |
CssParseError::Custom(SelectorParseError::BadValueInAttr(t)) |
CssParseError::Custom(SelectorParseError::ExpectedBarInAttr(t)) |
CssParseError::Custom(SelectorParseError::NoQualifiedNameInAttributeSelector(t)) |
CssParseError::Custom(SelectorParseError::InvalidQualNameInAttr(t)) |
CssParseError::Custom(SelectorParseError::ExplicitNamespaceUnexpectedToken(t)) |
CssParseError::Custom(SelectorParseError::PseudoElementExpectedIdent(t)) |
CssParseError::Custom(SelectorParseError::NoIdentForPseudo(t)) |
CssParseError::Custom(SelectorParseError::ClassNeedsIdent(t)) |
CssParseError::Custom(SelectorParseError::PseudoElementExpectedColon(t)) =>
(None, Some(ErrorString::UnexpectedToken(t))),
CssParseError::Custom(SelectorParseError::ExpectedNamespace(namespace)) =>
(None, Some(ErrorString::Ident(namespace))),
CssParseError::Custom(SelectorParseError::UnsupportedPseudoClassOrElement(p)) =>
(None, Some(ErrorString::Ident(p))),
CssParseError::Custom(SelectorParseError::EmptySelector) |
CssParseError::Custom(SelectorParseError::DanglingCombinator) =>
(None, None),
CssParseError::Custom(SelectorParseError::EmptyNegation) =>
(None, Some(ErrorString::Snippet(")".into()))),
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::ExpectedIdentifier(token)
)) => {
(Some(ErrorString::UnexpectedToken(token)), None)
}
ParseErrorKind::Custom(SelectorParseErrorKind::UnexpectedTokenInAttributeSelector(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::BadValueInAttr(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::ExpectedBarInAttr(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::NoQualifiedNameInAttributeSelector(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::InvalidQualNameInAttr(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::ExplicitNamespaceUnexpectedToken(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::PseudoElementExpectedIdent(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::NoIdentForPseudo(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::ClassNeedsIdent(t)) |
ParseErrorKind::Custom(SelectorParseErrorKind::PseudoElementExpectedColon(t)) => {
(None, Some(ErrorString::UnexpectedToken(t)))
}
ParseErrorKind::Custom(SelectorParseErrorKind::ExpectedNamespace(namespace)) => {
(None, Some(ErrorString::Ident(namespace)))
}
ParseErrorKind::Custom(SelectorParseErrorKind::UnsupportedPseudoClassOrElement(p)) => {
(None, Some(ErrorString::Ident(p)))
}
ParseErrorKind::Custom(SelectorParseErrorKind::EmptySelector) |
ParseErrorKind::Custom(SelectorParseErrorKind::DanglingCombinator) => {
(None, None)
}
ParseErrorKind::Custom(SelectorParseErrorKind::EmptyNegation) => {
(None, Some(ErrorString::Snippet(")".into())))
}
err => match extract_error_param(err) {
Some(e) => (Some(e), None),
None => return None,
@ -186,7 +195,7 @@ fn extract_error_params<'a>(err: ParseError<'a>) -> Option<ErrorParams<'a>> {
}
impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
fn error_data(self) -> (CowRcStr<'a>, ParseError<'a>) {
fn error_data(self) -> (CowRcStr<'a>, ErrorKind<'a>) {
match self {
ContextualParseError::UnsupportedPropertyDeclaration(s, err) |
ContextualParseError::UnsupportedFontFaceDescriptor(s, err) |
@ -198,15 +207,18 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
ContextualParseError::UnsupportedRule(s, err) |
ContextualParseError::UnsupportedViewportDescriptorDeclaration(s, err) |
ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(s, err) |
ContextualParseError::InvalidMediaRule(s, err) =>
(s.into(), err),
ContextualParseError::InvalidMediaRule(s, err) => {
(s.into(), err.kind)
}
ContextualParseError::InvalidCounterStyleWithoutSymbols(s) |
ContextualParseError::InvalidCounterStyleNotEnoughSymbols(s) =>
(s.into(), StyleParseError::UnspecifiedError.into()),
ContextualParseError::InvalidCounterStyleNotEnoughSymbols(s) => {
(s.into(), ParseErrorKind::Custom(StyleParseErrorKind::UnspecifiedError.into()))
}
ContextualParseError::InvalidCounterStyleWithoutAdditiveSymbols |
ContextualParseError::InvalidCounterStyleExtendsWithSymbols |
ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols =>
("".into(), StyleParseError::UnspecifiedError.into())
ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols => {
("".into(), ParseErrorKind::Custom(StyleParseErrorKind::UnspecifiedError.into()))
}
}
}
@ -221,17 +233,23 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
fn to_gecko_message(&self) -> (Option<&'static [u8]>, &'static [u8], Action) {
let (msg, action): (&[u8], Action) = match *self {
ContextualParseError::UnsupportedPropertyDeclaration(
_, CssParseError::Basic(BasicParseError::UnexpectedToken(_))) |
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(_)), .. }
) |
ContextualParseError::UnsupportedPropertyDeclaration(
_, CssParseError::Basic(BasicParseError::AtRuleInvalid(_))) =>
(b"PEParseDeclarationDeclExpected\0", Action::Skip),
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::AtRuleInvalid(_)), .. }
) => {
(b"PEParseDeclarationDeclExpected\0", Action::Skip)
}
ContextualParseError::UnsupportedPropertyDeclaration(
_, CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::PropertyDeclaration(
PropertyDeclarationParseError::InvalidValue(_, ref err))))) => {
_, ParseError { kind: ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(_, ref err)
))
), .. }
) => {
let prefix = match *err {
Some(ValueParseError::InvalidColor(_)) => Some(&b"PEColorNotColor\0"[..]),
Some(ValueParseError::InvalidFilter(_)) => Some(&b"PEExpectedNoneOrURLOrFilterFunction\0"[..]),
Some(ValueParseErrorKind::InvalidColor(_)) => Some(&b"PEColorNotColor\0"[..]),
Some(ValueParseErrorKind::InvalidFilter(_)) => Some(&b"PEExpectedNoneOrURLOrFilterFunction\0"[..]),
_ => None,
};
return (prefix, b"PEValueParsingError\0", Action::Drop);
@ -245,67 +263,78 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
ContextualParseError::UnsupportedKeyframePropertyDeclaration(..) =>
(b"PEBadSelectorKeyframeRuleIgnored\0", Action::Nothing),
ContextualParseError::InvalidRule(
_, CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::UnexpectedTokenWithinNamespace(_)))) =>
(b"PEAtNSUnexpected\0", Action::Nothing),
_, ParseError { kind: ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::UnexpectedTokenWithinNamespace(_)
)), .. }
) => {
(b"PEAtNSUnexpected\0", Action::Nothing)
}
ContextualParseError::InvalidRule(
_, CssParseError::Basic(BasicParseError::AtRuleInvalid(_))) |
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::AtRuleInvalid(_)), .. }
) |
ContextualParseError::InvalidRule(
_, CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::UnsupportedAtRule(_)))) =>
(b"PEUnknownAtRule\0", Action::Nothing),
_, ParseError { kind: ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::UnsupportedAtRule(_)
)), .. }
) => {
(b"PEUnknownAtRule\0", Action::Nothing)
}
ContextualParseError::InvalidRule(_, ref err) => {
let prefix = match *err {
CssParseError::Custom(SelectorParseError::UnexpectedTokenInAttributeSelector(_)) =>
let prefix = match err.kind {
ParseErrorKind::Custom(SelectorParseErrorKind::UnexpectedTokenInAttributeSelector(_)) =>
Some(&b"PEAttSelUnexpected\0"[..]),
CssParseError::Custom(SelectorParseError::ExpectedBarInAttr(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::ExpectedBarInAttr(_)) =>
Some(&b"PEAttSelNoBar\0"[..]),
CssParseError::Custom(SelectorParseError::BadValueInAttr(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::BadValueInAttr(_)) =>
Some(&b"PEAttSelBadValue\0"[..]),
CssParseError::Custom(SelectorParseError::NoQualifiedNameInAttributeSelector(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::NoQualifiedNameInAttributeSelector(_)) =>
Some(&b"PEAttributeNameOrNamespaceExpected\0"[..]),
CssParseError::Custom(SelectorParseError::InvalidQualNameInAttr(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::InvalidQualNameInAttr(_)) =>
Some(&b"PEAttributeNameExpected\0"[..]),
CssParseError::Custom(SelectorParseError::ExplicitNamespaceUnexpectedToken(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::ExplicitNamespaceUnexpectedToken(_)) =>
Some(&b"PETypeSelNotType\0"[..]),
CssParseError::Custom(SelectorParseError::ExpectedNamespace(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::ExpectedNamespace(_)) =>
Some(&b"PEUnknownNamespacePrefix\0"[..]),
CssParseError::Custom(SelectorParseError::EmptySelector) =>
ParseErrorKind::Custom(SelectorParseErrorKind::EmptySelector) =>
Some(&b"PESelectorGroupNoSelector\0"[..]),
CssParseError::Custom(SelectorParseError::DanglingCombinator) =>
ParseErrorKind::Custom(SelectorParseErrorKind::DanglingCombinator) =>
Some(&b"PESelectorGroupExtraCombinator\0"[..]),
CssParseError::Custom(SelectorParseError::UnsupportedPseudoClassOrElement(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::UnsupportedPseudoClassOrElement(_)) =>
Some(&b"PEPseudoSelUnknown\0"[..]),
CssParseError::Custom(SelectorParseError::PseudoElementExpectedColon(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::PseudoElementExpectedColon(_)) =>
Some(&b"PEPseudoSelEndOrUserActionPC\0"[..]),
CssParseError::Custom(SelectorParseError::NoIdentForPseudo(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::NoIdentForPseudo(_)) =>
Some(&b"PEPseudoClassArgNotIdent\0"[..]),
CssParseError::Custom(SelectorParseError::PseudoElementExpectedIdent(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::PseudoElementExpectedIdent(_)) =>
Some(&b"PEPseudoSelBadName\0"[..]),
CssParseError::Custom(SelectorParseError::ClassNeedsIdent(_)) =>
ParseErrorKind::Custom(SelectorParseErrorKind::ClassNeedsIdent(_)) =>
Some(&b"PEClassSelNotIdent\0"[..]),
CssParseError::Custom(SelectorParseError::EmptyNegation) =>
ParseErrorKind::Custom(SelectorParseErrorKind::EmptyNegation) =>
Some(&b"PENegationBadArg\0"[..]),
_ => None,
};
return (prefix, b"PEBadSelectorRSIgnored\0", Action::Nothing);
}
ContextualParseError::InvalidMediaRule(_, ref err) => {
let err: &[u8] = match *err {
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::ExpectedIdentifier(..))) => {
let err: &[u8] = match err.kind {
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::ExpectedIdentifier(..)
)) => {
b"PEGatherMediaNotIdent\0"
},
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::MediaQueryExpectedFeatureName(..))) => {
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::MediaQueryExpectedFeatureName(..)
)) => {
b"PEMQExpectedFeatureName\0"
},
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::MediaQueryExpectedFeatureValue)) => {
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::MediaQueryExpectedFeatureValue
)) => {
b"PEMQExpectedFeatureValue\0"
},
CssParseError::Custom(SelectorParseError::Custom(
StyleParseError::RangedExpressionWithNoValue)) => {
ParseErrorKind::Custom(SelectorParseErrorKind::Custom(
StyleParseErrorKind::RangedExpressionWithNoValue
)) => {
b"PEMQNoMinMaxWithoutValue\0"
},
_ => {