/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/// <https://drafts.csswg.org/css-shadow-parts/#parsing-mapping> /// /// Returns `None` in the failure case. pubfn parse_part_mapping(input: &str) -> Option<Mapping<'_>> { // Step 1. Let input be the string being parsed. // Step 2. Let position be a pointer into input, initially pointing at the start of the string. // NOTE: We don't need an explicit position, we just drop stuff from the input
// Step 3. Collect a sequence of code points that are space characters let input = input.trim_start_matches(|c| c == ' ');
// Step 4. Collect a sequence of code points that are not space characters or U+003A COLON characters, // and let first token be the result. let space_or_colon_position = input
.char_indices()
.find(|(_, c)| matches!(c, ' ' | ':'))
.map(|(index, _)| index)
.unwrap_or(input.len()); let (first_token, input) = input.split_at(space_or_colon_position);
// Step 5. If first token is empty then return error. if first_token.is_empty() { return None;
}
// Step 6. Collect a sequence of code points that are space characters. let input = input.trim_start_matches(|c| c == ' ');
// Step 7. If the end of the input has been reached, return the tuple (first token, first token) if input.is_empty() { return Some((first_token, first_token));
}
// Step 8. If character at position is not a U+003A COLON character, return error. // Step 9. Consume the U+003A COLON character. let Some(input) = input.strip_prefix(':') else { return None;
};
// Step 10. Collect a sequence of code points that are space characters. let input = input.trim_start_matches(|c| c == ' ');
// Step 11. Collect a sequence of code points that are not space characters or U+003A COLON characters. // and let second token be the result. let space_or_colon_position = input
.char_indices()
.find(|(_, c)| matches!(c, ' ' | ':'))
.map(|(index, _)| index)
.unwrap_or(input.len()); let (second_token, input) = input.split_at(space_or_colon_position);
// Step 12. If second token is empty then return error. if second_token.is_empty() { return None;
}
// Step 13. Collect a sequence of code points that are space characters. let input = input.trim_start_matches(|c| c == ' ');
// Step 14. If position is not past the end of input then return error. if !input.is_empty() { return None;
}
// Step 14. Return the tuple (first token, second token).
Some((first_token, second_token))
}
/// <https://drafts.csswg.org/css-shadow-parts/#parsing-mapping-list> fn parse_mapping_list(input: &str) -> impl Iterator<Item = Mapping<'_>> { // Step 1. Let input be the string being parsed. // Step 2. Split the string input on commas. Let unparsed mappings be the resulting list of strings. let unparsed_mappings = input.split(',');
// Step 3. Let mappings be an initially empty list of tuples of tokens. // This list will be the result of this algorithm. // NOTE: We return an iterator here - it is up to the caller to turn it into a list
// Step 4. For each string unparsed mapping in unparsed mappings, run the following substeps:
unparsed_mappings.filter_map(|unparsed_mapping| { // Step 4.1 If unparsed mapping is empty or contains only space characters, // continue to the next iteration of the loop. if unparsed_mapping.chars().all(|c| c == ' ') { return None;
}
// Step 4.2 Let mapping be the result of parsing unparsed mapping using the rules for parsing part mappings. // Step 4.3 If mapping is an error then continue to the next iteration of the loop. // This allows clients to skip over new syntax that is not understood. // Step 4.4 Append mapping to mappings.
parse_part_mapping(unparsed_mapping)
})
}
/// Call the provided callback for each exported part with the given name. pubfn for_each_exported_part<F>(&self, name: &Atom, mut callback: F) where
F: FnMut(&AtomIdent),
{ for (from, to) in &self.mappings { if from == name {
callback(AtomIdent::cast(to));
}
}
}
#[test] fn parse_valid_mapping_list() { letmut mappings = parse_mapping_list("foo: bar, totally-invalid-mapping,,");
// "foo: bar" is a valid mapping
assert_eq!(
mappings.next(),
Some(("foo", "bar")), "First mapping should be in the list"
); // "totally-invalid-mapping" is not a valid mapping and should be ignored // "" is not valid (and consists of nothing but whitespace), so it should be ignored
assert!(mappings.next().is_none(), "No more mappings should exist");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.