Add pseudo_element_variant helper function to simplify some code. r?heycam draft
authorXidorn Quan <me@upsuper.org>
Tue, 11 Jul 2017 10:16:08 +1000
changeset 606498 31ff0cb3adf6a831ef783eac757b1bf396913c14
parent 606467 a4bfda6861dd85f64f056d47e48a4d2eef82ee8a
child 606499 978ebe8ddb87a7dd4b27f7e2f6e122d14921eafd
push id67712
push userxquan@mozilla.com
push dateTue, 11 Jul 2017 00:23:47 +0000
reviewersheycam
milestone56.0a1
Add pseudo_element_variant helper function to simplify some code. r?heycam MozReview-Commit-ID: FaqKdrTcdnE
servo/components/style/gecko/pseudo_element_definition.mako.rs
--- 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