mirror of
https://github.com/servo/servo.git
synced 2025-08-11 00:15:32 +01:00
Thread ParseError return values through CSS parsing.
This commit is contained in:
parent
58e39bfffa
commit
27ae1ef2e7
121 changed files with 2133 additions and 1505 deletions
|
@ -4,12 +4,14 @@
|
|||
|
||||
//! [@supports rules](https://drafts.csswg.org/css-conditional-3/#at-supports)
|
||||
|
||||
use cssparser::{BasicParseError, ParseError as CssParseError, ParserInput};
|
||||
use cssparser::{Delimiter, parse_important, Parser, SourceLocation, Token};
|
||||
use parser::ParserContext;
|
||||
use properties::{PropertyId, PropertyDeclaration, SourcePropertyDeclaration};
|
||||
use selectors::parser::SelectorParseError;
|
||||
use shared_lock::{DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
|
||||
use std::fmt;
|
||||
use style_traits::ToCss;
|
||||
use style_traits::{ToCss, ParseError, StyleParseError};
|
||||
use stylearc::Arc;
|
||||
use stylesheets::{CssRuleType, CssRules};
|
||||
|
||||
|
@ -81,7 +83,7 @@ impl SupportsCondition {
|
|||
/// Parse a condition
|
||||
///
|
||||
/// https://drafts.csswg.org/css-conditional/#supports_condition
|
||||
pub fn parse(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
|
||||
if let Ok(_) = input.try(|i| i.expect_ident_matching("not")) {
|
||||
let inner = SupportsCondition::parse_in_parens(input)?;
|
||||
return Ok(SupportsCondition::Not(Box::new(inner)));
|
||||
|
@ -90,7 +92,7 @@ impl SupportsCondition {
|
|||
let in_parens = SupportsCondition::parse_in_parens(input)?;
|
||||
|
||||
let (keyword, wrapper) = match input.next() {
|
||||
Err(()) => {
|
||||
Err(_) => {
|
||||
// End of input
|
||||
return Ok(in_parens)
|
||||
}
|
||||
|
@ -98,10 +100,10 @@ impl SupportsCondition {
|
|||
match_ignore_ascii_case! { &ident,
|
||||
"and" => ("and", SupportsCondition::And as fn(_) -> _),
|
||||
"or" => ("or", SupportsCondition::Or as fn(_) -> _),
|
||||
_ => return Err(())
|
||||
_ => return Err(SelectorParseError::UnexpectedIdent(ident.clone()).into())
|
||||
}
|
||||
}
|
||||
_ => return Err(())
|
||||
Ok(t) => return Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t)))
|
||||
};
|
||||
|
||||
let mut conditions = Vec::with_capacity(2);
|
||||
|
@ -118,7 +120,7 @@ impl SupportsCondition {
|
|||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens
|
||||
fn parse_in_parens(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
fn parse_in_parens<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
|
||||
// Whitespace is normally taken care of in `Parser::next`,
|
||||
// but we want to not include it in `pos` for the SupportsCondition::FutureSyntax cases.
|
||||
while input.try(Parser::expect_whitespace).is_ok() {}
|
||||
|
@ -127,17 +129,18 @@ impl SupportsCondition {
|
|||
Token::ParenthesisBlock => {
|
||||
input.parse_nested_block(|input| {
|
||||
// `input.try()` not needed here since the alternative uses `consume_all()`.
|
||||
parse_condition_or_declaration(input).or_else(|()| {
|
||||
parse_condition_or_declaration(input).or_else(|_| {
|
||||
consume_all(input);
|
||||
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
|
||||
})
|
||||
})
|
||||
}
|
||||
Token::Function(_) => {
|
||||
input.parse_nested_block(|i| Ok(consume_all(i))).unwrap();
|
||||
let result: Result<_, ParseError> = input.parse_nested_block(|i| Ok(consume_all(i)));
|
||||
result.unwrap();
|
||||
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
|
||||
}
|
||||
_ => Err(())
|
||||
t => Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,7 +159,8 @@ impl SupportsCondition {
|
|||
|
||||
/// supports_condition | declaration
|
||||
/// https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext
|
||||
pub fn parse_condition_or_declaration(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
pub fn parse_condition_or_declaration<'i, 't>(input: &mut Parser<'i, 't>)
|
||||
-> Result<SupportsCondition, ParseError<'i>> {
|
||||
if let Ok(condition) = input.try(SupportsCondition::parse) {
|
||||
Ok(SupportsCondition::Parenthesized(Box::new(condition)))
|
||||
} else {
|
||||
|
@ -244,7 +248,7 @@ fn consume_all(input: &mut Parser) {
|
|||
|
||||
impl Declaration {
|
||||
/// Parse a declaration
|
||||
pub fn parse(input: &mut Parser) -> Result<Declaration, ()> {
|
||||
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
|
||||
let prop = input.expect_ident()?.into_owned();
|
||||
input.expect_colon()?;
|
||||
let val = parse_anything(input);
|
||||
|
@ -260,12 +264,13 @@ impl Declaration {
|
|||
} else {
|
||||
return false
|
||||
};
|
||||
let mut input = Parser::new(&self.val);
|
||||
let mut input = ParserInput::new(&self.val);
|
||||
let mut input = Parser::new(&mut input);
|
||||
let context = ParserContext::new_with_rule_type(cx, Some(CssRuleType::Style));
|
||||
let mut declarations = SourcePropertyDeclaration::new();
|
||||
let res = input.parse_until_before(Delimiter::Bang, |input| {
|
||||
PropertyDeclaration::parse_into(&mut declarations, id, &context, input)
|
||||
.map_err(|_| ())
|
||||
.map_err(|e| StyleParseError::PropertyDeclaration(e).into())
|
||||
});
|
||||
let _ = input.try(parse_important);
|
||||
res.is_ok() && input.is_exhausted()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue