Auto merge of #17967 - jdm:parsefun, r=SimonSapin

CSS parsing optimizations

These address some small inefficiencies that showed up while profiling the myspace talos test.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- 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/17967)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-08-04 02:10:05 -05:00 committed by GitHub
commit aef95cf195
2 changed files with 12 additions and 13 deletions

View file

@ -30,7 +30,7 @@ use font_metrics::FontMetricsProvider;
#[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide}; #[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide};
use logical_geometry::WritingMode; use logical_geometry::WritingMode;
use media_queries::Device; use media_queries::Device;
use parser::{Parse, ParserContext}; use parser::ParserContext;
use properties::animated_properties::AnimatableLonghand; use properties::animated_properties::AnimatableLonghand;
#[cfg(feature = "gecko")] use properties::longhands::system_font::SystemFont; #[cfg(feature = "gecko")] use properties::longhands::system_font::SystemFont;
use selector_parser::PseudoElement; use selector_parser::PseudoElement;
@ -421,12 +421,11 @@ impl CSSWideKeyword {
} }
} }
impl Parse for CSSWideKeyword { impl CSSWideKeyword {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse(input: &mut Parser) -> Result<Self, ()> {
let ident = input.expect_ident()?.clone(); let ident = input.expect_ident().map_err(|_| ())?.clone();
input.expect_exhausted()?; input.expect_exhausted().map_err(|_| ())?;
CSSWideKeyword::from_ident(&ident) CSSWideKeyword::from_ident(&ident).ok_or(())
.ok_or(SelectorParseError::UnexpectedIdent(ident).into())
} }
} }
@ -1486,9 +1485,9 @@ impl PropertyDeclaration {
id.check_allowed_in(rule_type, context.stylesheet_origin)?; id.check_allowed_in(rule_type, context.stylesheet_origin)?;
match id { match id {
PropertyId::Custom(name) => { PropertyId::Custom(name) => {
let value = match input.try(|i| CSSWideKeyword::parse(context, i)) { let value = match input.try(|i| CSSWideKeyword::parse(i)) {
Ok(keyword) => DeclaredValueOwned::CSSWideKeyword(keyword), Ok(keyword) => DeclaredValueOwned::CSSWideKeyword(keyword),
Err(_) => match ::custom_properties::SpecifiedValue::parse(context, input) { Err(()) => match ::custom_properties::SpecifiedValue::parse(context, input) {
Ok(value) => DeclaredValueOwned::Value(value), Ok(value) => DeclaredValueOwned::Value(value),
Err(e) => return Err(PropertyDeclarationParseError::InvalidValue(name.to_string().into(), Err(e) => return Err(PropertyDeclarationParseError::InvalidValue(name.to_string().into(),
ValueParseError::from_parse_error(e))), ValueParseError::from_parse_error(e))),
@ -1498,9 +1497,9 @@ impl PropertyDeclaration {
Ok(()) Ok(())
} }
PropertyId::Longhand(id) => { PropertyId::Longhand(id) => {
input.try(|i| CSSWideKeyword::parse(context, i)).map(|keyword| { input.try(|i| CSSWideKeyword::parse(i)).map(|keyword| {
PropertyDeclaration::CSSWideKeyword(id, keyword) PropertyDeclaration::CSSWideKeyword(id, keyword)
}).or_else(|_| { }).or_else(|()| {
input.look_for_var_functions(); input.look_for_var_functions();
let start = input.position(); let start = input.position();
input.parse_entirely(|input| id.parse_value(context, input)) input.parse_entirely(|input| id.parse_value(context, input))
@ -1529,7 +1528,7 @@ impl PropertyDeclaration {
}) })
} }
PropertyId::Shorthand(id) => { PropertyId::Shorthand(id) => {
if let Ok(keyword) = input.try(|i| CSSWideKeyword::parse(context, i)) { if let Ok(keyword) = input.try(|i| CSSWideKeyword::parse(i)) {
if id == ShorthandId::All { if id == ShorthandId::All {
declarations.all_shorthand = AllShorthand::CSSWideKeyword(keyword) declarations.all_shorthand = AllShorthand::CSSWideKeyword(keyword)
} else { } else {

View file

@ -77,7 +77,7 @@ fn escape_css_ident(ident: &str) -> String {
return ident.into() return ident.into()
} }
let mut escaped = String::new(); let mut escaped = String::with_capacity(ident.len());
// A leading dash does not need to be escaped as long as it is not the // A leading dash does not need to be escaped as long as it is not the
// *only* character in the identifier. // *only* character in the identifier.