servo: Merge #18745 - style: Custom properties cleanup (from emilio:custom-props-less-unwrap); r=nox

Use less unwrap and custom types in custom properties.

The idea is for this to shed some light in https://bugzilla.mozilla.org/show_bug.cgi?id=1403845.

Source-Repo: https://github.com/servo/servo
Source-Revision: 9bf299bba9cd24c25200503fce2c19047eeb1b90

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 3a6d2a4156b4e54fb54efca85c5ee32fd65e601d
This commit is contained in:
Emilio Cobos Álvarez 2017-10-05 02:18:52 -05:00
Родитель 6782409d2b
Коммит 56f9cf13ba
1 изменённых файлов: 31 добавлений и 24 удалений

Просмотреть файл

@ -197,7 +197,7 @@ where
let ref key = index[index.len() - self.pos - 1]; let ref key = index[index.len() - self.pos - 1];
self.pos += 1; self.pos += 1;
let value = self.inner.values.get(key).unwrap(); let value = &self.inner.values[key];
Some((key, value)) Some((key, value))
} }
} }
@ -244,13 +244,13 @@ impl SpecifiedValue {
_context: &ParserContext, _context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<Box<Self>, ParseError<'i>> { ) -> Result<Box<Self>, ParseError<'i>> {
let mut references = Some(PrecomputedHashSet::default()); let mut references = PrecomputedHashSet::default();
let (first, css, last) = parse_self_contained_declaration_value(input, &mut references)?; let (first, css, last) = parse_self_contained_declaration_value(input, Some(&mut references))?;
Ok(Box::new(SpecifiedValue { Ok(Box::new(SpecifiedValue {
css: css.into_owned(), css: css.into_owned(),
first_token_type: first, first_token_type: first,
last_token_type: last, last_token_type: last,
references: references.unwrap(), references
})) }))
} }
} }
@ -259,13 +259,13 @@ impl SpecifiedValue {
pub fn parse_non_custom_with_var<'i, 't> pub fn parse_non_custom_with_var<'i, 't>
(input: &mut Parser<'i, 't>) (input: &mut Parser<'i, 't>)
-> Result<(TokenSerializationType, Cow<'i, str>), ParseError<'i>> { -> Result<(TokenSerializationType, Cow<'i, str>), ParseError<'i>> {
let (first_token_type, css, _) = parse_self_contained_declaration_value(input, &mut None)?; let (first_token_type, css, _) = parse_self_contained_declaration_value(input, None)?;
Ok((first_token_type, css)) Ok((first_token_type, css))
} }
fn parse_self_contained_declaration_value<'i, 't>( fn parse_self_contained_declaration_value<'i, 't>(
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
references: &mut Option<PrecomputedHashSet<Name>> references: Option<&mut PrecomputedHashSet<Name>>
) -> Result< ) -> Result<
(TokenSerializationType, Cow<'i, str>, TokenSerializationType), (TokenSerializationType, Cow<'i, str>, TokenSerializationType),
ParseError<'i> ParseError<'i>
@ -288,7 +288,7 @@ fn parse_self_contained_declaration_value<'i, 't>(
/// https://drafts.csswg.org/css-syntax-3/#typedef-declaration-value /// https://drafts.csswg.org/css-syntax-3/#typedef-declaration-value
fn parse_declaration_value<'i, 't>( fn parse_declaration_value<'i, 't>(
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
references: &mut Option<PrecomputedHashSet<Name>>, references: Option<&mut PrecomputedHashSet<Name>>,
missing_closing_characters: &mut String missing_closing_characters: &mut String
) -> Result<(TokenSerializationType, TokenSerializationType), ParseError<'i>> { ) -> Result<(TokenSerializationType, TokenSerializationType), ParseError<'i>> {
input.parse_until_before(Delimiter::Bang | Delimiter::Semicolon, |input| { input.parse_until_before(Delimiter::Bang | Delimiter::Semicolon, |input| {
@ -305,7 +305,7 @@ fn parse_declaration_value<'i, 't>(
/// invalid at the top level /// invalid at the top level
fn parse_declaration_value_block<'i, 't>( fn parse_declaration_value_block<'i, 't>(
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
references: &mut Option<PrecomputedHashSet<Name>>, mut references: Option<&mut PrecomputedHashSet<Name>>,
missing_closing_characters: &mut String missing_closing_characters: &mut String
) -> Result<(TokenSerializationType, TokenSerializationType), ParseError<'i>> { ) -> Result<(TokenSerializationType, TokenSerializationType), ParseError<'i>> {
let mut token_start = input.position(); let mut token_start = input.position();
@ -319,7 +319,11 @@ fn parse_declaration_value_block<'i, 't>(
macro_rules! nested { macro_rules! nested {
() => { () => {
input.parse_nested_block(|input| { input.parse_nested_block(|input| {
parse_declaration_value_block(input, references, missing_closing_characters) parse_declaration_value_block(
input,
references.as_mut().map(|r| &mut **r),
missing_closing_characters
)
})? })?
} }
} }
@ -353,7 +357,10 @@ fn parse_declaration_value_block<'i, 't>(
if name.eq_ignore_ascii_case("var") { if name.eq_ignore_ascii_case("var") {
let args_start = input.state(); let args_start = input.state();
input.parse_nested_block(|input| { input.parse_nested_block(|input| {
parse_var_function(input, references) parse_var_function(
input,
references.as_mut().map(|r| &mut **r),
)
})?; })?;
input.reset(&args_start); input.reset(&args_start);
} }
@ -420,7 +427,7 @@ fn parse_declaration_value_block<'i, 't>(
// If the var function is valid, return Ok((custom_property_name, fallback)) // If the var function is valid, return Ok((custom_property_name, fallback))
fn parse_var_function<'i, 't>( fn parse_var_function<'i, 't>(
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
references: &mut Option<PrecomputedHashSet<Name>> references: Option<&mut PrecomputedHashSet<Name>>
) -> Result<(), ParseError<'i>> { ) -> Result<(), ParseError<'i>> {
let name = input.expect_ident_cloned()?; let name = input.expect_ident_cloned()?;
let name: Result<_, ParseError> = let name: Result<_, ParseError> =
@ -438,7 +445,7 @@ fn parse_var_function<'i, 't>(
Ok(()) Ok(())
})?; })?;
} }
if let Some(ref mut refs) = *references { if let Some(refs) = references {
refs.insert(Atom::from(name)); refs.insert(Atom::from(name));
} }
Ok(()) Ok(())
@ -463,8 +470,7 @@ pub fn cascade<'a>(
None => { None => {
let mut map = OrderedMap::new(); let mut map = OrderedMap::new();
if let Some(inherited) = inherited { if let Some(inherited) = inherited {
for name in &inherited.index { for (name, inherited_value) in inherited.iter() {
let inherited_value = inherited.get(name).unwrap();
map.insert(name, BorrowedSpecifiedValue { map.insert(name, BorrowedSpecifiedValue {
css: &inherited_value.css, css: &inherited_value.css,
first_token_type: inherited_value.first_token_type, first_token_type: inherited_value.first_token_type,
@ -568,9 +574,7 @@ fn substitute_all(
) -> CustomPropertiesMap { ) -> CustomPropertiesMap {
let mut custom_properties_map = CustomPropertiesMap::new(); let mut custom_properties_map = CustomPropertiesMap::new();
let mut invalid = PrecomputedHashSet::default(); let mut invalid = PrecomputedHashSet::default();
for name in &specified_values_map.index { for (name, value) in specified_values_map.iter() {
let value = specified_values_map.get(name).unwrap();
// If this value is invalid at computed-time it wont be inserted in computed_values_map. // If this value is invalid at computed-time it wont be inserted in computed_values_map.
// Nothing else to do. // Nothing else to do.
let _ = substitute_one( let _ = substitute_one(
@ -603,7 +607,7 @@ fn substitute_one(
if invalid.contains(name) { if invalid.contains(name) {
return Err(()); return Err(());
} }
let computed_value = if specified_value.references.map(|set| set.is_empty()) == Some(false) { let computed_value = if specified_value.references.map_or(false, |set| !set.is_empty()) {
let mut partial_computed_value = ComputedValue::empty(); let mut partial_computed_value = ComputedValue::empty();
let mut input = ParserInput::new(&specified_value.css); let mut input = ParserInput::new(&specified_value.css);
let mut input = Parser::new(&mut input); let mut input = Parser::new(&mut input);
@ -652,12 +656,15 @@ fn substitute_one(
/// ///
/// Return `Err(())` if `input` is invalid at computed-value time. /// Return `Err(())` if `input` is invalid at computed-value time.
/// or `Ok(last_token_type that was pushed to partial_computed_value)` otherwise. /// or `Ok(last_token_type that was pushed to partial_computed_value)` otherwise.
fn substitute_block<'i, 't, F>(input: &mut Parser<'i, 't>, fn substitute_block<'i, 't, F>(
input: &mut Parser<'i, 't>,
position: &mut (SourcePosition, TokenSerializationType), position: &mut (SourcePosition, TokenSerializationType),
partial_computed_value: &mut ComputedValue, partial_computed_value: &mut ComputedValue,
substitute_one: &mut F) substitute_one: &mut F
-> Result<TokenSerializationType, ParseError<'i>> ) -> Result<TokenSerializationType, ParseError<'i>>
where F: FnMut(&Name, &mut ComputedValue) -> Result<TokenSerializationType, ()> { where
F: FnMut(&Name, &mut ComputedValue) -> Result<TokenSerializationType, ()>
{
let mut last_token_type = TokenSerializationType::nothing(); let mut last_token_type = TokenSerializationType::nothing();
let mut set_position_at_next_iteration = false; let mut set_position_at_next_iteration = false;
loop { loop {