mirror of
https://github.com/servo/servo.git
synced 2025-08-08 06:55:31 +01:00
Auto merge of #18341 - ferjm:bug1384225.media.errors, r=jdm
stylo: Error reporting for unknown media features - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix [bug 1384225](https://bugzilla.mozilla.org/show_bug.cgi?id=1384225) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18341) <!-- Reviewable:end -->
This commit is contained in:
commit
fef2cfde8c
12 changed files with 206 additions and 109 deletions
|
@ -19,7 +19,7 @@ pub enum ContextualParseError<'a> {
|
|||
UnsupportedPropertyDeclaration(&'a str, ParseError<'a>),
|
||||
/// A font face descriptor was not recognized.
|
||||
UnsupportedFontFaceDescriptor(&'a str, ParseError<'a>),
|
||||
/// A font feature values descroptor was not recognized.
|
||||
/// A font feature values descriptor was not recognized.
|
||||
UnsupportedFontFeatureValuesDescriptor(&'a str, ParseError<'a>),
|
||||
/// A keyframe rule was not valid.
|
||||
InvalidKeyframeRule(&'a str, ParseError<'a>),
|
||||
|
@ -44,7 +44,9 @@ pub enum ContextualParseError<'a> {
|
|||
/// A counter style rule had extends with symbols.
|
||||
InvalidCounterStyleExtendsWithSymbols,
|
||||
/// A counter style rule had extends with additive-symbols.
|
||||
InvalidCounterStyleExtendsWithAdditiveSymbols
|
||||
InvalidCounterStyleExtendsWithAdditiveSymbols,
|
||||
/// A media rule was invalid for some reason.
|
||||
InvalidMediaRule(&'a str, ParseError<'a>),
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for ContextualParseError<'a> {
|
||||
|
@ -168,6 +170,10 @@ impl<'a> fmt::Display for ContextualParseError<'a> {
|
|||
ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols => {
|
||||
write!(f, "Invalid @counter-style rule: 'system: extends …' with 'additive-symbols'")
|
||||
}
|
||||
ContextualParseError::InvalidMediaRule(media_rule, ref err) => {
|
||||
write!(f, "Invalid media rule: {}, ", media_rule)?;
|
||||
parse_error_to_str(err, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ use media_queries::MediaType;
|
|||
use parser::ParserContext;
|
||||
use properties::{ComputedValues, StyleBuilder};
|
||||
use properties::longhands::font_size;
|
||||
use selectors::parser::SelectorParseError;
|
||||
use servo_arc::Arc;
|
||||
use std::fmt::{self, Write};
|
||||
use std::sync::atomic::{AtomicBool, AtomicIsize, Ordering};
|
||||
|
@ -469,6 +468,89 @@ unsafe fn find_in_table<F>(mut current_entry: *const nsCSSProps_KTableEntry,
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_feature_value<'i, 't>(feature: &nsMediaFeature,
|
||||
feature_value_type: nsMediaFeature_ValueType,
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>)
|
||||
-> Result<MediaExpressionValue, ParseError<'i>> {
|
||||
let value = match feature_value_type {
|
||||
nsMediaFeature_ValueType::eLength => {
|
||||
let length = Length::parse_non_negative(context, input)?;
|
||||
// FIXME(canaltinova): See bug 1396057. Gecko doesn't support calc
|
||||
// inside media queries. This check is for temporarily remove it
|
||||
// for parity with gecko. We should remove this check when we want
|
||||
// to support it.
|
||||
if let Length::Calc(_) = length {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::Length(length)
|
||||
},
|
||||
nsMediaFeature_ValueType::eInteger => {
|
||||
// FIXME(emilio): We should use `Integer::parse` to handle `calc`
|
||||
// properly in integer expressions. Note that calc is still not
|
||||
// supported in media queries per FIXME above.
|
||||
let i = input.expect_integer()?;
|
||||
if i < 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::Integer(i as u32)
|
||||
}
|
||||
nsMediaFeature_ValueType::eBoolInteger => {
|
||||
let i = input.expect_integer()?;
|
||||
if i < 0 || i > 1 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::BoolInteger(i == 1)
|
||||
}
|
||||
nsMediaFeature_ValueType::eFloat => {
|
||||
MediaExpressionValue::Float(input.expect_number()?)
|
||||
}
|
||||
nsMediaFeature_ValueType::eIntRatio => {
|
||||
let a = input.expect_integer()?;
|
||||
if a <= 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
|
||||
input.expect_delim('/')?;
|
||||
|
||||
let b = input.expect_integer()?;
|
||||
if b <= 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::IntRatio(a as u32, b as u32)
|
||||
}
|
||||
nsMediaFeature_ValueType::eResolution => {
|
||||
MediaExpressionValue::Resolution(Resolution::parse(input)?)
|
||||
}
|
||||
nsMediaFeature_ValueType::eEnumerated => {
|
||||
let keyword = input.expect_ident()?;
|
||||
let keyword = unsafe {
|
||||
bindings::Gecko_LookupCSSKeyword(keyword.as_bytes().as_ptr(),
|
||||
keyword.len() as u32)
|
||||
};
|
||||
|
||||
let first_table_entry: *const nsCSSProps_KTableEntry = unsafe {
|
||||
*feature.mData.mKeywordTable.as_ref()
|
||||
};
|
||||
|
||||
let value =
|
||||
match unsafe { find_in_table(first_table_entry, |kw, _| kw == keyword) } {
|
||||
Some((_kw, value)) => {
|
||||
value
|
||||
}
|
||||
None => return Err(StyleParseError::UnspecifiedError.into()),
|
||||
};
|
||||
|
||||
MediaExpressionValue::Enumerated(value)
|
||||
}
|
||||
nsMediaFeature_ValueType::eIdent => {
|
||||
MediaExpressionValue::Ident(input.expect_ident()?.as_ref().to_owned())
|
||||
}
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
/// Trivially construct a new expression.
|
||||
fn new(feature: &'static nsMediaFeature,
|
||||
|
@ -488,13 +570,24 @@ impl Expression {
|
|||
/// ```
|
||||
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
input.expect_parenthesis_block()?;
|
||||
input.expect_parenthesis_block().map_err(|err|
|
||||
match err {
|
||||
BasicParseError::UnexpectedToken(t) => StyleParseError::ExpectedIdentifier(t),
|
||||
_ => StyleParseError::UnspecifiedError,
|
||||
}
|
||||
)?;
|
||||
|
||||
input.parse_nested_block(|input| {
|
||||
// FIXME: remove extra indented block when lifetimes are non-lexical
|
||||
let feature;
|
||||
let range;
|
||||
{
|
||||
let ident = input.expect_ident()?;
|
||||
let ident = input.expect_ident().map_err(|err|
|
||||
match err {
|
||||
BasicParseError::UnexpectedToken(t) => StyleParseError::ExpectedIdentifier(t),
|
||||
_ => StyleParseError::UnspecifiedError,
|
||||
}
|
||||
)?;
|
||||
|
||||
let mut flags = 0;
|
||||
let result = {
|
||||
|
@ -530,17 +623,19 @@ impl Expression {
|
|||
Ok((f, r)) => {
|
||||
feature = f;
|
||||
range = r;
|
||||
}
|
||||
Err(()) => return Err(SelectorParseError::UnexpectedIdent(ident.clone()).into()),
|
||||
},
|
||||
Err(()) => {
|
||||
return Err(StyleParseError::MediaQueryExpectedFeatureName(ident.clone()).into())
|
||||
},
|
||||
}
|
||||
|
||||
if (feature.mReqFlags & !flags) != 0 {
|
||||
return Err(SelectorParseError::UnexpectedIdent(ident.clone()).into());
|
||||
return Err(StyleParseError::MediaQueryExpectedFeatureName(ident.clone()).into());
|
||||
}
|
||||
|
||||
if range != nsMediaExpression_Range::eEqual &&
|
||||
feature.mRangeType != nsMediaFeature_RangeType::eMinMaxAllowed {
|
||||
return Err(SelectorParseError::UnexpectedIdent(ident.clone()).into());
|
||||
feature.mRangeType != nsMediaFeature_RangeType::eMinMaxAllowed {
|
||||
return Err(StyleParseError::MediaQueryExpectedFeatureName(ident.clone()).into());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -556,80 +651,11 @@ impl Expression {
|
|||
return Ok(Expression::new(feature, None, range));
|
||||
}
|
||||
|
||||
let value = match feature.mValueType {
|
||||
nsMediaFeature_ValueType::eLength => {
|
||||
let length = Length::parse_non_negative(context, input)?;
|
||||
// FIXME(canaltinova): See bug 1396057. Gecko doesn't support calc
|
||||
// inside media queries. This check is for temporarily remove it
|
||||
// for parity with gecko. We should remove this check when we want
|
||||
// to support it.
|
||||
if let Length::Calc(_) = length {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::Length(length)
|
||||
},
|
||||
nsMediaFeature_ValueType::eInteger => {
|
||||
// FIXME(emilio): We should use `Integer::parse` to handle `calc`
|
||||
// properly in integer expressions. Note that calc is still not
|
||||
// supported in media queries per FIXME above.
|
||||
let i = input.expect_integer()?;
|
||||
if i < 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::Integer(i as u32)
|
||||
}
|
||||
nsMediaFeature_ValueType::eBoolInteger => {
|
||||
let i = input.expect_integer()?;
|
||||
if i < 0 || i > 1 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::BoolInteger(i == 1)
|
||||
}
|
||||
nsMediaFeature_ValueType::eFloat => {
|
||||
MediaExpressionValue::Float(input.expect_number()?)
|
||||
}
|
||||
nsMediaFeature_ValueType::eIntRatio => {
|
||||
let a = input.expect_integer()?;
|
||||
if a <= 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
|
||||
input.expect_delim('/')?;
|
||||
|
||||
let b = input.expect_integer()?;
|
||||
if b <= 0 {
|
||||
return Err(StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
MediaExpressionValue::IntRatio(a as u32, b as u32)
|
||||
}
|
||||
nsMediaFeature_ValueType::eResolution => {
|
||||
MediaExpressionValue::Resolution(Resolution::parse(input)?)
|
||||
}
|
||||
nsMediaFeature_ValueType::eEnumerated => {
|
||||
let keyword = input.expect_ident()?;
|
||||
let keyword = unsafe {
|
||||
bindings::Gecko_LookupCSSKeyword(keyword.as_bytes().as_ptr(),
|
||||
keyword.len() as u32)
|
||||
};
|
||||
|
||||
let first_table_entry: *const nsCSSProps_KTableEntry = unsafe {
|
||||
*feature.mData.mKeywordTable.as_ref()
|
||||
};
|
||||
|
||||
let value =
|
||||
match unsafe { find_in_table(first_table_entry, |kw, _| kw == keyword) } {
|
||||
Some((_kw, value)) => {
|
||||
value
|
||||
}
|
||||
None => return Err(StyleParseError::UnspecifiedError.into()),
|
||||
};
|
||||
|
||||
MediaExpressionValue::Enumerated(value)
|
||||
}
|
||||
nsMediaFeature_ValueType::eIdent => {
|
||||
MediaExpressionValue::Ident(input.expect_ident()?.as_ref().to_owned())
|
||||
}
|
||||
};
|
||||
let value = parse_feature_value(feature,
|
||||
feature.mValueType,
|
||||
context, input).map_err(|_|
|
||||
StyleParseError::MediaQueryExpectedFeatureValue
|
||||
)?;
|
||||
|
||||
Ok(Expression::new(feature, Some(value), range))
|
||||
})
|
||||
|
|
|
@ -8,8 +8,10 @@
|
|||
|
||||
use Atom;
|
||||
use context::QuirksMode;
|
||||
use cssparser::{Delimiter, Parser, Token, ParserInput};
|
||||
use parser::ParserContext;
|
||||
use cssparser::{Delimiter, Parser};
|
||||
use cssparser::{Token, ParserInput};
|
||||
use error_reporting::{ContextualParseError, ParseErrorReporter};
|
||||
use parser::{ParserContext, ParserErrorContext};
|
||||
use selectors::parser::SelectorParseError;
|
||||
use serialize_comma_separated_list;
|
||||
use std::fmt;
|
||||
|
@ -240,19 +242,32 @@ impl MediaQuery {
|
|||
/// media query list is only filled with the equivalent of "not all", see:
|
||||
///
|
||||
/// https://drafts.csswg.org/mediaqueries/#error-handling
|
||||
pub fn parse_media_query_list(context: &ParserContext, input: &mut Parser) -> MediaList {
|
||||
pub fn parse_media_query_list<R>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser,
|
||||
error_reporter: &R,
|
||||
) -> MediaList
|
||||
where
|
||||
R: ParseErrorReporter,
|
||||
{
|
||||
if input.is_exhausted() {
|
||||
return MediaList::empty()
|
||||
}
|
||||
|
||||
let mut media_queries = vec![];
|
||||
loop {
|
||||
let start_position = input.position();
|
||||
let start_location = input.current_source_location();
|
||||
match input.parse_until_before(Delimiter::Comma, |i| MediaQuery::parse(context, i)) {
|
||||
Ok(mq) => {
|
||||
media_queries.push(mq);
|
||||
},
|
||||
Err(..) => {
|
||||
Err(err) => {
|
||||
media_queries.push(MediaQuery::never_matching());
|
||||
let error = ContextualParseError::InvalidMediaRule(
|
||||
input.slice_from(start_position), err);
|
||||
let error_context = ParserErrorContext { error_reporter };
|
||||
context.log_css_error(&error_context, start_location, error);
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -178,7 +178,8 @@ impl<'a, 'i, R: ParseErrorReporter> AtRuleParser<'i> for TopLevelRuleParser<'a,
|
|||
let url_string = input.expect_url_or_string()?.as_ref().to_owned();
|
||||
let specified_url = SpecifiedUrl::parse_from_string(url_string, &self.context)?;
|
||||
|
||||
let media = parse_media_query_list(&self.context, input);
|
||||
let media = parse_media_query_list(&self.context, input,
|
||||
self.error_context.error_reporter);
|
||||
let media = Arc::new(self.shared_lock.wrap(media));
|
||||
|
||||
let prelude = AtRuleNonBlockPrelude::Import(specified_url, media, location);
|
||||
|
@ -354,7 +355,8 @@ impl<'a, 'b, 'i, R: ParseErrorReporter> AtRuleParser<'i> for NestedRuleParser<'a
|
|||
|
||||
match_ignore_ascii_case! { &*name,
|
||||
"media" => {
|
||||
let media_queries = parse_media_query_list(self.context, input);
|
||||
let media_queries = parse_media_query_list(self.context, input,
|
||||
self.error_context.error_reporter);
|
||||
let arc = Arc::new(self.shared_lock.wrap(media_queries));
|
||||
Ok(AtRuleType::WithBlock(AtRuleBlockPrelude::Media(arc, location)))
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue