Bug 1341985 - Part 7: Add a utility function to convert PseudoElement to nsIAtom*. r?heycam draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Fri, 10 Mar 2017 10:32:25 +0900
changeset 496324 4c4631a768350e89507481803f4d4d582f7d33f5
parent 496323 1b28ef0ad5b14298cb993e786e362937233bb65b
child 496325 c038533e701a49cb8f2b11ed6f481e65a9ac2881
push id48568
push userhikezoe@mozilla.com
push dateFri, 10 Mar 2017 01:36:35 +0000
reviewersheycam
bugs1341985
milestone55.0a1
Bug 1341985 - Part 7: Add a utility function to convert PseudoElement to nsIAtom*. r?heycam MozReview-Commit-ID: 14qYFpiW0iG
servo/components/style/gecko/selector_parser.rs
servo/components/style/gecko/wrapper.rs
--- a/servo/components/style/gecko/selector_parser.rs
+++ b/servo/components/style/gecko/selector_parser.rs
@@ -2,21 +2,23 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! Gecko-specific bits for selector-parsing.
 
 use cssparser::ToCss;
 use element_state::ElementState;
 use gecko_bindings::structs::CSSPseudoClassType;
+use gecko_bindings::structs::nsIAtom;
 use selector_parser::{SelectorParser, PseudoElementCascadeType};
 use selector_parser::{attr_equals_selector_is_shareable, attr_exists_selector_is_shareable};
 use selectors::parser::AttrSelector;
 use std::borrow::Cow;
 use std::fmt;
+use std::ptr;
 use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
 
 /// A representation of a CSS pseudo-element.
 ///
 /// In Gecko, we represent pseudo-elements as plain `Atom`s.
 ///
 /// The boolean field represents whether this element is an anonymous box. This
 /// is just for convenience, instead of recomputing it.
@@ -106,16 +108,22 @@ impl PseudoElement {
                 }
             }}
         }
 
         include!("generated/gecko_pseudo_element_helper.rs");
 
         None
     }
+
+    /// Returns null or nsIAtom pointer corresponding to a given PseudoElement.
+    #[inline]
+    pub fn ns_atom_or_null_from_opt(pseudo: Option<&PseudoElement>) -> *mut nsIAtom {
+        pseudo.map(|p| p.as_atom().as_ptr()).unwrap_or(ptr::null_mut())
+    }
 }
 
 impl ToCss for PseudoElement {
     fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
         // FIXME: why does the atom contain one colon? Pseudo-element has two
         debug_assert!(self.0.as_slice().starts_with(&[b':' as u16]) &&
                       !self.0.as_slice().starts_with(&[b':' as u16, b':' as u16]));
         try!(dest.write_char(':'));
--- a/servo/components/style/gecko/wrapper.rs
+++ b/servo/components/style/gecko/wrapper.rs
@@ -411,17 +411,17 @@ impl<'le> TElement for GeckoElement<'le>
     }
 
     fn style_attribute(&self) -> Option<&Arc<RwLock<PropertyDeclarationBlock>>> {
         let declarations = unsafe { Gecko_GetStyleAttrDeclarationBlock(self.0) };
         declarations.map(|s| s.as_arc_opt()).unwrap_or(None)
     }
 
     fn get_animation_rules(&self, pseudo: Option<&PseudoElement>) -> AnimationRules {
-        let atom_ptr = pseudo.map(|p| p.as_atom().as_ptr()).unwrap_or(ptr::null_mut());
+        let atom_ptr = PseudoElement::ns_atom_or_null_from_opt(pseudo);
         unsafe {
             AnimationRules(
                 Gecko_GetAnimationRule(self.0, atom_ptr, CascadeLevel::Animations).into_arc_opt(),
                 Gecko_GetAnimationRule(self.0, atom_ptr, CascadeLevel::Transitions).into_arc_opt())
         }
     }
 
     fn get_state(&self) -> ElementState {
@@ -449,19 +449,18 @@ impl<'le> TElement for GeckoElement<'le>
                                        /* ignoreCase = */ false)
         }
     }
 
     fn existing_style_for_restyle_damage<'a>(&'a self,
                                              _existing_values: &'a Arc<ComputedValues>,
                                              pseudo: Option<&PseudoElement>)
                                              -> Option<&'a nsStyleContext> {
+        let atom_ptr = PseudoElement::ns_atom_or_null_from_opt(pseudo);
         unsafe {
-            let atom_ptr = pseudo.map(|p| p.as_atom().as_ptr())
-                                 .unwrap_or(ptr::null_mut());
             let context_ptr = Gecko_GetStyleContext(self.as_node().0, atom_ptr);
             context_ptr.as_ref()
         }
     }
 
     fn has_dirty_descendants(&self) -> bool {
         self.flags() & (NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO as u32) != 0
     }