Add pseudo_element_variant helper function to simplify some code. r?heycam
draft
Add pseudo_element_variant helper function to simplify some code. r?heycam
MozReview-Commit-ID: FaqKdrTcdnE
--- a/servo/components/style/gecko/pseudo_element_definition.mako.rs
+++ b/servo/components/style/gecko/pseudo_element_definition.mako.rs
@@ -18,91 +18,98 @@ pub const EAGER_PSEUDO_COUNT: usize = ${
/// The list of eager pseudos.
pub const EAGER_PSEUDOS: [PseudoElement; EAGER_PSEUDO_COUNT] = [
% for eager_pseudo_name in EAGER_PSEUDOS:
PseudoElement::${eager_pseudo_name},
% endfor
];
+<%def name="pseudo_element_variant(pseudo)">
+ PseudoElement::${pseudo.capitalized()}
+</%def>
+
impl PseudoElement {
/// Executes a closure with each simple (not functional)
/// pseudo-element as an argument.
pub fn each_simple<F>(mut fun: F)
where F: FnMut(Self),
{
% for pseudo in PSEUDOS:
- fun(PseudoElement::${pseudo.capitalized()});
+ fun(${pseudo_element_variant(pseudo)});
% endfor
}
/// Get the pseudo-element as an atom.
#[inline]
pub fn atom(&self) -> Atom {
match *self {
% for pseudo in PSEUDOS:
- PseudoElement::${pseudo.capitalized()} => atom!("${pseudo.value}"),
+ ${pseudo_element_variant(pseudo)} => atom!("${pseudo.value}"),
% endfor
}
}
/// Whether this pseudo-element is an anonymous box.
#[inline]
fn is_anon_box(&self) -> bool {
match *self {
% for pseudo in PSEUDOS:
- PseudoElement::${pseudo.capitalized()} => ${str(pseudo.is_anon_box()).lower()},
+ % if pseudo.is_anon_box():
+ ${pseudo_element_variant(pseudo)} => true,
+ % endif
% endfor
+ _ => false,
}
}
/// Whether this pseudo-element is eagerly-cascaded.
#[inline]
pub fn is_eager(&self) -> bool {
matches!(*self,
${" | ".join(map(lambda name: "PseudoElement::{}".format(name), EAGER_PSEUDOS))})
}
/// Gets the flags associated to this pseudo-element, or 0 if it's an
/// anonymous box.
pub fn flags(&self) -> u32 {
match *self {
% for pseudo in PSEUDOS:
- PseudoElement::${pseudo.capitalized()} =>
+ ${pseudo_element_variant(pseudo)} =>
% if pseudo.is_anon_box():
structs::CSS_PSEUDO_ELEMENT_UA_SHEET_ONLY,
% else:
structs::SERVO_CSS_PSEUDO_ELEMENT_FLAGS_${pseudo.original_ident},
% endif
% endfor
}
}
/// Construct a pseudo-element from a `CSSPseudoElementType`.
#[inline]
pub fn from_pseudo_type(type_: CSSPseudoElementType) -> Option<Self> {
match type_ {
% for pseudo in PSEUDOS:
% if not pseudo.is_anon_box():
CSSPseudoElementType::${pseudo.original_ident} => {
- Some(PseudoElement::${pseudo.capitalized()})
+ Some(${pseudo_element_variant(pseudo)})
},
% endif
% endfor
_ => None,
}
}
/// Construct a pseudo-element from an anonymous box `Atom`.
#[inline]
pub fn from_anon_box_atom(atom: &Atom) -> Option<Self> {
% for pseudo in PSEUDOS:
% if pseudo.is_anon_box():
if atom == &atom!("${pseudo.value}") {
- return Some(PseudoElement::${pseudo.capitalized()});
+ return Some(${pseudo_element_variant(pseudo)});
}
% endif
% endfor
None
}
/// Constructs an atom from a string of text, and whether we're in a
/// user-agent stylesheet.
@@ -111,19 +118,19 @@ impl PseudoElement {
/// box pseudo-elements.
///
/// Returns `None` if the pseudo-element is not recognised.
#[inline]
pub fn from_slice(s: &str, in_ua_stylesheet: bool) -> Option<Self> {
use std::ascii::AsciiExt;
% for pseudo in PSEUDOS:
- if in_ua_stylesheet || PseudoElement::${pseudo.capitalized()}.exposed_in_non_ua_sheets() {
+ if in_ua_stylesheet || ${pseudo_element_variant(pseudo)}.exposed_in_non_ua_sheets() {
if s.eq_ignore_ascii_case("${pseudo.value[1:]}") {
- return Some(PseudoElement::${pseudo.capitalized()})
+ return Some(${pseudo_element_variant(pseudo)});
}
}
% endfor
None
}
/// Returns the pseudo-element's definition as a string, with only one colon