Skip to content

Commit

Permalink
style: Simplify media query code now that lifetimes are non-lexical.
Browse files Browse the repository at this point in the history
We can deindent and simplify a bunch of this code now.

Differential Revision: https://phabricator.services.mozilla.com/D49509
  • Loading branch information
emilio committed Nov 4, 2019
1 parent 11c1317 commit ca05003
Showing 1 changed file with 39 additions and 66 deletions.
105 changes: 39 additions & 66 deletions components/style/media_queries/media_feature_expression.rs
Expand Up @@ -244,7 +244,7 @@ fn consume_operation_or_colon(input: &mut Parser) -> Result<Option<Operator>, ()

fn disabled_by_pref(feature: &Atom) -> bool {
if *feature == atom!("-moz-touch-enabled") {
return !static_prefs::pref!("layout.css.moz-touch-enabled.enabled")
return !static_prefs::pref!("layout.css.moz-touch-enabled.enabled");
}
false
}
Expand Down Expand Up @@ -286,80 +286,53 @@ impl MediaFeatureExpression {
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
// FIXME: remove extra indented block when lifetimes are non-lexical
let feature_index;
let feature;
let range;
{
let location = input.current_source_location();
let ident = input.expect_ident()?;

let mut requirements = ParsingRequirements::empty();

if context.in_ua_or_chrome_sheet() {
requirements.insert(ParsingRequirements::CHROME_AND_UA_ONLY);
}
let mut requirements = ParsingRequirements::empty();
let location = input.current_source_location();
let ident = input.expect_ident()?;

let result = {
let mut feature_name = &**ident;
if context.in_ua_or_chrome_sheet() {
requirements.insert(ParsingRequirements::CHROME_AND_UA_ONLY);
}

if starts_with_ignore_ascii_case(feature_name, "-webkit-") {
feature_name = &feature_name[8..];
requirements.insert(ParsingRequirements::WEBKIT_PREFIX);
}
let mut feature_name = &**ident;

let range = if starts_with_ignore_ascii_case(feature_name, "min-") {
feature_name = &feature_name[4..];
Some(Range::Min)
} else if starts_with_ignore_ascii_case(feature_name, "max-") {
feature_name = &feature_name[4..];
Some(Range::Max)
} else {
None
};

let atom = Atom::from(string_as_ascii_lowercase(feature_name));

if disabled_by_pref(&atom) {
return Err(location.new_custom_error(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
));
}
if starts_with_ignore_ascii_case(feature_name, "-webkit-") {
feature_name = &feature_name[8..];
requirements.insert(ParsingRequirements::WEBKIT_PREFIX);
}

match MEDIA_FEATURES
.iter()
.enumerate()
.find(|(_, f)| f.name == atom)
{
Some((i, f)) => Ok((i, f, range)),
None => Err(()),
}
};
let range = if starts_with_ignore_ascii_case(feature_name, "min-") {
feature_name = &feature_name[4..];
Some(Range::Min)
} else if starts_with_ignore_ascii_case(feature_name, "max-") {
feature_name = &feature_name[4..];
Some(Range::Max)
} else {
None
};

match result {
Ok((i, f, r)) => {
feature_index = i;
feature = f;
range = r;
},
Err(()) => {
return Err(location.new_custom_error(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
));
},
}
let atom = Atom::from(string_as_ascii_lowercase(feature_name));

if !(feature.requirements & !requirements).is_empty() {
let (feature_index, feature) = match MEDIA_FEATURES
.iter()
.enumerate()
.find(|(_, f)| f.name == atom)
{
Some((i, f)) => (i, f),
None => {
return Err(location.new_custom_error(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
));
}
))
},
};

if range.is_some() && !feature.allows_ranges() {
return Err(location.new_custom_error(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
));
}
if disabled_by_pref(&feature.name) ||
!requirements.contains(feature.requirements) ||
(range.is_some() && !feature.allows_ranges())
{
return Err(location.new_custom_error(
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
));
}

let operator = input.try(consume_operation_or_colon);
Expand Down

0 comments on commit ca05003

Please sign in to comment.