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

@ -16,9 +16,10 @@ gecko = []
[dependencies]
app_units = "0.4.1"
cssparser = "0.13.7"
cssparser = "0.14.0"
euclid = "0.13"
heapsize = {version = "0.4", optional = true}
heapsize_derive = {version = "0.1", optional = true}
selectors = { path = "../selectors" }
serde = {version = "0.9", optional = true}
serde_derive = {version = "0.9", optional = true}

View file

@ -18,8 +18,12 @@ extern crate app_units;
extern crate euclid;
#[cfg(feature = "servo")] extern crate heapsize;
#[cfg(feature = "servo")] #[macro_use] extern crate heapsize_derive;
extern crate selectors;
#[cfg(feature = "servo")] #[macro_use] extern crate serde_derive;
use selectors::parser::SelectorParseError;
use std::borrow::Cow;
/// Opaque type stored in type-unsafe work queues for parallel layout.
/// Must be transmutable to and from `TNode`.
pub type UnsafeNode = (usize, usize);
@ -68,3 +72,71 @@ pub mod viewport;
pub use values::{ToCss, OneOrMoreCommaSeparated};
pub use viewport::HasViewportPercentage;
/// The error type for all CSS parsing routines.
pub type ParseError<'i> = cssparser::ParseError<'i, SelectorParseError<'i, StyleParseError<'i>>>;
#[derive(Clone, Debug, PartialEq)]
/// Errors that can be encountered while parsing CSS values.
pub enum StyleParseError<'i> {
/// A bad URL token in a DVB.
BadUrlInDeclarationValueBlock,
/// A bad string token in a DVB.
BadStringInDeclarationValueBlock,
/// Unexpected closing parenthesis in a DVB.
UnbalancedCloseParenthesisInDeclarationValueBlock,
/// Unexpected closing bracket in a DVB.
UnbalancedCloseSquareBracketInDeclarationValueBlock,
/// Unexpected closing curly bracket in a DVB.
UnbalancedCloseCurlyBracketInDeclarationValueBlock,
/// A property declaration parsing error.
PropertyDeclaration(PropertyDeclarationParseError),
/// A property declaration value had input remaining after successfully parsing.
PropertyDeclarationValueNotExhausted,
/// An unexpected dimension token was encountered.
UnexpectedDimension(Cow<'i, str>),
/// A media query using a ranged expression with no value was encountered.
RangedExpressionWithNoValue,
/// A function was encountered that was not expected.
UnexpectedFunction(Cow<'i, str>),
/// @namespace must be before any rule but @charset and @import
UnexpectedNamespaceRule,
/// @import must be before any rule but @charset
UnexpectedImportRule,
/// Unexpected @charset rule encountered.
UnexpectedCharsetRule,
/// Unsupported @ rule
UnsupportedAtRule(Cow<'i, str>),
/// A placeholder for many sources of errors that require more specific variants.
UnspecifiedError,
}
/// The result of parsing a property declaration.
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum PropertyDeclarationParseError {
/// The property declaration was for an unknown property.
UnknownProperty,
/// The property declaration was for a disabled experimental property.
ExperimentalProperty,
/// The property declaration contained an invalid value.
InvalidValue,
/// The declaration contained an animation property, and we were parsing
/// this as a keyframe block (so that property should be ignored).
///
/// See: https://drafts.csswg.org/css-animations/#keyframes
AnimationPropertyInKeyframeBlock,
/// The property is not allowed within a page rule.
NotAllowedInPageRule,
}
impl<'a> From<StyleParseError<'a>> for ParseError<'a> {
fn from(this: StyleParseError<'a>) -> Self {
cssparser::ParseError::Custom(SelectorParseError::Custom(this))
}
}
impl<'a> From<PropertyDeclarationParseError> for ParseError<'a> {
fn from(this: PropertyDeclarationParseError) -> Self {
cssparser::ParseError::Custom(SelectorParseError::Custom(StyleParseError::PropertyDeclaration(this)))
}
}

View file

@ -171,9 +171,13 @@ macro_rules! __define_css_keyword_enum__actual {
impl $name {
/// Parse this property from a CSS input stream.
pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> {
pub fn parse<'i, 't>(input: &mut ::cssparser::Parser<'i, 't>)
-> Result<$name, $crate::ParseError<'i>> {
let ident = input.expect_ident()?;
Self::from_ident(&ident)
.map_err(|()| ::cssparser::ParseError::Basic(
::cssparser::BasicParseError::UnexpectedToken(
::cssparser::Token::Ident(ident))))
}
/// Parse this property from an already-tokenized identifier.

View file

@ -4,8 +4,8 @@
//! Helper types for the `@viewport` rule.
use {CSSPixel, PinchZoomFactor};
use cssparser::{Parser, ToCss};
use {CSSPixel, PinchZoomFactor, ParseError};
use cssparser::{Parser, ToCss, ParseError as CssParseError, BasicParseError};
use euclid::size::TypedSize2D;
use std::ascii::AsciiExt;
use std::fmt;
@ -140,7 +140,7 @@ impl Zoom {
/// Parse a zoom value per:
///
/// https://drafts.csswg.org/css-device-adapt/#descdef-viewport-zoom
pub fn parse(input: &mut Parser) -> Result<Zoom, ()> {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Zoom, ParseError<'i>> {
use cssparser::Token;
match try!(input.next()) {
@ -150,7 +150,7 @@ impl Zoom {
Ok(Zoom::Number(value.value)),
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
Ok(Zoom::Auto),
_ => Err(())
t => Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t)))
}
}