style: Remove PropertyId::name.

It's only used for the error path in property parsing, so most of the time is
not useful.

Use the just-introduced NonCustomPropertyId::name to preserve the alias name,
which we were doing by passing the name around.

Bug: 1466645
Reviewed-by: xidorn
MozReview-Commit-ID: 46xxZKCoeBB
This commit is contained in:
Emilio Cobos Álvarez 2018-06-04 20:57:49 +02:00
parent 7529788375
commit 915c8725ae
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 47 additions and 42 deletions

View file

@ -1116,9 +1116,7 @@ where
let mut parser = Parser::new(&mut input); let mut parser = Parser::new(&mut input);
let start_position = parser.position(); let start_position = parser.position();
parser.parse_entirely(|parser| { parser.parse_entirely(|parser| {
let name = id.name().into(); PropertyDeclaration::parse_into(declarations, id, &context, parser)
PropertyDeclaration::parse_into(declarations, id, name, &context, parser)
.map_err(|e| e.into())
}).map_err(|err| { }).map_err(|err| {
let location = err.location; let location = err.location;
let error = ContextualParseError::UnsupportedPropertyDeclaration( let error = ContextualParseError::UnsupportedPropertyDeclaration(
@ -1169,7 +1167,7 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for PropertyDeclarationParser<'a, 'b> {
} }
}; };
input.parse_until_before(Delimiter::Bang, |input| { input.parse_until_before(Delimiter::Bang, |input| {
PropertyDeclaration::parse_into(self.declarations, id, name, self.context, input) PropertyDeclaration::parse_into(self.declarations, id, self.context, input)
})?; })?;
let importance = match input.try(parse_important) { let importance = match input.try(parse_important) {
Ok(()) => Importance::Important, Ok(()) => Importance::Important,

View file

@ -23,7 +23,7 @@ use std::fmt::{self, Write};
use std::mem::{self, ManuallyDrop}; use std::mem::{self, ManuallyDrop};
#[cfg(feature = "servo")] use cssparser::RGBA; #[cfg(feature = "servo")] use cssparser::RGBA;
use cssparser::{CowRcStr, Parser, TokenSerializationType, serialize_identifier}; use cssparser::{Parser, TokenSerializationType};
use cssparser::ParserInput; use cssparser::ParserInput;
#[cfg(feature = "servo")] use euclid::SideOffsets2D; #[cfg(feature = "servo")] use euclid::SideOffsets2D;
use context::QuirksMode; use context::QuirksMode;
@ -1765,21 +1765,6 @@ impl PropertyId {
} }
} }
/// Returns the name of the property without CSS escaping.
pub fn name(&self) -> Cow<'static, str> {
match *self {
PropertyId::ShorthandAlias(id, _) |
PropertyId::Shorthand(id) => id.name().into(),
PropertyId::LonghandAlias(id, _) |
PropertyId::Longhand(id) => id.name().into(),
PropertyId::Custom(ref name) => {
let mut s = String::new();
write!(&mut s, "--{}", name).unwrap();
s.into()
}
}
}
fn non_custom_id(&self) -> Option<NonCustomPropertyId> { fn non_custom_id(&self) -> Option<NonCustomPropertyId> {
Some(match *self { Some(match *self {
PropertyId::Custom(_) => return None, PropertyId::Custom(_) => return None,
@ -2042,13 +2027,13 @@ impl PropertyDeclaration {
pub fn parse_into<'i, 't>( pub fn parse_into<'i, 't>(
declarations: &mut SourcePropertyDeclaration, declarations: &mut SourcePropertyDeclaration,
id: PropertyId, id: PropertyId,
name: CowRcStr<'i>,
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<(), ParseError<'i>> { ) -> Result<(), ParseError<'i>> {
assert!(declarations.is_empty()); assert!(declarations.is_empty());
debug_assert!(id.allowed_in(context), "{:?}", id); debug_assert!(id.allowed_in(context), "{:?}", id);
let non_custom_id = id.non_custom_id();
let start = input.state(); let start = input.state();
match id { match id {
PropertyId::Custom(property_name) => { PropertyId::Custom(property_name) => {
@ -2059,7 +2044,10 @@ impl PropertyDeclaration {
Ok(keyword) => DeclaredValueOwned::CSSWideKeyword(keyword), Ok(keyword) => DeclaredValueOwned::CSSWideKeyword(keyword),
Err(()) => match ::custom_properties::SpecifiedValue::parse(input) { Err(()) => match ::custom_properties::SpecifiedValue::parse(input) {
Ok(value) => DeclaredValueOwned::Value(value), Ok(value) => DeclaredValueOwned::Value(value),
Err(e) => return Err(StyleParseErrorKind::new_invalid(name, e)), Err(e) => return Err(StyleParseErrorKind::new_invalid(
format!("--{}", property_name),
e,
)),
} }
}; };
declarations.push(PropertyDeclaration::Custom(CustomDeclaration { declarations.push(PropertyDeclaration::Custom(CustomDeclaration {
@ -2084,7 +2072,10 @@ impl PropertyDeclaration {
input.reset(&start); input.reset(&start);
let (first_token_type, css) = let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input).map_err(|e| { ::custom_properties::parse_non_custom_with_var(input).map_err(|e| {
StyleParseErrorKind::new_invalid(name, e) StyleParseErrorKind::new_invalid(
non_custom_id.unwrap().name(),
e,
)
})?; })?;
Ok(PropertyDeclaration::WithVariables(VariableDeclaration { Ok(PropertyDeclaration::WithVariables(VariableDeclaration {
id, id,
@ -2096,7 +2087,10 @@ impl PropertyDeclaration {
}), }),
})) }))
} else { } else {
Err(StyleParseErrorKind::new_invalid(name, err)) Err(StyleParseErrorKind::new_invalid(
non_custom_id.unwrap().name(),
err,
))
} }
}) })
}).map(|declaration| { }).map(|declaration| {
@ -2130,7 +2124,10 @@ impl PropertyDeclaration {
input.reset(&start); input.reset(&start);
let (first_token_type, css) = let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input).map_err(|e| { ::custom_properties::parse_non_custom_with_var(input).map_err(|e| {
StyleParseErrorKind::new_invalid(name, e) StyleParseErrorKind::new_invalid(
non_custom_id.unwrap().name(),
e,
)
})?; })?;
let unparsed = Arc::new(UnparsedValue { let unparsed = Arc::new(UnparsedValue {
css: css.into_owned(), css: css.into_owned(),
@ -2152,7 +2149,10 @@ impl PropertyDeclaration {
} }
Ok(()) Ok(())
} else { } else {
Err(StyleParseErrorKind::new_invalid(name, err)) Err(StyleParseErrorKind::new_invalid(
non_custom_id.unwrap().name(),
err,
))
} }
}) })
} }

View file

@ -623,12 +623,12 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for KeyframeDeclarationParser<'a, 'b> {
let id = match PropertyId::parse(&name, self.context) { let id = match PropertyId::parse(&name, self.context) {
Ok(id) => id, Ok(id) => id,
Err(()) => return Err(input.new_custom_error( Err(()) => return Err(input.new_custom_error(
StyleParseErrorKind::UnknownProperty(name.clone()) StyleParseErrorKind::UnknownProperty(name)
)), )),
}; };
// TODO(emilio): Shouldn't this use parse_entirely? // TODO(emilio): Shouldn't this use parse_entirely?
PropertyDeclaration::parse_into(self.declarations, id, name, self.context, input)?; PropertyDeclaration::parse_into(self.declarations, id, self.context, input)?;
// In case there is still unparsed text in the declaration, we should // In case there is still unparsed text in the declaration, we should
// roll back. // roll back.

View file

@ -316,20 +316,23 @@ impl Declaration {
let mut input = ParserInput::new(&self.0); let mut input = ParserInput::new(&self.0);
let mut input = Parser::new(&mut input); let mut input = Parser::new(&mut input);
input.parse_entirely(|input| -> Result<(), CssParseError<()>> { input.parse_entirely(|input| -> Result<(), CssParseError<()>> {
let prop = input.expect_ident_cloned().unwrap(); let prop = input.expect_ident_cloned().unwrap();
input.expect_colon().unwrap(); input.expect_colon().unwrap();
let id = PropertyId::parse(&prop, context) let id = PropertyId::parse(&prop, context)
.map_err(|_| input.new_custom_error(()))?; .map_err(|_| input.new_custom_error(()))?;
let mut declarations = SourcePropertyDeclaration::new(); let mut declarations = SourcePropertyDeclaration::new();
input.parse_until_before(Delimiter::Bang, |input| { input.parse_until_before(Delimiter::Bang, |input| {
PropertyDeclaration::parse_into(&mut declarations, id, prop, &context, input) PropertyDeclaration::parse_into(
.map_err(|_| input.new_custom_error(())) &mut declarations,
})?; id,
let _ = input.try(parse_important); &context,
Ok(()) input,
}) ).map_err(|_| input.new_custom_error(()))
.is_ok() })?;
let _ = input.try(parse_important);
Ok(())
}).is_ok()
} }
} }

View file

@ -177,7 +177,11 @@ pub enum ValueParseErrorKind<'i> {
impl<'i> StyleParseErrorKind<'i> { impl<'i> StyleParseErrorKind<'i> {
/// Create an InvalidValue parse error /// Create an InvalidValue parse error
pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> ParseError<'i> { pub fn new_invalid<S>(name: S, value_error: ParseError<'i>) -> ParseError<'i>
where
S: Into<CowRcStr<'i>>,
{
let name = name.into();
let variant = match value_error.kind { let variant = match value_error.kind {
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => { cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => {
match e { match e {